Skip to content

Commit

Permalink
chore(crit): use singular form for explore options
Browse files Browse the repository at this point in the history
The options for `crit x` were using mix of plural and singular forms.
This now fixes it to use singular only. This commit also makes some
trivial bug fixes.

Signed-off-by: Prajwal S N <[email protected]>
  • Loading branch information
snprajwal committed Jul 18, 2023
1 parent f8745d1 commit 8b1fc77
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
10 changes: 5 additions & 5 deletions crit/cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -220,9 +220,9 @@ var infoCmd = &cobra.Command{

// The `crit x` command
var xCmd = &cobra.Command{
Use: "x DIR {ps|fds|mems|rss}",
Use: "x DIR {ps|fd|mem|rss}",
Short: "Explore the image directory",
Long: "Explore the image directory with one of (ps, fds, mems, rss) options",
Long: "Explore the image directory with one of (ps, fd, mem, rss) options",
// Exactly two arguments are required:
// * Path of the input directory
// * Explore type
Expand All @@ -242,14 +242,14 @@ var xCmd = &cobra.Command{
switch args[1] {
case "ps":
xData, err = c.ExplorePs()
case "fds":
case "fd":
xData, err = c.ExploreFds()
case "mems":
case "mem":
xData, err = c.ExploreMems()
case "rss":
xData, err = c.ExploreRss()
default:
err = errors.New("error exploring directory: invalid explore type")
err = errors.New("invalid explore type (supported: {ps|fd|mem|rss})")
}
if err != nil {
log.Fatal(fmt.Errorf("error exploring directory: %w", err))
Expand Down
7 changes: 6 additions & 1 deletion crit/explore.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (c *crit) ExplorePs() (*PsTree, error) {
// Fd represents the file descriptors opened in a single process
type Fd struct {
PId uint32 `json:"pId"`
Files []*File `json:"files,omitempty"`
Files []*File `json:"files"`
}

// File represents a single opened file
Expand Down Expand Up @@ -145,6 +145,11 @@ func (c *crit) ExploreFds() ([]*Fd, error) {
Path: filePath,
})

// Omit if the process has no file descriptors
if len(fdEntry.Files) == 0 {
continue
}

fds = append(fds, &fdEntry)
}

Expand Down
14 changes: 7 additions & 7 deletions crit/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ func ReadMagic(f *os.File) (string, error) {

// Helper to convert bytes into a more readable unit
func countBytes(n int64) string {
const UNIT int64 = 1024
if n < UNIT {
const unit int64 = 1024
if n < unit {
return fmt.Sprint(n, " B")
}
div, exp := UNIT, 0
for i := n / UNIT; i >= UNIT; i /= UNIT {
div *= UNIT
div, exp := unit, 0
for i := n / unit; i >= unit; i /= unit {
div *= unit
exp++
}
return fmt.Sprintf("%.1f %cB", float64(n)/float64(div), "KMGTPE"[exp])
Expand Down Expand Up @@ -152,7 +152,7 @@ func getRegFilePath(dir string, file *fdinfo.FileEntry, fID uint32) (string, err
if file.GetReg() != nil {
return file.GetReg().GetName(), nil
}
return "Unknown path", nil
return "unknown", nil
}

if regImg == nil {
Expand All @@ -168,7 +168,7 @@ func getRegFilePath(dir string, file *fdinfo.FileEntry, fID uint32) (string, err
}
}

return "Unknown path", nil
return "unknown", nil
}

// Helper to get file path of pipe files
Expand Down
4 changes: 2 additions & 2 deletions test/crit/crit-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ function command_test {

# explore image directory
$CRIT x "$TEST_IMG_DIR" ps || exit 1
$CRIT x "$TEST_IMG_DIR" fds || exit 1
$CRIT x "$TEST_IMG_DIR" mems || exit 1
$CRIT x "$TEST_IMG_DIR" fd || exit 1
$CRIT x "$TEST_IMG_DIR" mem || exit 1
$CRIT x "$TEST_IMG_DIR" rss || exit 1
}

Expand Down
2 changes: 1 addition & 1 deletion test/loop/loop.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ int main(void)
exit(1);
}

// Create a file descriptor for "crit x ./ fds" test
// Create a file descriptor for "crit x ./ fd" test
open("/dev/null", O_RDONLY);

chdir("/");
Expand Down

0 comments on commit 8b1fc77

Please sign in to comment.