]> Sergey Matveev's repositories - glocate.git/blob - walker.go
Newer ZFS has different octalize rule
[glocate.git] / walker.go
1 /*
2 glocate -- ZFS-diff-friendly locate-like utility
3 Copyright (C) 2022-2023 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "io/fs"
22         "log"
23         "os"
24         "path"
25 )
26
27 var (
28         WalkerFiles int64
29         WalkerDirs  int64
30 )
31
32 func walker(sink chan Ent, root []string) error {
33         files, err := os.ReadDir(path.Join(root...)) // it is already sorted
34         if err != nil {
35                 return err
36         }
37         var info fs.FileInfo
38         ents := make([]Ent, 0, len(files))
39         for _, file := range files {
40                 ent := Ent{name: append([]string{}, append(root, file.Name())...)}
41                 info, err = file.Info()
42                 if err == nil {
43                         if info.IsDir() {
44                                 ent.name[len(ent.name)-1] += "/"
45                         } else if info.Mode().IsRegular() {
46                                 ent.size = info.Size()
47                         }
48                         ent.mtime = info.ModTime().Unix()
49                 } else {
50                         log.Println("can not stat:", path.Join(ent.name...), ":", err)
51                 }
52                 ents = append(ents, ent)
53         }
54         for _, ent := range ents {
55                 sink <- ent
56                 if ent.IsDir() {
57                         WalkerDirs++
58                 } else {
59                         WalkerFiles++
60                         continue
61                 }
62                 err = walker(sink, ent.name)
63                 if err != nil {
64                         log.Println("can not stat:", path.Join(ent.name...), ":", err)
65                         continue
66                 }
67         }
68         return nil
69 }