Skip to content

Commit

Permalink
call getdents multiple times
Browse files Browse the repository at this point in the history
Getdents can be called multiple times. Set the default size of the
buffer to 5MB and call it multiple times.
  • Loading branch information
catatsuy committed Sep 18, 2021
1 parent 3559306 commit 683aeef
Showing 1 changed file with 34 additions and 31 deletions.
65 changes: 34 additions & 31 deletions cli/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ const (
ExitCodeOK = 0
ExitCodeParseFlagError = 1
ExitCodeFail = 1

// 5MB
DefaultBufSize = 5 * 1024 * 1024
)

type CLI struct {
Expand Down Expand Up @@ -47,15 +50,15 @@ func (c *CLI) Run(args []string) int {
var (
version bool
debug bool
bufSize int64
bufSize int
)

flags := flag.NewFlagSet("lls", flag.ContinueOnError)
flags.SetOutput(c.errStream)

flags.Int64Var(&bufSize, "buf-size", 0, "specify buf size")
flags.IntVar(&bufSize, "buf-size", DefaultBufSize, "specify buf size")

flags.BoolVar(&version, "version", false, "Print version information and quit")
flags.BoolVar(&version, "version", false, "print version information and quit")
flags.BoolVar(&debug, "debug", false, "debug mode")

err := flags.Parse(args[1:])
Expand Down Expand Up @@ -88,7 +91,7 @@ func (c *CLI) Run(args []string) int {
return c.run(target, debug, bufSize)
}

func (c *CLI) run(target string, debug bool, bufSize int64) int {
func (c *CLI) run(target string, debug bool, bufSize int) int {
f, err := os.Open(target)
if err != nil {
fmt.Fprintln(c.errStream, err)
Expand All @@ -107,41 +110,41 @@ func (c *CLI) run(target string, debug bool, bufSize int64) int {
return ExitCodeFail
}

if bufSize == 0 {
bufSize = finfo.Size()
}

// about actual size: 20 + filename
// ls -dl
buf := make([]byte, bufSize)
n, err := syscall.Getdents(int(f.Fd()), buf)
if err != nil {
fmt.Fprintln(c.errStream, err)
return ExitCodeFail
}

if debug {
fmt.Fprintf(c.errStream, "bufSize: %d; getdents ret: %d\n", bufSize, n)
return ExitCodeOK
}
for {
n, err := syscall.Getdents(int(f.Fd()), buf)
if err != nil {
fmt.Fprintln(c.errStream, err)
return ExitCodeFail
}

for bufp := 0; bufp < n; {
dirent := (*syscall.Dirent)(unsafe.Pointer(&buf[bufp]))
bufp += int(dirent.Reclen)
if n == 0 {
break
}

// deleted file
if dirent.Ino == 0 {
continue
if debug {
fmt.Fprintf(c.errStream, "bufSize: %d; getdents ret: %d\n", bufSize, n)
}

bb := (*[256]byte)(unsafe.Pointer(&dirent.Name[0]))
name := string(bb[0:blen(*bb)])
for bufp := 0; bufp < n; {
dirent := (*syscall.Dirent)(unsafe.Pointer(&buf[bufp]))
bufp += int(dirent.Reclen)

// deleted file
if dirent.Ino == 0 {
continue
}

bb := (*[256]byte)(unsafe.Pointer(&dirent.Name[0]))
name := string(bb[0:blen(*bb)])

if name == "." || name == ".." {
// ignore
continue
if name == "." || name == ".." {
// ignore
continue
}
fmt.Fprintln(c.outStream, name)
}
fmt.Fprintln(c.outStream, name)
}

return ExitCodeOK
Expand Down

0 comments on commit 683aeef

Please sign in to comment.