forked from dnanexus/dxfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.go
330 lines (271 loc) · 7.14 KB
/
util.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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package dxfuse
import (
"database/sql"
"fmt"
"log"
"os"
"sync"
"time"
"github.com/dnanexus/dxda"
"github.com/hashicorp/go-retryablehttp"
"github.com/jacobsa/fuse/fuseutil"
"github.com/jacobsa/fuse/fuseops"
)
const (
CreatedFilesDir = "/var/dxfuse/created_files"
DatabaseFile = "/var/dxfuse/metadata.db"
HttpClientPoolSize = 4
LogFile = "/var/log/dxfuse.log"
MaxDirSize = 10 * 1000
MaxNumFileHandles = 1000 * 1000
NumRetriesDefault = 3
Version = "v0.19"
)
const (
InodeInvalid = 0
InodeRoot = fuseops.RootInodeID // This is an OS constant
)
const (
KiB = 1024
MiB = 1024 * KiB
GiB = 1024 * MiB
)
const (
// It turns out that in order for regular users to be able to create file,
// we need to use 777 permissions for directories.
dirReadOnlyMode = 0555 | os.ModeDir
dirReadWriteMode = 0777 | os.ModeDir
fileReadOnlyMode = 0444
fileReadWriteMode = 0644
)
// A URL generated with the /file-xxxx/download API call, that is
// used to download file ranges.
type DxDownloadURL struct {
URL string `json:"url"`
Headers map[string]string `json:"headers"`
}
type Options struct {
ReadOnly bool
Verbose bool
VerboseLevel int
Uid uint32
Gid uint32
}
type Filesys struct {
// inherit empty implementations for all the filesystem
// methods we do not implement
fuseutil.NotImplementedFileSystem
// configuration information for accessing dnanexus servers
dxEnv dxda.DXEnvironment
// various options
options Options
// A file holding a sqlite3 database with all the files and
// directories collected thus far.
dbFullPath string
// Lock for protecting shared access to the database
mutex sync.Mutex
// a pool of http clients, for short requests, such as file creation,
// or file describe.
httpClientPool chan(*retryablehttp.Client)
// metadata database
mdb *MetadataDb
// prefetch state for all files
pgs *PrefetchGlobalState
// background upload state
fugs *FileUploadGlobalState
// API to dx
ops *DxOps
// all open files
fhCounter uint64
fhTable map[fuseops.HandleID]*FileHandle
// all open directories
dhCounter uint64
dhTable map[fuseops.HandleID]*DirHandle
nonce *Nonce
tmpFileCounter uint64
// is the the system shutting down (unmounting)
shutdownCalled bool
}
// A node is a generalization over files and directories
type Node interface {
GetInode() fuseops.InodeID
GetAttrs() fuseops.InodeAttributes
}
// directories
type Dir struct {
Parent string // the parent directory, used for debugging
Dname string // This is the last part of the full path
FullPath string // combine parent and dname, then normalize
Inode int64
Ctime time.Time // DNAx does not record times per directory.
Mtime time.Time // we use the project creation time, and mtime as an approximation.
Mode os.FileMode // uint32
Uid uint32
Gid uint32
// extra information, used internally
ProjId string
ProjFolder string
Populated bool
// is this a faux dir?
faux bool
}
func (d Dir) GetAttrs() (a fuseops.InodeAttributes) {
a.Size = 4096
a.Nlink = 1
a.Mtime = d.Mtime
a.Ctime = d.Ctime
a.Mode = os.ModeDir | d.Mode
a.Crtime = d.Ctime
a.Uid = d.Uid
a.Gid = d.Gid
return
}
func (d Dir) GetInode() fuseops.InodeID {
return fuseops.InodeID(d.Inode)
}
// Kinds of files
const (
FK_Regular = 10
FK_Symlink = 11
FK_Applet = 12
FK_Workflow = 13
FK_Record = 14
FK_Database = 15
FK_Other = 16
)
// A Unix file can stand for any DNAx data object. For example, it could be a workflow or an applet.
// We distinguish between them based on the Id (file-xxxx, applet-xxxx, workflow-xxxx, ...).
type File struct {
Kind int // Kind of object this is
Id string // Required to build a download URL
ProjId string // Note: this could be a container
// One of {open, closing, closed}.
// Closed is the only state where the file can be read
State string
// One of {live, archival, archived, unarchiving}.
// Live is the only state where the file can be read.
ArchivalState string
Name string
Size int64
Inode int64
Ctime time.Time
Mtime time.Time
Mode os.FileMode // uint32
Nlink int
Uid uint32
Gid uint32
// tags and properties
Tags []string
Properties map[string]string
// for a symlink, it holds the path.
// For a regular file, a path to a local copy (if any).
InlineData string
}
func (f File) GetAttrs() (a fuseops.InodeAttributes) {
a.Size = uint64(f.Size)
a.Nlink = uint32(f.Nlink)
a.Mtime = f.Mtime
a.Ctime = f.Ctime
a.Mode = f.Mode
a.Crtime = f.Ctime
a.Uid = f.Uid
a.Gid = f.Gid
return
}
func (f File) GetInode() fuseops.InodeID {
return fuseops.InodeID(f.Inode)
}
// Files can be opened in read-only mode, or read-write mode.
const (
RO_Remote = 1 // read only file that is on the cloud
RW_File = 2 // newly created file
RO_LocalCopy = 3 // read only file that has a local copy
)
type FileHandle struct {
fKind int
f File
hid fuseops.HandleID
// URL used for downloading file ranges.
// Used for read-only files.
url *DxDownloadURL
// Local file copy, may be empty.
localPath *string
// 1. Used for reading from an immutable local copy
// 2. Used for writing to newly created files.
fd *os.File
}
type DirHandle struct {
d Dir
entries []fuseutil.Dirent
}
// A handle used when operating on a filesystem
// operation. We normally need a transaction and an http client.
type OpHandle struct {
httpClient *retryablehttp.Client
txn *sql.Tx
err error
}
func (oph *OpHandle) RecordError(err error) error {
oph.err = err
return err
}
// Utility functions
func MaxInt64(x, y int64) int64 {
if x < y {
return y
}
return x
}
func MinInt64(x, y int64) int64 {
if x > y {
return y
}
return x
}
func MaxInt(x, y int) int {
if x < y {
return y
}
return x
}
func MinInt(x, y int) int {
if x > y {
return y
}
return x
}
// convert time in seconds since 1-Jan 1970, to the equivalent
// golang structure
func SecondsToTime(t int64) time.Time {
return time.Unix(t, 0)
}
func Time2string(t time.Time) string {
return fmt.Sprintf("%02d:%02d:%02d.%03d", t.Hour(), t.Minute(), t.Second(), t.Nanosecond()/1000000)
}
// add a timestamp and module name, to a log message
func LogMsg(moduleName string, a string, args ...interface{}) {
msg := fmt.Sprintf(a, args...)
now := time.Now()
log.Printf("%s %s: %s", Time2string(now), moduleName, msg)
}
//
// 1024 => 1KB
// 10240 => 10KB
// 1100000 => 1MB
func BytesToString(numBytes int64) string {
byteModifier := []string {"B", "KB", "MB", "GB", "TB", "EB", "ZB" }
digits := make([]int, 0)
for numBytes > 0 {
d := int(numBytes % 1024)
digits = append(digits, d)
numBytes = numBytes / 1024
}
// which digit is the most significant?
msd := len(digits)
if msd >= len(byteModifier) {
// This number is so large that we don't have a modifier
// for it.
return fmt.Sprintf("%dB", numBytes)
}
return fmt.Sprintf("%d%s", digits[msd], byteModifier[msd])
}