github.com/keybase/client/go@v0.0.0-20241007131713-f10651d043c8/kbfs/kbfstool/stat.go (about)

     1  // Copyright 2016 Keybase Inc. All rights reserved.
     2  // Use of this source code is governed by a BSD
     3  // license that can be found in the LICENSE file.
     4  
     5  package main
     6  
     7  import (
     8  	"flag"
     9  	"fmt"
    10  	"time"
    11  
    12  	"github.com/keybase/client/go/kbfs/data"
    13  	"github.com/keybase/client/go/kbfs/fsrpc"
    14  	"github.com/keybase/client/go/kbfs/libkbfs"
    15  	"golang.org/x/net/context"
    16  )
    17  
    18  func statNode(ctx context.Context, config libkbfs.Config, nodePathStr string) error {
    19  	p, err := fsrpc.NewPath(nodePathStr)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	n, ei, err := p.GetNode(ctx, config)
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	// If n is non-nil, ignore the EntryInfo returned by
    30  	// p.getNode() so we can exercise the Stat() codepath. We
    31  	// can't compare the two, since they might legitimately differ
    32  	// due to races.
    33  	if n != nil {
    34  		ei, err = config.KBFSOps().Stat(ctx, n)
    35  		if err != nil {
    36  			return err
    37  		}
    38  	}
    39  
    40  	var symPathStr string
    41  	if ei.Type == data.Sym {
    42  		symPathStr = fmt.Sprintf("SymPath: %s, ", ei.SymPath)
    43  	}
    44  
    45  	mtimeStr := time.Unix(0, ei.Mtime).String()
    46  	ctimeStr := time.Unix(0, ei.Ctime).String()
    47  
    48  	fmt.Printf("{Type: %s, Size: %d, %sMtime: %s, Ctime: %s}\n", ei.Type, ei.Size, symPathStr, mtimeStr, ctimeStr)
    49  
    50  	return nil
    51  }
    52  
    53  func stat(ctx context.Context, config libkbfs.Config, args []string) (exitStatus int) {
    54  	flags := flag.NewFlagSet("kbfs stat", flag.ContinueOnError)
    55  	err := flags.Parse(args)
    56  	if err != nil {
    57  		printError("stat", err)
    58  		return 1
    59  	}
    60  
    61  	nodePaths := flags.Args()
    62  	if len(nodePaths) == 0 {
    63  		printError("stat", errAtLeastOnePath)
    64  		return 1
    65  	}
    66  
    67  	for _, nodePath := range nodePaths {
    68  		err := statNode(ctx, config, nodePath)
    69  		if err != nil {
    70  			printError("stat", err)
    71  			return 1
    72  		}
    73  	}
    74  
    75  	return 0
    76  }