Skip to content

Commit

Permalink
chore(files): explicit 'not set' in missing stats
Browse files Browse the repository at this point in the history
  • Loading branch information
lidel committed Aug 13, 2024
1 parent 4817ac7 commit f0fea76
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 15 deletions.
27 changes: 16 additions & 11 deletions core/commands/files.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ Mtime: <mtime>`
filesFormatOptionName = "format"
filesSizeOptionName = "size"
filesWithLocalOptionName = "with-local"
filesStatUnspecified = "not set"
)

var filesStatCmd = &cmds.Command{
Expand All @@ -172,7 +173,8 @@ var filesStatCmd = &cmds.Command{
},
Options: []cmds.Option{
cmds.StringOption(filesFormatOptionName, "Print statistics in given format. Allowed tokens: "+
"<hash> <size> <cumulsize> <type> <childs>. Conflicts with other format options.").WithDefault(defaultStatFormat),
"<hash> <size> <cumulsize> <type> <childs> and optional <mode> <mode-octal> <mtime> <mtime-secs> <mtime-nsecs>."+
"Conflicts with other format options.").WithDefault(defaultStatFormat),
cmds.BoolOption(filesHashOptionName, "Print only hash. Implies '--format=<hash>'. Conflicts with other format options."),
cmds.BoolOption(filesSizeOptionName, "Print only size. Implies '--format=<cumulsize>'. Conflicts with other format options."),
cmds.BoolOption(filesWithLocalOptionName, "Compute the amount of the dag that is local, and if possible the total size"),
Expand Down Expand Up @@ -243,13 +245,16 @@ var filesStatCmd = &cmds.Command{
},
Encoders: cmds.EncoderMap{
cmds.Text: cmds.MakeTypedEncoder(func(req *cmds.Request, w io.Writer, out *statOutput) error {
var mode os.FileMode
mode, modeo := filesStatUnspecified, filesStatUnspecified
if out.Mode != 0 {
mode = os.FileMode(out.Mode)
mode = strings.ToLower(os.FileMode(out.Mode).String())
modeo = "0" + strconv.FormatInt(int64(out.Mode&0x1FF), 8)
}
var mtime string
mtime, mtimes, mtimens := filesStatUnspecified, filesStatUnspecified, filesStatUnspecified
if out.Mtime > 0 {
mtime = time.Unix(out.Mtime, int64(out.MtimeNsecs)).UTC().Format("2 Jan 2006, 15:04:05 MST")
mtimes = strconv.FormatInt(out.Mtime, 10)
mtimens = strconv.Itoa(out.MtimeNsecs)
}

s, _ := statGetFormatOptions(req)
Expand All @@ -258,11 +263,11 @@ var filesStatCmd = &cmds.Command{
s = strings.Replace(s, "<cumulsize>", fmt.Sprintf("%d", out.CumulativeSize), -1)
s = strings.Replace(s, "<childs>", fmt.Sprintf("%d", out.Blocks), -1)
s = strings.Replace(s, "<type>", out.Type, -1)
s = strings.Replace(s, "<mode>", strings.ToLower(mode.String()), -1)
s = strings.Replace(s, "<mode>", mode, -1)
s = strings.Replace(s, "<mode-octal>", modeo, -1)
s = strings.Replace(s, "<mtime>", mtime, -1)
s = strings.Replace(s, "<mtime-secs>", strconv.FormatInt(out.Mtime, 10), -1)
s = strings.Replace(s, "<mtime-nsecs>", strconv.Itoa(out.MtimeNsecs), -1)
s = strings.Replace(s, "<mode-octal>", "0"+strconv.FormatInt(int64(out.Mode&0x1FF), 8), -1)
s = strings.Replace(s, "<mtime-secs>", mtimes, -1)
s = strings.Replace(s, "<mtime-nsecs>", mtimens, -1)

fmt.Fprintln(w, s)

Expand Down Expand Up @@ -1399,13 +1404,13 @@ The mode argument must be specified in Unix numeric notation.
$ ipfs files stat /foo
...
Type: file
Mode: -rw-r--r--
Mode: -rw-r--r-- (0644)
...
`,
},
Arguments: []cmds.Argument{
cmds.StringArg("mode", true, false, "mode to apply to node"),
cmds.StringArg("path", true, false, "Path to target node"),
cmds.StringArg("mode", true, false, "Mode to apply to node (numeric notation)"),
cmds.StringArg("path", true, false, "Path to apply mode"),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
nd, err := cmdenv.GetNode(env)
Expand Down
8 changes: 4 additions & 4 deletions test/sharness/t0250-files-api.sh
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,8 @@ test_files_api() {
echo "Size: 4" >> file1stat_expect &&
echo "ChildBlocks: 0" >> file1stat_expect &&
echo "Type: file" >> file1stat_expect &&
echo "Mode: ---------- (00)" >> file1stat_expect &&
echo "Mtime: " >> file1stat_expect &&
echo "Mode: not set (not set)" >> file1stat_expect &&
echo "Mtime: not set" >> file1stat_expect &&
test_cmp file1stat_expect file1stat_actual
'

Expand All @@ -245,8 +245,8 @@ test_files_api() {
echo "Size: 4" >> file1stat_expect &&
echo "ChildBlocks: 0" >> file1stat_expect &&
echo "Type: file" >> file1stat_expect &&
echo "Mode: ---------- (00)" >> file1stat_expect &&
echo "Mtime: " >> file1stat_expect &&
echo "Mode: not set (not set)" >> file1stat_expect &&
echo "Mtime: not set" >> file1stat_expect &&
test_cmp file1stat_expect file1stat_actual
'

Expand Down

0 comments on commit f0fea76

Please sign in to comment.