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