Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions cmd/hdfs/count.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package main

import (
"fmt"
"os"
)

const SNAPSHOT_FORMAT = "%18d %24d %24s %28s \n"

func count(args []string, humanReadable bool) {
if len(args) == 0 {
fatalWithUsage()
}

expanded, client, err := getClientAndExpandedPaths(args)
if err != nil {
fatal(err)
}

for _, p := range expanded {
_, err := client.Stat(p)
if err != nil {
fmt.Fprintln(os.Stderr, err)
status = 1
continue
}

cs, err := client.GetContentSummary(p)
if err != nil {
fmt.Fprintln(os.Stderr, err)
status = 1
continue
}

contentSize := formatBytesHuman(uint64(cs.Size()), humanReadable)
fmt.Printf(SNAPSHOT_FORMAT, cs.DirectoryCount(), cs.FileCount(), contentSize, p)
}
}
8 changes: 8 additions & 0 deletions cmd/hdfs/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ The flags available are a subset of the POSIX ones, but should behave similarly.

Valid commands:
ls [-lah] [FILE]...
count [-h] [FILE]...
rm [-rf] FILE...
mv [-nT] SOURCE... DEST
mkdir [-p] FILE...
Expand Down Expand Up @@ -66,6 +67,9 @@ Valid commands:
chownOpts = getopt.New()
chownR = chownOpts.Bool('R')

countOpts = getopt.New()
counth = countOpts.Bool('h')

headTailOpts = getopt.New()
headtailn = headTailOpts.Int64('n', -1)
headtailc = headTailOpts.Int64('c', -1)
Expand All @@ -91,6 +95,7 @@ func init() {
touchOpts.SetUsage(printHelp)
chmodOpts.SetUsage(printHelp)
chownOpts.SetUsage(printHelp)
countOpts.SetUsage(printHelp)
headTailOpts.SetUsage(printHelp)
duOpts.SetUsage(printHelp)
getmergeOpts.SetUsage(printHelp)
Expand Down Expand Up @@ -138,6 +143,9 @@ func main() {
du(duOpts.Args(), *dus, *duh)
case "checksum":
checksum(argv[1:])
case "count":
countOpts.Parse(argv)
count(countOpts.Args(), *counth)
case "get":
get(argv[1:])
case "getmerge":
Expand Down
57 changes: 57 additions & 0 deletions cmd/hdfs/test/count.bats
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
#!/usr/bin/env bats

load helper

setup() {
$HDFS mkdir -p /_test_cmd/count/dir1
$HDFS mkdir -p /_test_cmd/count/dir2
$HDFS mkdir -p /_test_cmd/count/dir3
$HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/count/foo.txt
$HDFS put $ROOT_TEST_DIR/testdata/foo.txt /_test_cmd/count/dir1/foo1.txt
}

@test "count" {
run $HDFS count /_test_cmd/count/foo.txt
assert_success
assert_output <<OUT
0 1 4 /_test_cmd/count/foo.txt
OUT
}

@test "count human readable" {
run $HDFS count -h /_test_cmd/count/foo.txt
assert_success
assert_output <<OUT
0 1 4B /_test_cmd/count/foo.txt
OUT
}

@test "count dir" {
run $HDFS count /_test_cmd/count
assert_success
assert_output <<OUT
4 2 8 /_test_cmd/count
OUT
}

@test "count wildcard" {
run $HDFS count /_test_cmd/count/dir*
assert_success
assert_output <<OUT
1 1 4 /_test_cmd/count/dir1
1 0 0 /_test_cmd/count/dir2
1 0 0 /_test_cmd/count/dir3
OUT
}

@test "count nonexistent" {
run $HDFS count /_test_cmd/nonexistent
assert_failure
assert_output <<OUT
stat /_test_cmd/nonexistent: file does not exist
OUT
}

teardown() {
$HDFS rm -r /_test_cmd/count
}
8 changes: 8 additions & 0 deletions cmd/hdfs/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"strconv"
)

func formatBytes(i uint64) string {
Expand All @@ -18,3 +19,10 @@ func formatBytes(i uint64) string {
return fmt.Sprintf("%dB", i)
}
}

func formatBytesHuman(i uint64, humanReadable bool) string {
if humanReadable {
return formatBytes(i)
}
return strconv.FormatUint(i, 10)
}