Parcourir la source

Several changes

Added flag -r for refresh time
Fixed inode caching
improved findByGID in filecontainer
luis il y a 7 ans
Parent
commit
7beb28f479

+ 24 - 20
README.md

@@ -3,14 +3,36 @@ cloudmount
 
 Linux util to Mount cloud drives
 
-Usage:
+####Usage:
+```bash
+$ cloudmount -h
+
+cloudmount-0.1-5-gea7b804 - built: 2017-07-10 18:24:27 UTC
+Usage: cloudmount [options] MOUNTPOINT
+
+Options:
+  -d	Run app in background
+  -o string
+    	uid,gid ex: -o uid=1000,gid=0 
+  -r duration
+    	Timed cloud synchronization interval [if applied] (default 2m0s)
+  -t string
+    	which cloud service to use [gdrive] (default "gdrive")
+  -v	Verbose log
+  -w string
+    	Work dir, path that holds configurations (default "/home/stdio/.cloudmount")
+
+```
+
+
+#### Example:
 ```bash
 $ go get dev.hexasoftware.com/hxs/cloudmount
 $ cloudmount MOUNTPOINT
 
 ```
 
-Support for:
+#### Support for:
 * Google Drive
 
 
@@ -52,21 +74,3 @@ HUP    | Perform a GC and shows memory usage <small>Works when its not running i
 
 
 
-####TODO:
-* Assure concurrency support on inode/handle creation for gdrive
-* Improve caching to refresh and save inodes
-* Reverse package structure instead of gdrivemount/cmd/gdrivemount use this as main package and move logic to subpackage
-* Use cloudmount -t gdrive -o uid, gid  MOUNTPOINT and add Support for other cloud services
-
-
-
-
-
-
-
-
-
-
-TODO:
-* create interface for drives
-* manager to handle drivers

+ 3 - 2
flags.go

