forked from kahing/goofys
-
Notifications
You must be signed in to change notification settings - Fork 0
/
flags.go
476 lines (399 loc) · 12 KB
/
flags.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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
// Copyright 2015 - 2017 Ka-Hing Cheung
// Copyright 2015 - 2017 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package internal
import (
. "github.com/kahing/goofys/api/common"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"strings"
"text/tabwriter"
"text/template"
"time"
"github.com/urfave/cli"
)
var flagCategories map[string]string
// Set up custom help text for goofys; in particular the usage section.
func filterCategory(flags []cli.Flag, category string) (ret []cli.Flag) {
for _, f := range flags {
if flagCategories[f.GetName()] == category {
ret = append(ret, f)
}
}
return
}
func init() {
cli.AppHelpTemplate = `NAME:
{{.Name}} - {{.Usage}}
USAGE:
{{.Name}} {{if .Flags}}[global options]{{end}} bucket[:prefix] mountpoint
{{if .Version}}
VERSION:
{{.Version}}
{{end}}{{if len .Authors}}
AUTHOR(S):
{{range .Authors}}{{ . }}{{end}}
{{end}}{{if .Commands}}
COMMANDS:
{{range .Commands}}{{join .Names ", "}}{{ "\t" }}{{.Usage}}
{{end}}{{end}}{{if .Flags}}
GLOBAL OPTIONS:
{{range category .Flags ""}}{{.}}
{{end}}
TUNING OPTIONS:
{{range category .Flags "tuning"}}{{.}}
{{end}}
AWS S3 OPTIONS:
{{range category .Flags "aws"}}{{.}}
{{end}}
MISC OPTIONS:
{{range category .Flags "misc"}}{{.}}
{{end}}{{end}}{{if .Copyright }}
COPYRIGHT:
{{.Copyright}}
{{end}}
`
}
var VersionHash string
func NewApp() (app *cli.App) {
uid, gid := MyUserAndGroup()
s3Default := (&S3Config{}).Init()
app = &cli.App{
Name: "goofys",
Version: "0.22.0-" + VersionHash,
Usage: "Mount an S3 bucket locally",
HideHelp: true,
Writer: os.Stderr,
Flags: []cli.Flag{
cli.BoolFlag{
Name: "help, h",
Usage: "Print this help text and exit successfully.",
},
/////////////////////////
// File system
/////////////////////////
cli.StringSliceFlag{
Name: "o",
Usage: "Additional system-specific mount options. Be careful!",
},
cli.StringFlag{
Name: "cache",
Usage: "Directory to use for data cache. " +
"Requires catfs and `-o allow_other'. " +
"Can also pass in other catfs options " +
"(ex: --cache \"--free:10%:$HOME/cache\") (default: off)",
},
cli.IntFlag{
Name: "dir-mode",
Value: 0755,
Usage: "Permission bits for directories. (default: 0755)",
},
cli.IntFlag{
Name: "file-mode",
Value: 0644,
Usage: "Permission bits for files. (default: 0644)",
},
cli.IntFlag{
Name: "uid",
Value: uid,
Usage: "UID owner of all inodes.",
},
cli.IntFlag{
Name: "gid",
Value: gid,
Usage: "GID owner of all inodes.",
},
/////////////////////////
// S3
/////////////////////////
cli.StringFlag{
Name: "endpoint",
Value: "",
Usage: "The non-AWS endpoint to connect to." +
" Possible values: http://127.0.0.1:8081/",
},
cli.StringFlag{
Name: "region",
Value: s3Default.Region,
Usage: "The region to connect to. Usually this is auto-detected." +
" Possible values: us-east-1, us-west-1, us-west-2, eu-west-1, " +
"eu-central-1, ap-southeast-1, ap-southeast-2, ap-northeast-1, " +
"sa-east-1, cn-north-1",
},
cli.BoolFlag{
Name: "requester-pays",
Usage: "Whether to allow access to requester-pays buckets (default: off)",
},
cli.StringFlag{
Name: "storage-class",
Value: s3Default.StorageClass,
Usage: "The type of storage to use when writing objects." +
" Possible values: REDUCED_REDUNDANCY, STANDARD, STANDARD_IA.",
},
cli.StringFlag{
Name: "profile",
Usage: "Use a named profile from $HOME/.aws/credentials instead of \"default\"",
},
cli.BoolFlag{
Name: "use-content-type",
Usage: "Set Content-Type according to file extension and /etc/mime.types (default: off)",
},
/// http://docs.aws.amazon.com/AmazonS3/latest/API/RESTObjectPUT.html
/// See http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingServerSideEncryption.html
cli.BoolFlag{
Name: "sse",
Usage: "Enable basic server-side encryption at rest (SSE-S3) in S3 for all writes (default: off)",
},
cli.StringFlag{
Name: "sse-kms",
Usage: "Enable KMS encryption (SSE-KMS) for all writes using this particular KMS `key-id`. Leave blank to Use the account's CMK - customer master key (default: off)",
Value: "",
},
cli.StringFlag{
Name: "sse-c",
Usage: "Enable server-side encryption using this base64-encoded key (default: off)",
Value: "",
},
/// http://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl
cli.StringFlag{
Name: "acl",
Usage: "The canned ACL to apply to the object. Possible values: private, public-read, public-read-write, authenticated-read, aws-exec-read, bucket-owner-read, bucket-owner-full-control (default: off)",
Value: "",
},
cli.BoolFlag{
Name: "subdomain",
Usage: "Enable subdomain mode of S3",
},
/////////////////////////
// Tuning
/////////////////////////
cli.BoolFlag{
Name: "cheap",
Usage: "Reduce S3 operation costs at the expense of some performance (default: off)",
},
cli.BoolFlag{
Name: "no-implicit-dir",
Usage: "Assume all directory objects (\"dir/\") exist (default: off)",
},
cli.DurationFlag{
Name: "stat-cache-ttl",
Value: time.Minute,
Usage: "How long to cache StatObject results and inode attributes.",
},
cli.DurationFlag{
Name: "type-cache-ttl",
Value: time.Minute,
Usage: "How long to cache name -> file/dir mappings in directory " +
"inodes.",
},
cli.DurationFlag{
Name: "http-timeout",
Value: 30 * time.Second,
Usage: "Set the timeout on HTTP requests to S3",
},
/////////////////////////
// Debugging
/////////////////////////
cli.BoolFlag{
Name: "debug_fuse",
Usage: "Enable fuse-related debugging output.",
},
cli.BoolFlag{
Name: "debug_s3",
Usage: "Enable S3-related debugging output.",
},
cli.BoolFlag{
Name: "f",
Usage: "Run goofys in foreground.",
},
},
}
var funcMap = template.FuncMap{
"category": filterCategory,
"join": strings.Join,
}
flagCategories = map[string]string{}
for _, f := range []string{"region", "sse", "sse-kms", "sse-c", "storage-class", "acl", "requester-pays"} {
flagCategories[f] = "aws"
}
for _, f := range []string{"cheap", "no-implicit-dir", "stat-cache-ttl", "type-cache-ttl", "http-timeout"} {
flagCategories[f] = "tuning"
}
for _, f := range []string{"help, h", "debug_fuse", "debug_s3", "version, v", "f"} {
flagCategories[f] = "misc"
}
cli.HelpPrinter = func(w io.Writer, templ string, data interface{}) {
w = tabwriter.NewWriter(w, 1, 8, 2, ' ', 0)
var tmplGet = template.Must(template.New("help").Funcs(funcMap).Parse(templ))
tmplGet.Execute(w, app)
}
return
}
func parseOptions(m map[string]string, s string) {
// NOTE(jacobsa): The man pages don't define how escaping works, and as far
// as I can tell there is no way to properly escape or quote a comma in the
// options list for an fstab entry. So put our fingers in our ears and hope
// that nobody needs a comma.
for _, p := range strings.Split(s, ",") {
var name string
var value string
// Split on the first equals sign.
if equalsIndex := strings.IndexByte(p, '='); equalsIndex != -1 {
name = p[:equalsIndex]
value = p[equalsIndex+1:]
} else {
name = p
}
m[name] = value
}
return
}
// PopulateFlags adds the flags accepted by run to the supplied flag set, returning the
// variables into which the flags will parse.
func PopulateFlags(c *cli.Context) (ret *FlagStorage) {
flags := &FlagStorage{
// File system
MountOptions: make(map[string]string),
DirMode: os.FileMode(c.Int("dir-mode")),
FileMode: os.FileMode(c.Int("file-mode")),
Uid: uint32(c.Int("uid")),
Gid: uint32(c.Int("gid")),
// Tuning,
Cheap: c.Bool("cheap"),
ExplicitDir: c.Bool("no-implicit-dir"),
StatCacheTTL: c.Duration("stat-cache-ttl"),
TypeCacheTTL: c.Duration("type-cache-ttl"),
HTTPTimeout: c.Duration("http-timeout"),
// Common Backend Config
Endpoint: c.String("endpoint"),
UseContentType: c.Bool("use-content-type"),
// Debugging,
DebugFuse: c.Bool("debug_fuse"),
DebugS3: c.Bool("debug_s3"),
Foreground: c.Bool("f"),
}
// S3
if c.IsSet("region") || c.IsSet("requester-pays") || c.IsSet("storage-class") ||
c.IsSet("profile") || c.IsSet("sse") || c.IsSet("sse-kms") ||
c.IsSet("sse-c") || c.IsSet("acl") || c.IsSet("subdomain") {
if flags.Backend == nil {
flags.Backend = (&S3Config{}).Init()
}
config, _ := flags.Backend.(*S3Config)
config.Region = c.String("region")
config.RegionSet = c.IsSet("region")
config.RequesterPays = c.Bool("requester-pays")
config.StorageClass = c.String("storage-class")
config.Profile = c.String("profile")
config.UseSSE = c.Bool("sse")
config.UseKMS = c.IsSet("sse-kms")
config.KMSKeyID = c.String("sse-kms")
config.SseC = c.String("sse-c")
config.ACL = c.String("acl")
config.Subdomain = c.Bool("subdomain")
// KMS implies SSE
if config.UseKMS {
config.UseSSE = true
}
}
// Handle the repeated "-o" flag.
for _, o := range c.StringSlice("o") {
parseOptions(flags.MountOptions, o)
}
flags.MountPointArg = c.Args()[1]
flags.MountPoint = flags.MountPointArg
var err error
defer func() {
if err != nil {
flags.Cleanup()
}
}()
if c.IsSet("cache") {
cache := c.String("cache")
cacheArgs := strings.Split(c.String("cache"), ":")
cacheDir := cacheArgs[len(cacheArgs)-1]
cacheArgs = cacheArgs[:len(cacheArgs)-1]
fi, err := os.Stat(cacheDir)
if err != nil || !fi.IsDir() {
io.WriteString(cli.ErrWriter,
fmt.Sprintf("Invalid value \"%v\" for --cache: not a directory\n\n",
cacheDir))
return nil
}
if _, ok := flags.MountOptions["allow_other"]; !ok {
flags.MountPointCreated, err = ioutil.TempDir("", ".goofys-mnt")
if err != nil {
io.WriteString(cli.ErrWriter,
fmt.Sprintf("Unable to create temp dir: %v", err))
return nil
}
flags.MountPoint = flags.MountPointCreated
}
cacheArgs = append([]string{"--test", "-f"}, cacheArgs...)
if flags.MountPointArg == flags.MountPoint {
cacheArgs = append(cacheArgs, "-ononempty")
}
cacheArgs = append(cacheArgs, "--")
cacheArgs = append(cacheArgs, flags.MountPoint)
cacheArgs = append(cacheArgs, cacheDir)
cacheArgs = append(cacheArgs, flags.MountPointArg)
fuseLog.Debugf("catfs %v", cacheArgs)
catfs := exec.Command("catfs", cacheArgs...)
_, err = catfs.Output()
if err != nil {
if ee, ok := err.(*exec.Error); ok {
io.WriteString(cli.ErrWriter,
fmt.Sprintf("--cache requires catfs (%v) but %v\n\n",
"http://github.com/kahing/catfs",
ee.Error()))
} else if ee, ok := err.(*exec.ExitError); ok {
io.WriteString(cli.ErrWriter,
fmt.Sprintf("Invalid value \"%v\" for --cache: %v\n\n",
cache, string(ee.Stderr)))
}
return nil
}
flags.Cache = cacheArgs[1:]
}
return flags
}
func MassageMountFlags(args []string) (ret []string) {
if len(args) == 5 && args[3] == "-o" {
// looks like it's coming from fstab!
mountOptions := ""
ret = append(ret, args[0])
for _, p := range strings.Split(args[4], ",") {
if strings.HasPrefix(p, "-") {
ret = append(ret, p)
} else {
mountOptions += p
mountOptions += ","
}
}
if len(mountOptions) != 0 {
// remove trailing ,
mountOptions = mountOptions[:len(mountOptions)-1]
ret = append(ret, "-o")
ret = append(ret, mountOptions)
}
ret = append(ret, args[1])
ret = append(ret, args[2])
} else {
return args
}
return
}