Parcourir la source

Changes on proj struct

luis il y a 7 ans
Parent
commit
1fac6df0f5
8 fichiers modifiés avec 153 ajouts et 58 suppressions
  1. 1 2
      README.md
  2. 42 0
      core/core.go
  3. 11 0
      core/driver.go
  4. 9 12
      fs/gdrivefs/client.go
  5. 4 2
      fs/gdrivefs/fileentry.go
  6. 32 28
      fs/gdrivefs/gdrive-fuse.go
  7. 4 3
      fs/gdrivefs/service.go
  8. 50 11
      main.go

+ 1 - 2
README.md

@@ -6,8 +6,7 @@ Linux util to Mount cloud drives
 Usage:
 ```bash
 $ go get dev.hexasoftware.com/hxs/cloudmount
-$ go install dev.hexasoftware.com/hxs/cloudmount
-$
+$ cloudmount MOUNTPOINT
 
 ```
 

+ 42 - 0
core/core.go

@@ -0,0 +1,42 @@
+package core
+
+import (
+	"os/user"
+	"path/filepath"
+	"strconv"
+)
+
+var (
+	Drivers = map[string]DriverFactory{}
+	Config  ConfigData
+)
+
+type ConfigData struct {
+	WorkDir string
+	UID     uint32 // Mount UID
+	GID     uint32 // Mount GID
+}
+
+// TODO Friendly panics
+func init() {
+	usr, err := user.Current()
+	if err != nil {
+		panic(err)
+	}
+
+	uid, err := strconv.Atoi(usr.Uid)
+	if err != nil {
+		panic(err)
+	}
+	gid, err := strconv.Atoi(usr.Uid)
+	if err != nil {
+		panic(gid)
+	}
+
+	Config = ConfigData{
+		WorkDir: filepath.Join(usr.HomeDir, ".cloudmount"),
+		UID:     uint32(uid),
+		GID:     uint32(gid),
+	}
+
+}

+ 11 - 0
core/driver.go

@@ -0,0 +1,11 @@
+package core
+
+import "github.com/jacobsa/fuse/fuseutil"
+
+// Base Driver
+type Driver interface {
+	fuseutil.FileSystem
+	Refresh()
+}
+
+type DriverFactory func() Driver

+ 9 - 12
fs/gdrivefs/client.go

