]> Sergey Matveev's repositories - sgodup.git/blob - walk.go
Raise copyright years
[sgodup.git] / walk.go
1 /*
2 sgodup -- File deduplication utility
3 Copyright (C) 2020-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"
22         "log"
23         "os"
24         "path"
25         "syscall"
26 )
27
28 type FileInode struct {
29         Path string
30         Size int64
31         Dev  uint64
32         Ino  uint64
33 }
34
35 func walker(c chan FileInode, dirPath string) {
36         dirFd, err := os.Open(dirPath)
37         if err != nil {
38                 log.Fatal(err)
39         }
40         for {
41                 fis, err := dirFd.Readdir(1 << 10)
42                 if err != nil {
43                         if err == io.EOF {
44                                 break
45                         }
46                         log.Fatal(err)
47                 }
48                 for _, fi := range fis {
49                         stat := fi.Sys().(*syscall.Stat_t)
50                         if fi.Mode().IsRegular() {
51                                 c <- FileInode{
52                                         path.Join(dirPath, fi.Name()),
53                                         fi.Size(),
54                                         stat.Dev, stat.Ino,
55                                 }
56                         } else if fi.IsDir() {
57                                 walker(c, path.Join(dirPath, fi.Name()))
58                         }
59                 }
60         }
61         if err = dirFd.Close(); err != nil {
62                 log.Fatal(err)
63         }
64 }
65
66 func walk(dirPath string) chan FileInode {
67         c := make(chan FileInode, 1<<10)
68         go func() {
69                 walker(c, dirPath)
70                 close(c)
71         }()
72         return c
73 }