This repository has been archived by the owner on Jan 14, 2024. It is now read-only.
forked from fyne-io/fyne
-
Notifications
You must be signed in to change notification settings - Fork 0
/
file.go
298 lines (243 loc) · 6.7 KB
/
file.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package repository
import (
"io"
"os"
"path"
"path/filepath"
"strings"
"github.com/Anwert/fyne/v2"
"github.com/Anwert/fyne/v2/storage"
"github.com/Anwert/fyne/v2/storage/repository"
)
// fileSchemePrefix is used for when we need a hard-coded version of "file://"
// for string processing
const fileSchemePrefix string = "file://"
// declare conformance with repository types
var _ repository.Repository = (*FileRepository)(nil)
var _ repository.WritableRepository = (*FileRepository)(nil)
var _ repository.HierarchicalRepository = (*FileRepository)(nil)
var _ repository.ListableRepository = (*FileRepository)(nil)
var _ repository.MovableRepository = (*FileRepository)(nil)
var _ repository.CopyableRepository = (*FileRepository)(nil)
var _ fyne.URIReadCloser = (*file)(nil)
var _ fyne.URIWriteCloser = (*file)(nil)
type file struct {
*os.File
uri fyne.URI
}
func (f *file) URI() fyne.URI {
return f.uri
}
// FileRepository implements a simple wrapper around golang's filesystem
// interface libraries. It should be registered by the driver on platforms
// where it is appropriate to do so.
//
// This repository is suitable to handle the file:// scheme.
//
// Since: 2.0
type FileRepository struct {
}
// NewFileRepository creates a new FileRepository instance.
// The caller needs to call repository.Register() with the result of this function.
//
// Since: 2.0
func NewFileRepository() *FileRepository {
return &FileRepository{}
}
// Exists implements repository.Repository.Exists
//
// Since: 2.0
func (r *FileRepository) Exists(u fyne.URI) (bool, error) {
p := u.Path()
_, err := os.Stat(p)
ok := false
if err == nil {
ok = true
} else if os.IsNotExist(err) {
err = nil
}
return ok, err
}
func openFile(uri fyne.URI, create bool) (*file, error) {
path := uri.Path()
var f *os.File
var err error
if create {
f, err = os.Create(path) // If it exists this will truncate which is what we wanted
} else {
f, err = os.Open(path)
}
return &file{File: f, uri: uri}, err
}
// Reader implements repository.Repository.Reader
//
// Since: 2.0
func (r *FileRepository) Reader(u fyne.URI) (fyne.URIReadCloser, error) {
return openFile(u, false)
}
// CanRead implements repository.Repository.CanRead
//
// Since: 2.0
func (r *FileRepository) CanRead(u fyne.URI) (bool, error) {
f, err := os.OpenFile(u.Path(), os.O_RDONLY, 0666)
if err == nil {
f.Close()
} else {
if os.IsPermission(err) {
return false, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
return true, nil
}
// Destroy implements repository.Repository.Destroy
func (r *FileRepository) Destroy(scheme string) {
// do nothing
}
// Writer implements repository.WritableRepository.Writer
//
// Since: 2.0
func (r *FileRepository) Writer(u fyne.URI) (fyne.URIWriteCloser, error) {
return openFile(u, true)
}
// CanWrite implements repository.WritableRepository.CanWrite
//
// Since: 2.0
func (r *FileRepository) CanWrite(u fyne.URI) (bool, error) {
f, err := os.OpenFile(u.Path(), os.O_WRONLY, 0666)
if err == nil {
f.Close()
} else {
if os.IsPermission(err) {
return false, nil
}
if os.IsNotExist(err) {
// We may need to do extra logic to check if the
// directory is writable, but presumably the
// IsPermission check covers this.
return true, nil
}
return false, err
}
return true, nil
}
// Delete implements repository.WritableRepository.Delete
//
// Since: 2.0
func (r *FileRepository) Delete(u fyne.URI) error {
return os.Remove(u.Path())
}
// Parent implements repository.HierarchicalRepository.Parent
//
// Since: 2.0
func (r *FileRepository) Parent(u fyne.URI) (fyne.URI, error) {
s := u.String()
// trim trailing slash
s = strings.TrimSuffix(s, "/")
// trim the scheme
s = strings.TrimPrefix(s, fileSchemePrefix)
// Completely empty URI with just a scheme
if s == "" {
return nil, repository.ErrURIRoot
}
parent := ""
// use the system native path resolution
parent = filepath.Dir(s)
if parent[len(parent)-1] != filepath.Separator {
parent += "/"
}
// only root is it's own parent
if filepath.Clean(parent) == filepath.Clean(s) {
return nil, repository.ErrURIRoot
}
return storage.NewFileURI(parent), nil
}
// Child implements repository.HierarchicalRepository.Child
//
// Since: 2.0
func (r *FileRepository) Child(u fyne.URI, component string) (fyne.URI, error) {
newURI := u.Scheme() + "://" + u.Authority()
newURI += path.Join(u.Path(), component)
// stick the query and fragment back on the end
if query := u.Query(); len(query) > 0 {
newURI += "?" + query
}
if fragment := u.Fragment(); len(fragment) > 0 {
newURI += "#" + fragment
}
return storage.ParseURI(newURI)
}
// List implements repository.ListableRepository.List()
//
// Since: 2.0
func (r *FileRepository) List(u fyne.URI) ([]fyne.URI, error) {
path := u.Path()
files, err := os.ReadDir(path)
if err != nil {
return nil, err
}
urilist := []fyne.URI{}
for _, f := range files {
uri := storage.NewFileURI(filepath.Join(path, f.Name()))
urilist = append(urilist, uri)
}
return urilist, nil
}
// CreateListable implements repository.ListableRepository.CreateListable.
func (r *FileRepository) CreateListable(u fyne.URI) error {
path := u.Path()
err := os.Mkdir(path, 0755)
return err
}
// CanList implements repository.ListableRepository.CanList()
//
// Since: 2.0
func (r *FileRepository) CanList(u fyne.URI) (bool, error) {
p := u.Path()
info, err := os.Stat(p)
if err != nil {
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
if !info.IsDir() {
return false, nil
}
// We know it is a directory, but we don't know if we can read it, so
// we'll just try to do so and see if we get a permissions error.
f, err := os.Open(p)
if err == nil {
_, err = f.Readdir(1)
f.Close()
}
if err != nil && err != io.EOF {
return false, err
}
if os.IsPermission(err) {
return false, nil
}
// it is a directory, and checking the permissions did not error out
return true, nil
}
// Copy implements repository.CopyableRepository.Copy()
//
// Since: 2.0
func (r *FileRepository) Copy(source, destination fyne.URI) error {
// NOTE: as far as I can tell, golang does not have an optimized Copy
// function - everything I can find on the 'net suggests doing more
// or less the equivalent of GenericCopy(), hence why that is used.
return repository.GenericCopy(source, destination)
}
// Move implements repository.MovableRepository.Move()
//
// Since: 2.0
func (r *FileRepository) Move(source, destination fyne.URI) error {
// NOTE: as far as I can tell, golang does not have an optimized Move
// function - everything I can find on the 'net suggests doing more
// or less the equivalent of GenericMove(), hence why that is used.
return repository.GenericMove(source, destination)
}