forked from rfjakob/cshatag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
write.go
57 lines (52 loc) · 1.49 KB
/
write.go
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
53
54
55
56
57
package main
// This file has all functions that actually change something on disk
import (
"errors"
"os"
"runtime"
"github.com/pkg/xattr"
)
// storeAttr stores "attr" into extended attributes.
// Should look like this afterwards:
//
// $ getfattr -d foo.txt
// user.shatag.sha256="dc9fe2260fd6748b29532be0ca2750a50f9eca82046b15497f127eba6dda90e8"
// user.shatag.ts="1560177334.020775051"
func storeAttr(f *os.File, attr fileAttr) (err error) {
if args.dryrun {
return nil
}
if runtime.GOOS == "darwin" {
// SMB or MacOS bug: when working on an SMB mounted filesystem on a Mac, it seems the call
// to `fsetxattr` does not update the xattr but removes it instead. So it takes two runs
// of `cshatag` to update the attribute.
// To work around this issue, we remove the xattr explicitly before setting it again.
// https://github.com/rfjakob/cshatag/issues/8
removeAttr(f)
}
err = xattr.FSet(f, xattrTs, []byte(attr.ts.prettyPrint()))
if err != nil {
return
}
err = xattr.FSet(f, xattrSha256, attr.sha256)
return
}
// removeAttr removes any previously stored extended attributes. Returns an error
// if removal of either the timestamp or checksum xattrs fails.
func removeAttr(f *os.File) error {
if args.dryrun {
return nil
}
err1 := xattr.FRemove(f, xattrTs)
err2 := xattr.FRemove(f, xattrSha256)
if err1 != nil && err2 != nil {
return errors.New(err1.Error() + " " + err2.Error())
}
if err1 != nil {
return err1
}
if err2 != nil {
return err2
}
return nil
}