|
| 1 | +//go:build !windows |
| 2 | + |
| 3 | +package scanner |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + "os" |
| 8 | + |
| 9 | + "golang.org/x/sys/unix" |
| 10 | +) |
| 11 | + |
| 12 | +func processDir(ctx context.Context, node *FileNode, dirPath string, st *scanState, pathBuf []byte) { |
| 13 | + defer st.wg.Done() |
| 14 | + |
| 15 | + select { |
| 16 | + case <-ctx.Done(): |
| 17 | + return |
| 18 | + default: |
| 19 | + } |
| 20 | + |
| 21 | + if skipDirs[dirPath] { |
| 22 | + return |
| 23 | + } |
| 24 | + |
| 25 | + entries, err := os.ReadDir(dirPath) |
| 26 | + if err != nil { |
| 27 | + node.Err = err |
| 28 | + return |
| 29 | + } |
| 30 | + |
| 31 | + st.dirsScanned.Add(1) |
| 32 | + st.currentPath.Store(dirPath) |
| 33 | + |
| 34 | + // Open directory fd for Fstatat — avoids kernel re-resolving the full path per file. |
| 35 | + dirfd, err := unix.Open(dirPath, unix.O_RDONLY|unix.O_DIRECTORY|unix.O_CLOEXEC, 0) |
| 36 | + if err != nil { |
| 37 | + // Fall back to entry.Info() if we can't open the dirfd. |
| 38 | + processDirFallback(ctx, node, dirPath, entries, st, pathBuf) |
| 39 | + return |
| 40 | + } |
| 41 | + defer unix.Close(dirfd) |
| 42 | + |
| 43 | + // Pre-allocate children slice to avoid repeated growth. |
| 44 | + if cap(node.Children) == 0 { |
| 45 | + node.Children = make([]*FileNode, 0, len(entries)) |
| 46 | + } |
| 47 | + |
| 48 | + for _, entry := range entries { |
| 49 | + select { |
| 50 | + case <-ctx.Done(): |
| 51 | + return |
| 52 | + default: |
| 53 | + } |
| 54 | + |
| 55 | + if entry.Type()&os.ModeSymlink != 0 { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + name := entry.Name() |
| 60 | + |
| 61 | + // Build child path using reusable buffer to avoid filepath.Join allocation. |
| 62 | + pathBuf = appendPath(pathBuf[:0], dirPath, name) |
| 63 | + childPath := string(pathBuf) |
| 64 | + |
| 65 | + child := &FileNode{ |
| 66 | + Name: name, |
| 67 | + IsDir: entry.IsDir(), |
| 68 | + Parent: node, |
| 69 | + } |
| 70 | + |
| 71 | + if entry.IsDir() { |
| 72 | + node.addChild(child) |
| 73 | + st.wg.Add(1) |
| 74 | + select { |
| 75 | + case st.workCh <- work{node: child, path: childPath}: |
| 76 | + default: |
| 77 | + processDir(ctx, child, childPath, st, pathBuf) |
| 78 | + } |
| 79 | + } else { |
| 80 | + // Use Fstatat with the directory fd — single syscall, no path re-resolution. |
| 81 | + var stat unix.Stat_t |
| 82 | + if err := unix.Fstatat(dirfd, name, &stat, unix.AT_SYMLINK_NOFOLLOW); err == nil { |
| 83 | + child.Size = stat.Blocks * 512 |
| 84 | + } else { |
| 85 | + child.Err = err |
| 86 | + } |
| 87 | + st.filesFound.Add(1) |
| 88 | + st.bytesFound.Add(child.Size) |
| 89 | + node.addChild(child) |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +// processDirFallback handles the case where we can't open a dirfd (e.g. permission issues). |
| 95 | +func processDirFallback(ctx context.Context, node *FileNode, dirPath string, entries []os.DirEntry, st *scanState, pathBuf []byte) { |
| 96 | + if cap(node.Children) == 0 { |
| 97 | + node.Children = make([]*FileNode, 0, len(entries)) |
| 98 | + } |
| 99 | + |
| 100 | + for _, entry := range entries { |
| 101 | + select { |
| 102 | + case <-ctx.Done(): |
| 103 | + return |
| 104 | + default: |
| 105 | + } |
| 106 | + |
| 107 | + if entry.Type()&os.ModeSymlink != 0 { |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + name := entry.Name() |
| 112 | + pathBuf = appendPath(pathBuf[:0], dirPath, name) |
| 113 | + childPath := string(pathBuf) |
| 114 | + |
| 115 | + child := &FileNode{ |
| 116 | + Name: name, |
| 117 | + IsDir: entry.IsDir(), |
| 118 | + Parent: node, |
| 119 | + } |
| 120 | + |
| 121 | + if entry.IsDir() { |
| 122 | + node.addChild(child) |
| 123 | + st.wg.Add(1) |
| 124 | + select { |
| 125 | + case st.workCh <- work{node: child, path: childPath}: |
| 126 | + default: |
| 127 | + processDir(ctx, child, childPath, st, pathBuf) |
| 128 | + } |
| 129 | + } else { |
| 130 | + if info, err := entry.Info(); err == nil { |
| 131 | + child.Size = fileSize(info) |
| 132 | + } else { |
| 133 | + child.Err = err |
| 134 | + } |
| 135 | + st.filesFound.Add(1) |
| 136 | + st.bytesFound.Add(child.Size) |
| 137 | + node.addChild(child) |
| 138 | + } |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +// appendPath builds dirPath + "/" + name into buf without allocating. |
| 143 | +func appendPath(buf []byte, dir, name string) []byte { |
| 144 | + buf = append(buf, dir...) |
| 145 | + buf = append(buf, '/') |
| 146 | + buf = append(buf, name...) |
| 147 | + return buf |
| 148 | +} |
0 commit comments