@@ -14,12 +14,13 @@ import (
 func parseFlags(config *core.Config) (err error) {
 	var mountoptsFlag string
 
-	flag.StringVar(&config.CloudFSDriver, "t", "gdrive", "which cloud service to use [gdrive]")
+	flag.StringVar(&config.CloudFSDriver, "t", config.CloudFSDriver, "which cloud service to use [gdrive]")
 	flag.BoolVar(&config.Daemonize, "d", false, "Run app in background")
 	flag.BoolVar(&config.VerboseLog, "v", false, "Verbose log")
 	flag.StringVar(&config.HomeDir, "w", config.HomeDir, "Work dir, path that holds configurations")
+	flag.DurationVar(&config.RefreshTime, "r", config.RefreshTime, "Timed cloud synchronization interval [if applied]")
 
-	flag.StringVar(&mountoptsFlag, "o", "", "-o [opts]  uid,gid")
+	flag.StringVar(&mountoptsFlag, "o", "", "uid,gid ex: -o uid=1000,gid=0 ")
 
 	flag.Usage = func() {
 		fmt.Fprintf(os.Stderr, "Usage: %s [options] MOUNTPOINT\n\n", os.Args[0])

+ 6 - 4
internal/core/config.go

@@ -1,12 +1,14 @@
 package core
 
+import "time"
+
 // Config struct
 type Config struct {
 	Daemonize     bool
 	CloudFSDriver string
 	VerboseLog    bool
-
-	HomeDir string
-	UID     uint32 // Mount UID
-	GID     uint32 // Mount GID
+	RefreshTime   time.Duration
+	HomeDir       string
+	UID           uint32 // Mount UID
+	GID           uint32 // Mount GID
 }

+ 8 - 5
internal/core/core.go

@@ -11,6 +11,7 @@ import (
 	"runtime"
 	"strconv"
 	"syscall"
+	"time"
 
 	"dev.hexasoftware.com/hxs/prettylog"
 
@@ -47,11 +48,13 @@ func New() *Core {
 	return &Core{
 		Drivers: map[string]DriverFactory{},
 		Config: Config{
-			HomeDir:    filepath.Join(usr.HomeDir, ".cloudmount"),
-			UID:        uint32(uid),
-			GID:        uint32(gid),
-			VerboseLog: false,
-			Daemonize:  false,
+			Daemonize:     false,
+			CloudFSDriver: "gdrive",
+			VerboseLog:    false,
+			RefreshTime:   2 * time.Minute,
+			HomeDir:       filepath.Join(usr.HomeDir, ".cloudmount"),
+			UID:           uint32(uid),
+			GID:           uint32(gid),
 		},
 	}
 

+ 20 - 2
internal/fs/gdrivefs/file_container.go

@@ -1,6 +1,7 @@
 package gdrivefs
 
 import (
+	"os"
 	"sync"
 
 	"github.com/jacobsa/fuse/fuseops"
@@ -22,7 +23,16 @@ func NewFileContainer(fs *GDriveFS) *FileContainer {
 		fs:          fs,
 		inodeMU:     &sync.Mutex{},
 	}
-	fc.tree = fc.FileEntry(fuseops.RootInodeID)
+	rootEntry := fc.FileEntry(fuseops.RootInodeID)
+
+	rootEntry.Attr = fuseops.InodeAttributes{
+		Mode: os.FileMode(0755) | os.ModeDir,
+		Uid:  fs.config.UID,
+		Gid:  fs.config.GID,
+	}
+	rootEntry.isDir = true
+	fc.tree = rootEntry
+
 	return fc
 }
 
@@ -30,6 +40,15 @@ func (fc *FileContainer) FindByInode(inode fuseops.InodeID) *FileEntry {
 	return fc.fileEntries[inode]
 }
 
+func (fc *FileContainer) FindByGID(gid string) *FileEntry {
+	for _, v := range fc.fileEntries {
+		if v.GFile != nil && v.GFile.Id == gid {
+			return v
+		}
+	}
+	return nil
+}
+
 //Return or create inode
 func (fc *FileContainer) FileEntry(inodeOps ...fuseops.InodeID) *FileEntry {
 
@@ -73,5 +92,4 @@ func (fc *FileContainer) RemoveEntry(entry *FileEntry) {
 		}
 	}
 	delete(fc.fileEntries, inode)
-
 }

+ 6 - 6
internal/fs/gdrivefs/file_entry.go

@@ -54,14 +54,14 @@ func (fe *FileEntry) RemoveChild(child *FileEntry) {
 }
 
 // useful for debug to count children
-func (fe *FileEntry) Count() int {
+/*func (fe *FileEntry) Count() int {
 	count := 0
 
 	for _, c := range fe.children {
 		count += c.Count()
 	}
 	return count + len(fe.children)
-}
+}*/
 
 // SetGFile update attributes and set drive.File
 func (fe *FileEntry) SetGFile(f *drive.File) {
@@ -160,7 +160,7 @@ func (fe *FileEntry) Cache() *os.File {
 // WRONG
 func (fe *FileEntry) solveAppendGFile(f *drive.File, inode fuseops.InodeID) *FileEntry {
 
-	fil := fe.FindByGID(f.Id, true)
+	fil := fe.container.FindByGID(f.Id)
 	if fil != nil { // ignore existing ID
 		return fil
 	}
@@ -169,7 +169,7 @@ func (fe *FileEntry) solveAppendGFile(f *drive.File, inode fuseops.InodeID) *Fil
 		return fe.AppendGFile(f, inode) // = append(fs.root.fileList, entry)
 	}
 	for _, parent := range f.Parents { // hierarchy add
-		parentEntry := fe.FindByGID(parent, true)
+		parentEntry := fe.container.FindByGID(parent)
 		if parentEntry == nil {
 			log.Fatalln("Non existent parent", parent)
 		}
@@ -255,7 +255,7 @@ func (fe *FileEntry) FindByName(name string, recurse bool) *FileEntry {
 }
 
 // FindByGID find by google drive ID
-func (fe *FileEntry) FindByGID(gdriveID string, recurse bool) *FileEntry {
+/*func (fe *FileEntry) FindByGID(gdriveID string, recurse bool) *FileEntry {
 	// Recurse??
 	for _, e := range fe.children {
 		if e.GFile.Id == gdriveID {
@@ -270,4 +270,4 @@ func (fe *FileEntry) FindByGID(gdriveID string, recurse bool) *FileEntry {
 	}
 	// For each child we findByInode
 	return nil
-}
+}*/

+ 12 - 27
internal/fs/gdrivefs/gdrivefs.go

@@ -64,15 +64,6 @@ func New(core *core.Core) core.Driver {
 	fs.root.uid = core.Config.UID
 	fs.root.gid = core.Config.GID
 
-	rootEntry := fs.root.FileEntry(fuseops.RootInodeID)
-
-	rootEntry.Attr = fuseops.InodeAttributes{
-		Mode: os.FileMode(0755) | os.ModeDir,
-		Uid:  core.Config.UID,
-		Gid:  core.Config.GID,
-	}
-	rootEntry.isDir = true
-
 	//fs.root = rootEntry
 
 	// Temporary entry
@@ -110,16 +101,6 @@ func (fs *GDriveFS) createHandle() *Handle {
 	return handle
 }
 
-// Cache somewhere?
-/*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() {
 
 	go func() {
@@ -127,7 +108,7 @@ func (fs *GDriveFS) timedRefresh() {
 			if time.Now().After(fs.nextRefresh) {
 				fs.Refresh()
 			}
-			time.Sleep(2 * time.Minute) // 2 minutes
+			time.Sleep(fs.config.RefreshTime) // 2 minutes
 		}
 	}()
 
@@ -197,7 +178,9 @@ func (fs *GDriveFS) Refresh() {
 	// Helper func to recurse
 	// Everything loaded we add to our entries
 	// Add file and its parents priorizing it parent
+
 	var appendFile func(df *drive.File)
+
 	appendFile = func(df *drive.File) {
 		for _, pID := range df.Parents {
 			parentFile, ok := fileMap[pID]
@@ -213,18 +196,19 @@ func (fs *GDriveFS) Refresh() {
 
 		// Find existing entry
 		var inode fuseops.InodeID
-		entry := fs.root.tree.FindByGID(df.Id, true)
+		entry := fs.root.FindByGID(df.Id)
+		// Store for later add
 		if entry == nil {
-			inode = root.FileEntry().Inode // This can be a problem if next time a existing inode comes? Allocate new file entry with new Inode
+			inode = fs.root.FileEntry().Inode // Register new inode on Same Root fs
+			//inode = root.FileEntry().Inode // This can be a problem if next time a existing inode comes? Allocate new file entry with new Inode
 		} else {
-			inode = entry.Inode //
+			inode = entry.Inode // Reuse existing root fs inode
 		}
 
-		newEntry := fs.root.tree.solveAppendGFile(df, inode) // Find right parent
-		if entry != nil && entry.GFile.Name == df.Name {     // Copy name from old entry
+		newEntry := root.tree.solveAppendGFile(df, inode) // Find right parent
+		if entry != nil && entry.GFile.Name == df.Name {  // Copy name from old entry
 			newEntry.Name = entry.Name
 		}
-
 		// add File
 	}
 
@@ -233,9 +217,10 @@ func (fs *GDriveFS) Refresh() {
 	}
 
 	log.Println("Refresh done, update root")
+	fs.root = root
 	//fs.root.children = root.children
 
-	log.Println("File count:", fs.root.tree.Count())
+	log.Println("File count:", len(fs.root.fileEntries))
 }
 
 ///////////////////////////////

BIN
test/cloudmount


+ 1 - 1
version.go

@@ -2,5 +2,5 @@ package main
 
 const (
   //Version contains version of the package
-  Version = "0.1-3-gf9b411c - built: 2017-07-10 16:54:44 UTC"
+  Version = "0.1-5-gea7b804 - built: 2017-07-10 18:24:27 UTC"
 )