-
Notifications
You must be signed in to change notification settings - Fork 7
/
write.go
111 lines (97 loc) · 2.8 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package filesystem
import (
"errors"
"os"
"syscall"
"time"
"github.com/Cloud-Foundations/Dominator/lib/fsutil"
)
var modePerm FileMode = syscall.S_IRWXU | syscall.S_IRWXG | syscall.S_IRWXO
func forceWriteMetadata(inode GenericInode, name string) error {
err := inode.WriteMetadata(name)
if err == nil {
return nil
}
if os.IsPermission(err) {
// Blindly attempt to remove immutable attributes.
fsutil.MakeMutable(name)
}
return inode.WriteMetadata(name)
}
func (inode *DirectoryInode) write(name string) error {
if err := inode.make(name); err != nil {
// If existing directory, don't blow it away, just update metadata.
if os.IsExist(err) {
if fi, err := os.Lstat(name); err == nil && fi.IsDir() {
return inode.writeMetadata(name)
}
}
fsutil.ForceRemoveAll(name)
if err := inode.make(name); err != nil {
return err
}
}
return inode.writeMetadata(name)
}
func (inode *DirectoryInode) make(name string) error {
return syscall.Mkdir(name, uint32(inode.Mode))
}
func (inode *DirectoryInode) writeMetadata(name string) error {
if err := os.Lchown(name, int(inode.Uid), int(inode.Gid)); err != nil {
return err
}
return syscall.Chmod(name, uint32(inode.Mode))
}
func (inode *RegularInode) writeMetadata(name string) error {
if err := os.Lchown(name, int(inode.Uid), int(inode.Gid)); err != nil {
return err
}
if err := syscall.Chmod(name, uint32(inode.Mode)); err != nil {
return err
}
t := time.Unix(inode.MtimeSeconds, int64(inode.MtimeNanoSeconds))
return os.Chtimes(name, t, t)
}
func (inode *SymlinkInode) write(name string) error {
if inode.make(name) != nil {
fsutil.ForceRemoveAll(name)
if err := inode.make(name); err != nil {
return err
}
}
return inode.writeMetadata(name)
}
func (inode *SymlinkInode) make(name string) error {
return os.Symlink(inode.Symlink, name)
}
func (inode *SymlinkInode) writeMetadata(name string) error {
return os.Lchown(name, int(inode.Uid), int(inode.Gid))
}
func (inode *SpecialInode) write(name string) error {
if inode.make(name) != nil {
fsutil.ForceRemoveAll(name)
if err := inode.make(name); err != nil {
return err
}
}
return inode.writeMetadata(name)
}
func (inode *SpecialInode) make(name string) error {
if inode.Mode&syscall.S_IFBLK != 0 || inode.Mode&syscall.S_IFCHR != 0 {
return syscall.Mknod(name, uint32(inode.Mode), int(inode.Rdev))
} else if inode.Mode&syscall.S_IFIFO != 0 {
return syscall.Mkfifo(name, uint32(inode.Mode))
} else {
return errors.New("unsupported mode")
}
}
func (inode *SpecialInode) writeMetadata(name string) error {
if err := os.Lchown(name, int(inode.Uid), int(inode.Gid)); err != nil {
return err
}
if err := syscall.Chmod(name, uint32(inode.Mode)); err != nil {
return err
}
t := time.Unix(inode.MtimeSeconds, int64(inode.MtimeNanoSeconds))
return os.Chtimes(name, t, t)
}