-
Notifications
You must be signed in to change notification settings - Fork 0
/
chmodcmd.go
87 lines (69 loc) · 2.23 KB
/
chmodcmd.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
package commands
import (
"encoding/json"
"errors"
"fmt"
"github.com/cpssd/paranoid/libpfs/returncodes"
"io/ioutil"
"os"
"path"
"syscall"
)
const PERM_MASK = 0777
//ChmodCommand is used to change the permissions of a file.
func ChmodCommand(paranoidDirectory, filePath string, perms os.FileMode) (returnCode returncodes.Code, returnError error) {
Log.Info("chmod command called")
Log.Verbose("chmod : given paranoidDirectory = " + paranoidDirectory)
err := GetFileSystemLock(paranoidDirectory, ExclusiveLock)
if err != nil {
return returncodes.EUNEXPECTED, err
}
defer func() {
err := UnLockFileSystem(paranoidDirectory)
if err != nil {
returnCode = returncodes.EUNEXPECTED
returnError = err
}
}()
namepath := getParanoidPath(paranoidDirectory, filePath)
fileType, err := getFileType(paranoidDirectory, namepath)
if err != nil {
return returncodes.EUNEXPECTED, err
}
if fileType == typeENOENT {
return returncodes.ENOENT, errors.New(filePath + " does not exist")
}
if fileType == typeSymlink {
return returncodes.EIO, errors.New(filePath + " is of type symlink")
}
inodeNameBytes, code, err := getFileInode(namepath)
if code != returncodes.OK {
return code, err
}
inodeName := string(inodeNameBytes)
code, err = canAccessFile(paranoidDirectory, inodeName, getAccessMode(syscall.O_WRONLY))
if err != nil {
return code, fmt.Errorf("unable to access %s: %s", filePath, err)
}
Log.Verbosef("chmod : changing permissions of "+inodeName+" to", perms)
inodePath := path.Join(paranoidDirectory, "inodes", inodeName)
inodeContents, err := ioutil.ReadFile(inodePath)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error reading inode: %s", err)
}
nodeData := &inode{}
err = json.Unmarshal(inodeContents, &nodeData)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error unmarshaling inode data: %s", err)
}
nodeData.Mode = (nodeData.Mode &^ PERM_MASK) | perms
jsonData, err := json.Marshal(nodeData)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error marshalling json: %s", err)
}
err = ioutil.WriteFile(inodePath, jsonData, 0600)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error writing inodes file: %s", err)
}
return returncodes.OK, nil
}