-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypeconv.go
More file actions
52 lines (44 loc) · 1.11 KB
/
typeconv.go
File metadata and controls
52 lines (44 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package fs
import (
"errors"
"path/filepath"
"strconv"
"github.com/transientvariable/cadre"
)
// FileMetadata converts a file system entry and produces a cadre.File.
func FileMetadata(fsys FS, entry *Entry) (*cadre.File, error) {
if fsys == nil {
return nil, errors.New("fs: file system is required")
}
if entry == nil {
return nil, errors.New("fs: entry is required")
}
r, err := fsys.Root()
if err != nil {
return nil, err
}
mtime := entry.ModTime()
m := &cadre.File{
Ctime: &mtime,
GID: itoa(int(entry.Attributes().GID())),
Directory: filepath.Join(fsys.PathSeparator(), r, filepath.Dir(entry.Path())),
Inode: itoa(int(entry.Attributes().Inode())),
Mode: itoa(int(entry.Mode())),
Mtime: &mtime,
Name: entry.Name(),
Owner: entry.Attributes().Owner(),
Path: filepath.Join(fsys.PathSeparator(), r, entry.Path()),
UID: itoa(int(entry.Attributes().UID())),
}
if !entry.IsDir() {
m.MimeType = entry.Attributes().MimeType()
m.Size = entry.Size()
}
return m, nil
}
func itoa(v int) string {
if v > 0 {
return strconv.Itoa(v)
}
return ""
}