-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkdircmd.go
98 lines (81 loc) · 2.57 KB
/
mkdircmd.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
package commands
import (
"encoding/json"
"errors"
"fmt"
"github.com/cpssd/paranoid/libpfs/returncodes"
"os"
"path"
"syscall"
)
// MkdirCommand is called when making a paranoidDirectory
func MkdirCommand(paranoidDirectory, dirPath string, mode os.FileMode) (returnCode returncodes.Code, returnError error) {
Log.Info("mkdir command called")
err := GetFileSystemLock(paranoidDirectory, ExclusiveLock)
if err != nil {
return returncodes.EUNEXPECTED, err
}
defer func() {
err := UnLockFileSystem(paranoidDirectory)
if err != nil {
returnCode = returncodes.EUNEXPECTED
returnError = err
}
}()
dirParanoidPath := getParanoidPath(paranoidDirectory, dirPath)
dirInfoPath := path.Join(dirParanoidPath, "info")
inodeBytes, err := generateNewInode()
if err != nil {
return returncodes.EUNEXPECTED, err
}
inodeString := string(inodeBytes)
inodePath := path.Join(paranoidDirectory, "inodes", inodeString)
contentsPath := path.Join(paranoidDirectory, "contents", inodeString)
fileType, err := getFileType(paranoidDirectory, dirParanoidPath)
if err != nil {
return returncodes.EUNEXPECTED, err
}
if fileType != typeENOENT {
return returncodes.EEXIST, errors.New(dirPath + " already exists")
}
err = os.Mkdir(dirParanoidPath, mode)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error making paranoidDirectory %s: %s", dirParanoidPath, err)
}
contentsFile, err := os.Create(contentsPath)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error creating contents file: %s", err)
}
defer contentsFile.Close()
err = contentsFile.Chmod(os.FileMode(mode))
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error changing file permissions: %s", err)
}
dirInfoFile, err := os.Create(dirInfoPath)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error creating info file: %s", err)
}
defer dirInfoFile.Close()
_, err = dirInfoFile.Write(inodeBytes)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error writing to info file: %s", err)
}
inodeFile, err := os.Create(inodePath)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error creating inode file: %s", err)
}
defer inodeFile.Close()
nodeData := &inode{
Mode: mode | syscall.S_IFDIR,
Inode: inodeString,
Count: 1}
jsonData, err := json.Marshal(nodeData)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error marshalling json: %s", err)
}
_, err = inodeFile.Write(jsonData)
if err != nil {
return returncodes.EUNEXPECTED, fmt.Errorf("error writing to inode file: %s", err)
}
return returncodes.OK, nil
}