-
Notifications
You must be signed in to change notification settings - Fork 0
/
rmdircmd.go
90 lines (74 loc) · 2.21 KB
/
rmdircmd.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
package commands
import (
"errors"
"fmt"
"github.com/cpssd/paranoid/libpfs/returncodes"
"io/ioutil"
"os"
"path"
"syscall"
)
// RmdirCommand removes a paranoidDirectory
func RmdirCommand(paranoidDirectory, dirPath string) (returnCode returncodes.Code, returnError error) {
Log.Info("rmdir 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
}
}()
dirToDelete := getParanoidPath(paranoidDirectory, dirPath)
dirType, err := getFileType(paranoidDirectory, dirToDelete)
if err != nil {
return returncodes.EUNEXPECTED, err
}
if dirType == typeENOENT {
return returncodes.ENOENT, errors.New(dirPath + " does not exist")
} else if dirType != typeDir {
return returncodes.ENOTDIR, errors.New(dirPath + " is not a paranoidDirectory")
}
files, err := ioutil.ReadDir(dirToDelete)
if err != nil {
if os.IsPermission(err) {
return returncodes.EACCES, errors.New("could not access " + dirPath)
}
return returncodes.EUNEXPECTED, fmt.Errorf("error reading paranoidDirectory: %s", err)
}
if len(files) > 1 {
return returncodes.ENOTEMPTY, errors.New(dirPath + " is not empty")
}
infoFileToDelete := path.Join(dirToDelete, "info")
inodeBytes, code, err := getFileInode(dirToDelete)
if code != returncodes.OK {
return code, err
}
inodeString := string(inodeBytes)
code, err = canAccessFile(paranoidDirectory, inodeString, getAccessMode(syscall.O_WRONLY))
if err != nil {
return code, fmt.Errorf("unable to access %s: %s", dirPath, err)
}
inodeFileToDelete := path.Join(paranoidDirectory, "inodes", inodeString)
contentsFileToDelete := path.Join(paranoidDirectory, "contents", inodeString)
code, err = deleteFile(contentsFileToDelete)
if code != returncodes.OK {
return code, err
}
code, err = deleteFile(inodeFileToDelete)
if code != returncodes.OK {
return code, err
}
code, err = deleteFile(infoFileToDelete)
if code != returncodes.OK {
return code, err
}
code, err = deleteFile(dirToDelete)
if code != returncodes.OK {
return code, err
}
return returncodes.OK, nil
}