@@ -8,9 +8,10 @@ import (
 	"net/http"
 	"net/url"
 	"os"
-	"os/user"
 	"path/filepath"
 
+	"dev.hexasoftware.com/hxs/cloudmount/core"
+
 	drive "google.golang.org/api/drive/v3"
 
 	"golang.org/x/oauth2"
@@ -33,12 +34,10 @@ func getClient(ctx context.Context, config *oauth2.Config) *http.Client {
 }
 
 func tokenCacheFile() (string, error) {
-	tokenCacheDir, err := getConfigPath()
-	if err != nil {
-		return "", err
-	}
+	tokenCacheDir := core.Config.WorkDir
+
+	err := os.MkdirAll(tokenCacheDir, 0700)
 
-	os.MkdirAll(tokenCacheDir, 0700)
 	return filepath.Join(tokenCacheDir, url.QueryEscape("auth.json")), err
 
 }
@@ -90,7 +89,7 @@ func saveToken(file string, token *oauth2.Token) {
 	json.NewEncoder(f).Encode(token)
 }
 
-func getConfigPath() (string, error) {
+/*func getConfigPath() (string, error) {
 	usr, err := user.Current()
 	if err != nil {
 		return "", err
@@ -98,14 +97,12 @@ func getConfigPath() (string, error) {
 	configDir := filepath.Join(usr.HomeDir, ".gdrivemount")
 
 	return configDir, nil
-}
+}*/
 
 func GetDriveService() *drive.Service {
 
-	configPath, err := getConfigPath()
-	if err != nil {
-		log.Fatal("Unable to fetch config path")
-	}
+	configPath := core.Config.WorkDir
+
 	ctx := context.Background()
 
 	b, err := ioutil.ReadFile(filepath.Join(configPath, "client_secret.json"))

+ 4 - 2
fs/gdrivefs/fileentry.go

@@ -10,6 +10,8 @@ import (
 	"strings"
 	"time"
 
+	"dev.hexasoftware.com/hxs/cloudmount/core"
+
 	"github.com/jacobsa/fuse/fuseops"
 	drive "google.golang.org/api/drive/v3"
 )
@@ -78,8 +80,8 @@ func (fe *FileEntry) SetGFile(f *drive.File) {
 	attr.Size = uint64(f.Size)
 	//attr.Size = uint64(f.QuotaBytesUsed)
 	// Temp
-	attr.Uid = fe.fs.getUID()
-	attr.Gid = fe.fs.getGID()
+	attr.Uid = core.Config.UID
+	attr.Gid = core.Config.GID
 	attr.Crtime, _ = time.Parse(time.RFC3339, f.CreatedTime)
 	attr.Ctime = attr.Crtime // Set CTime to created, although it is change inode metadata
 	attr.Mtime, _ = time.Parse(time.RFC3339, f.ModifiedTime)

+ 32 - 28
fs/gdrivefs/gdrive-fuse.go

@@ -5,10 +5,10 @@ import (
 	"io"
 	"os"
 	"os/user"
-	"strconv"
 	"syscall"
 	"time"
 
+	"dev.hexasoftware.com/hxs/cloudmount/core"
 	"dev.hexasoftware.com/hxs/prettylog"
 
 	"golang.org/x/net/context"
@@ -21,6 +21,10 @@ import (
 	"github.com/jacobsa/fuse/fuseutil"
 )
 
+func init() {
+	core.Drivers["gdrive"] = NewGDriveFS
+}
+
 var (
 	log = prettylog.New("gdrivemount")
 )
@@ -53,29 +57,7 @@ type GDriveFS struct {
 	// Map IDS with FileEntries
 }
 
-////////////////////////////////////////////////////////
-// TOOLS & HELPERS
-////////////////////////////////////////////////////////
-
-func (fs *GDriveFS) createHandle() *fileHandle {
-	// Lock here instead
-
-	var handle fuseops.HandleID
-
-	for handle = 1; handle < 99999; handle++ {
-		_, ok := fs.fileHandles[handle]
-		if !ok {
-			break
-		}
-	}
-
-	fh := &fileHandle{handleID: handle}
-	fs.fileHandles[handle] = fh
-
-	return fh
-}
-
-func NewGDriveFS() *GDriveFS {
+func NewGDriveFS() core.Driver {
 
 	osuser, err := user.Current()
 	if err != nil {
@@ -91,8 +73,8 @@ func NewGDriveFS() *GDriveFS {
 			Mode:  os.FileMode(0755) | os.ModeDir,
 			Nlink: 1,
 			Size:  4096,
-			Uid:   fs.getUID(),
-			Gid:   fs.getGID(),
+			Uid:   core.Config.UID,
+			Gid:   core.Config.GID,
 		},
 		GFile: nil,
 		Inode: fuseops.RootInodeID,
@@ -112,15 +94,37 @@ func NewGDriveFS() *GDriveFS {
 	return fs
 }
 
+////////////////////////////////////////////////////////
+// TOOLS & HELPERS
+////////////////////////////////////////////////////////
+
+func (fs *GDriveFS) createHandle() *fileHandle {
+	// Lock here instead
+
+	var handle fuseops.HandleID
+
+	for handle = 1; handle < 99999; handle++ {
+		_, ok := fs.fileHandles[handle]
+		if !ok {
+			break
+		}
+	}
+
+	fh := &fileHandle{handleID: handle}
+	fs.fileHandles[handle] = fh
+
+	return fh
+}
+
 // Cache somewhere?
-func (fs *GDriveFS) getUID() uint32 {
+/*func (fs *GDriveFS) getUID() uint32 {
 	uid, _ := strconv.Atoi(fs.osuser.Uid)
 	return uint32(uid)
 }
 func (fs *GDriveFS) getGID() uint32 {
 	gid, _ := strconv.Atoi(fs.osuser.Gid)
 	return uint32(gid)
-}
+}*/
 
 func (fs *GDriveFS) timedRefresh() {
 

+ 4 - 3
fs/gdrivefs/service.go

@@ -1,9 +1,10 @@
 package gdrivefs
 
-import "dev.hexasoftware.com/hxs/core"
+import "github.com/jacobsa/fuse/fuseutil"
 
-type Service interface {
-	core.Service
+// Driver for gdrive
+type GDriveDriver interface {
+	Fuse() fuseutil.FileSystem // Fetch the file system
 }
 
 type gdriveService struct {

+ 50 - 11
main.go

@@ -1,4 +1,4 @@
-//+build linux
+// +build linux
 
 package main
 
@@ -9,17 +9,21 @@ import (
 	"flag"
 	"fmt"
 	"os"
+	"strconv"
+	"strings"
 
 	"os/exec"
 	"os/signal"
 	"runtime"
 	"syscall"
 
-	"dev.hexasoftware.com/hxs/cloudmount/fs/gdrivefs"
+	"dev.hexasoftware.com/hxs/cloudmount/core"
 	"github.com/jacobsa/fuse"
 	"github.com/jacobsa/fuse/fuseutil"
 
 	"dev.hexasoftware.com/hxs/prettylog"
+
+	_ "dev.hexasoftware.com/hxs/cloudmount/fs/gdrivefs"
 	//_ "github.com/icattlecoder/godaemon" // No reason
 )
 
@@ -29,17 +33,19 @@ var (
 )
 
 func main() {
-	var daemonize bool
-	var verboselog bool
-	var clouddrive string
+	var daemonizeFlag bool
+	var verboselogFlag bool
+	var clouddriveFlag string
+	var mountoptsFlag string
 
 	prettylog.Global()
 	// getClient
 	fmt.Printf("%s-%s\n\n", Name, Version)
 
-	flag.StringVar(&clouddrive, "t", "gdrive", "which cloud service to use [gdrive]")
-	flag.BoolVar(&daemonize, "d", false, "Run app in background")
-	flag.BoolVar(&verboselog, "v", false, "Verbose log")
+	flag.StringVar(&clouddriveFlag, "t", "gdrive", "which cloud service to use [gdrive]")
+	flag.StringVar(&mountoptsFlag, "o", "", "-o [opts]  uid,gid")
+	flag.BoolVar(&daemonizeFlag, "d", false, "Run app in background")
+	flag.BoolVar(&verboselogFlag, "v", false, "Verbose log")
 
 	flag.Usage = func() {
 		fmt.Fprintf(os.Stderr, "Usage: %s [options] MOUNTPOINT\n\n", os.Args[0])
@@ -54,11 +60,44 @@ func main() {
 		//fmt.Println("Usage:\n gdrivemount [-d] [-v] MOUNTPOINT")
 		return
 	}
+	/////////////////////////////////////
+	// Parse mount opts
+	/////////////////
+	pmountopts := strings.Split(mountoptsFlag, ",")
+	mountopts := map[string]string{}
+	for _, v := range pmountopts {
+		keypart := strings.Split(v, "=")
+		if len(keypart) != 2 {
+			continue
+		}
+		mountopts[keypart[0]] = keypart[1]
+	}
 
-	driveFS := gdrivefs.NewGDriveFS() // there can be some interaction before daemon
+	/////////////////////////////////////
+	// Use mount opts
+	///////////////
+	uidStr, ok := mountopts["uid"]
+	if ok {
+		uid, err := strconv.Atoi(uidStr)
+		if err != nil {
+			panic(err)
+		}
+		core.Config.UID = uint32(uid)
+	}
+
+	///////////////////////////////
+	// cloud drive Type
+	/////////////////
+	f, ok := core.Drivers[clouddriveFlag] // there can be some interaction before daemon
+	if !ok {
+		log.Fatal("FileSystem not supported")
+	}
+	driveFS := f()
 
+	////////////////////////////////
 	// Daemon
-	if daemonize {
+	/////////////////
+	if daemonizeFlag {
 		subArgs := []string{}
 		for _, arg := range os.Args[1:] {
 			if arg == "-d" { // ignore daemon flag
@@ -84,7 +123,7 @@ func main() {
 	var err error
 	var mfs *fuse.MountedFileSystem
 
-	if verboselog {
+	if verboselogFlag {
 		mfs, err = fuse.Mount(mountPath, server, &fuse.MountConfig{DebugLogger: prettylog.New("fuse"), ErrorLogger: prettylog.New("fuse-err")})
 	} else {
 		mfs, err = fuse.Mount(mountPath, server, &fuse.MountConfig{})