Debian Codegraph @

trash/trash.go

raw
1 package trash
2
3 import (
4 "io/ioutil"
5 "os"
6 "path/filepath"
7 "strconv"
8 "time"
9 )
10
11 // Dir represents a trash directory.
12 type Dir struct {
13 path string
14 }
15
16 // New creates a new Trash directory.
17 func New(root string) (*Dir, error) {
18 // TODO: We need to check permissions and stuff on creation of a
19 // new trash. This means we need to check for a sticky bit and a
20 // bunch of other stuff.
21 panic("TODO")
22 }
23
24 func (d *Dir) file2path(s string) string {
25 return filepath.Join(d.path, "files", s)
26 }
27
28 func (d *Dir) info2path(s string) string {
29 return filepath.Join(d.path, "info", s+".trashinfo")
30 }
31
32 // Files returns a slice of the files in the trash.
33 func (d *Dir) Files() ([]string, error) {
34 dir, err := os.Open(filepath.Join(d.path, "files"))
35 if err != nil {
36 return nil, err
37 }
38 defer dir.Close()
39 return dir.Readdirnames(0)
40 }
41
42 // Stat returns the Info for the given file in the trash.
43 func (d *Dir) Stat(s string) (*Info, error) {
44 f, err := os.Open(d.info2path(s))
45 if err != nil {
46 return nil, err
47 }
48 defer f.Close()
49 return NewInfo(f)
50 }
51
52 // Trash moves the file at the given path to the trash.
53 func (d *Dir) Trash(p string) error {
54 // Find an open file name.
55 tname := filepath.Base(p)
56 for i := 2; d.exists(tname); i++ {
57 tname = filepath.Base(p) + "." + strconv.Itoa(i)
58 }
59
60 // First, write the trashinfo file.
61 abs, err := filepath.Abs(p)
62 if err != nil {
63 return err
64 }
65 info := &Info{abs, time.Now()}
66 err = ioutil.WriteFile(d.info2path(tname), []byte(info.String()), os.ModePerm)
67 if err != nil {
68 return err
69 }
70
71 // Next, move the file to the trash.
72 return os.Rename(p, d.file2path(tname))
73 }
74
75 // Restore moves the file from the trash to its original location.
76 func (d *Dir) Restore(s string) error {
77 info, err := d.Stat(s)
78 if err != nil {
79 return err
80 }
81
82 return d.RestoreTo(s, info.Path)
83 }
84
85 // RestoreTo moves the file from the trash to the specified location.
86 func (d *Dir) RestoreTo(s, p string) error {
87 _, err := os.Stat(p)
88 if err == nil {
89 return os.ErrExist
90 }
91
92 return os.Rename(d.file2path(s), p)
93 }
94
95 func (d *Dir) exists(s string) bool {
96 _, err := os.Stat(d.file2path(s))
97 if os.IsNotExist(err) {
98 return false
99 }
100 return true
101 }
102
103 // Erase removes the given file from the trash.
104 func (d *Dir) Erase(s string) error {
105 err := os.Remove(d.file2path(s))
106 if err != nil {
107 return err
108 }
109 err = os.Remove(d.info2path(s))
110 if err != nil {
111 return err
112 }
113 return nil
114 }
115
116 // EraseAll removes the given file and any children it contains.
117 func (d *Dir) EraseAll(s string) error {
118 err := os.RemoveAll(d.file2path(s))
119 if err != nil {
120 return err
121 }
122 err = os.RemoveAll(d.info2path(s))
123 if err != nil {
124 return nil
125 }
126 return nil
127 }
128
129 // Empty erases all the files in the trash.
130 func (d *Dir) Empty() error {
131 files, err := d.Files()
132 if err != nil {
133 return err
134 }
135 for _, i := range files {
136 err = d.EraseAll(i)
137 if err != nil {
138 return err
139 }
140 }
141 return nil
142 }
143
144 // IsEmpty returns whether or not the trash is empty.
145 func (d *Dir) IsEmpty() bool {
146 files, _ := d.Files()
147 return len(files) == 0
148 }