-
Notifications
You must be signed in to change notification settings - Fork 5
/
util.go
343 lines (301 loc) · 7.19 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
331
332
333
334
335
336
337
338
339
340
341
342
343
package dxfuse
import (
"database/sql"
"fmt"
"log"
"net/http"
"os"
"os/user"
"time"
"github.com/jacobsa/fuse/fuseops"
"github.com/shirou/gopsutil/process"
)
const (
KiB = 1024
MiB = 1024 * KiB
GiB = 1024 * MiB
)
const (
DatabaseFile = "metadata.db"
LogFile = "dxfuse.log"
)
const (
MinHttpClientPoolSize = 30
FileWriteInactivityThresh = 5 * time.Minute
MaxDirSize = 255 * 1000
MaxNumFileHandles = 1000 * 1000
NumRetriesDefault = 10
InitialUploadPartSize = 16 * MiB
MaxUploadPartSize = 700 * MiB
Version = "v1.4.0"
)
const (
InodeInvalid = 0
InodeRoot = fuseops.RootInodeID // This is an OS constant
)
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
fileWriteOnlyMode = 0222
)
const (
// flags for writing files to disk
DIRTY_FILES_ALL = 14 // all modified files
DIRTY_FILES_INACTIVE = 15 // only files there were unmodified recently
)
const (
// Permissions
PERM_VIEW = 1
PERM_UPLOAD = 2
PERM_CONTRIBUTE = 3
PERM_ADMINISTER = 4
)
// 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
}
// 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.Atime = d.Mtime
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_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, ...).
//
// Note: this struct is immutable by convention. The up-to-date data is always on the database,
// not in memory.
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
Uid uint32
Gid uint32
// tags and properties
Tags []string
Properties map[string]string
// is the file modified
dirtyData bool
dirtyMetadata bool
}
func (f File) GetAttrs() (a fuseops.InodeAttributes) {
a.Size = uint64(f.Size)
a.Nlink = 1
a.Atime = f.Mtime
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)
}
// A file that is scheduled for removal
type DeadFile struct {
Kind int // Kind of object this is
Id string // Required to build a download URL
ProjId string // Note: this could be a container
Inode int64
LocalPath string
}
// Information required to upload file data to the platform.
// It also includes updated tags and properties of a data-object.
//
// Not that not only files have attributes, applets and workflows
// have them too.
type DirtyFileInfo struct {
Inode int64
dirtyData bool
dirtyMetadata bool
// will be "" for files created locally, and not uploaded yet
Id string
FileSize int64
Mtime int64
LocalPath string
Tags []string
Properties map[string]string
Name string
Directory string
ProjFolder string
ProjId string
}
// A handle used when operating on a filesystem
// operation. We normally need a transaction and an http client.
type OpHandle struct {
httpClient *http.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])
}
func check(value bool) {
if !value {
log.Panicf("assertion failed")
os.Exit(1)
}
}
func boolToInt(b bool) int {
if b {
return 1
}
return 0
}
func intToBool(x int) bool {
if x > 0 {
return true
}
return false
}
// create a directory for all the dxfuse files in $HOME/.dxfuse
func MakeFSBaseDir() string {
user, err := user.Current()
if err != nil {
log.Printf("error, could not describe the user")
os.Exit(1)
}
dxfuseBaseDir := user.HomeDir + "/.dxfuse"
if _, err := os.Stat(dxfuseBaseDir); os.IsNotExist(err) {
os.Mkdir(dxfuseBaseDir, 0700)
}
return dxfuseBaseDir
}
func GetTgid(pid uint32) (tgid int32, err error) {
p, err := process.NewProcess(int32(pid))
if err != nil {
return -1, err
}
tgid, err = p.Tgid()
if err != nil {
return -1, err
}
return tgid, nil
}