forked from plexdrive/plexdrive
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
170 lines (148 loc) · 4.74 KB
/
main.go
File metadata and controls
170 lines (148 loc) · 4.74 KB
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
package main
import (
"fmt"
"os"
"os/user"
"path/filepath"
"time"
"strings"
"syscall"
"os/signal"
"github.com/claudetech/loggo"
. "github.com/claudetech/loggo/default"
flag "github.com/ogier/pflag"
"golang.org/x/sys/unix"
)
func main() {
// get the users home dir
user, err := user.Current()
if nil != err {
panic(fmt.Sprintf("Could not read users homedir %v\n", err))
}
// parse the command line arguments
argLogLevel := flag.IntP("verbosity", "v", 0, "Set the log level (0 = error, 1 = warn, 2 = info, 3 = debug, 4 = trace)")
argConfigPath := flag.StringP("config", "c", filepath.Join(user.HomeDir, ".plexdrive"), "The path to the configuration directory")
argTempPath := flag.StringP("temp", "t", os.TempDir(), "Path to a temporary directory to store temporary data")
argChunkSize := flag.Int64("chunk-size", 5*1024*1024, "The size of each chunk that is downloaded (in byte)")
argRefreshInterval := flag.Duration("refresh-interval", 5*time.Minute, "The time to wait till checking for changes")
argClearInterval := flag.Duration("clear-chunk-interval", 1*time.Minute, "The time to wait till clearing the chunk directory")
argClearChunkAge := flag.Duration("clear-chunk-age", 30*time.Minute, "The maximum age of a cached chunk file")
argMountOptions := flag.StringP("fuse-options", "o", "", "Fuse mount options (e.g. -fuse-options allow_other,...)")
argVersion := flag.Bool("version", false, "Displays program's version information")
argUID := flag.Int64("uid", -1, "Set the mounts UID (-1 = default permissions)")
argGID := flag.Int64("gid", -1, "Set the mounts GID (-1 = default permissions)")
flag.Parse()
// display version information
if *argVersion {
fmt.Println("2.0.0")
return
}
// check if mountpoint is specified
argMountPoint := flag.Arg(0)
if "" == argMountPoint {
flag.Usage()
panic(fmt.Errorf("Mountpoint not specified"))
}
// calculate uid / gid
uid := uint32(unix.Geteuid())
gid := uint32(unix.Getegid())
if *argUID > -1 {
uid = uint32(*argUID)
}
if *argGID > -1 {
gid = uint32(*argGID)
}
// parse the mount options
var mountOptions []string
if "" != *argMountOptions {
mountOptions = strings.Split(*argMountOptions, ",")
}
// initialize the logger with the specific log level
var logLevel loggo.Level
switch *argLogLevel {
case 0:
logLevel = loggo.Error
case 1:
logLevel = loggo.Warning
case 2:
logLevel = loggo.Info
case 3:
logLevel = loggo.Debug
case 4:
logLevel = loggo.Trace
default:
logLevel = loggo.Warning
}
Log.SetLevel(logLevel)
// debug all given parameters
Log.Debugf("verbosity : %v", logLevel)
Log.Debugf("config : %v", *argConfigPath)
Log.Debugf("temp : %v", *argTempPath)
Log.Debugf("chunk-size : %v", *argChunkSize)
Log.Debugf("refresh-interval : %v", *argRefreshInterval)
Log.Debugf("clear-chunk-interval : %v", *argClearInterval)
Log.Debugf("clear-chunk-age : %v", *argClearChunkAge)
Log.Debugf("fuse-options : %v", *argMountOptions)
Log.Debugf("UID : %v", uid)
Log.Debugf("GID : %v", gid)
// version missing here
// create all directories
if err := os.MkdirAll(*argConfigPath, 0766); nil != err {
Log.Errorf("Could not create configuration directory")
Log.Debugf("%v", err)
os.Exit(1)
}
chunkPath := filepath.Join(*argTempPath, "chunks")
if err := os.MkdirAll(chunkPath, 0777); nil != err {
Log.Errorf("Could not create temp chunk directory")
Log.Debugf("%v", err)
os.Exit(2)
}
// set the global buffer configuration
SetChunkPath(chunkPath)
SetChunkSize(*argChunkSize)
// read the configuration
configPath := filepath.Join(*argConfigPath, "config.json")
config, err := ReadConfig(configPath)
if nil != err {
config, err = CreateConfig(configPath)
if nil != err {
Log.Errorf("Could not read configuration")
Log.Debugf("%v", err)
os.Exit(3)
}
}
cache, err := NewCache(*argConfigPath, *argLogLevel > 3)
if nil != err {
Log.Errorf("Could not initialize cache")
Log.Debugf("%v", err)
os.Exit(4)
}
defer cache.Close()
drive, err := NewDriveClient(config, cache, *argRefreshInterval)
if nil != err {
Log.Errorf("Could not initialize Google Drive Client")
Log.Debugf("%v", err)
os.Exit(5)
}
// check os signals like SIGINT/TERM
checkOsSignals(argMountPoint)
go CleanChunkDir(chunkPath, *argClearInterval, *argClearChunkAge)
if err := Mount(drive, argMountPoint, mountOptions, uid, gid); nil != err {
Log.Debugf("%v", err)
os.Exit(6)
}
}
func checkOsSignals(mountpoint string) {
signals := make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGINT)
go func() {
for sig := range signals {
if sig == syscall.SIGINT {
if err := Unmount(mountpoint, false); nil != err {
Log.Warningf("%v", err)
}
}
}
}()
}