Kaynağa Gözat

Fixed issue in WriteAt

Truncate file on SetInodeAttribute with size 0
luis 7 yıl önce
ebeveyn
işleme
e531499d0a

+ 8 - 2
README.md

@@ -6,7 +6,11 @@ Mount a google drive in a folder
 
 Usage:
 ```bash
-gdrivemount [-d] [-v] destination_folder
+Usage: gdrivemount [options] MOUNTPOINT
+
+Options:
+  -d    Run app in background
+  -v    Verbose log
 ```
 
 Setup Google client secrets:
@@ -33,5 +37,7 @@ $HOME/.gdrivemount/client_secret.json
 
 TODO:
 --------
-Improve caching to refresh and save inodes
+* 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 // Cloudmount
 

+ 1 - 1
src/gdrivemount/cmd/gdrivemount/version.go

@@ -2,5 +2,5 @@ package main
 
 const (
   //Version contains version of the package
-  Version = "0.0.1 - built: 2017-07-07 03:10:31 UTC"
+  Version = "0.0.1-1-g67aeb24 - built: 2017-07-07 04:03:29 UTC"
 )

+ 4 - 0
src/gdrivemount/fileentry.go

@@ -76,6 +76,7 @@ func (fe *FileEntry) SetGFile(f *drive.File) {
 	attr := fuseops.InodeAttributes{}
 	attr.Nlink = 1
 	attr.Size = uint64(f.Size)
+	//attr.Size = uint64(f.QuotaBytesUsed)
 	// Temp
 	attr.Uid = fe.fs.getUID()
 	attr.Gid = fe.fs.getGID()
@@ -99,6 +100,7 @@ func (fe *FileEntry) Sync() (err error) {
 	if fe.tempFile == nil {
 		return
 	}
+
 	fe.tempFile.Sync()
 	fe.tempFile.Seek(0, io.SeekStart)
 
@@ -129,6 +131,7 @@ func (fe *FileEntry) Cache() *os.File {
 	}
 	var res *http.Response
 	var err error
+	log.Println("Caching file:", fe.GFile.Name)
 	// Export GDocs (Special google doc documents needs to be exported make a config somewhere for this)
 	switch fe.GFile.MimeType {
 	case "application/vnd.google-apps.document":
@@ -156,6 +159,7 @@ func (fe *FileEntry) Cache() *os.File {
 	}
 	io.Copy(fe.tempFile, res.Body)
 
+	log.Println("Caching done", fe.GFile.Name)
 	fe.tempFile.Seek(0, io.SeekStart)
 	return fe.tempFile
 

+ 37 - 12
src/gdrivemount/gdrive-fuse.go

@@ -135,15 +135,14 @@ func (fs *GDriveFS) timedRefresh() {
 
 }
 
-// Reload service files
+// Refresh service files
 func (fs *GDriveFS) Refresh() {
 	fs.nextRefresh = time.Now().Add(1 * time.Minute)
 
 	fileList := []*drive.File{}
 	fileMap := map[string]*drive.File{} // Temporary map by google drive fileID
-	//fileMap := map[string]*FileEntry{}
 
-	gdFields := googleapi.Field("nextPageToken, files(id,name,size,mimeType,parents,createdTime,modifiedTime)")
+	gdFields := googleapi.Field("nextPageToken, files(id,name,size,quotaBytesUsed, mimeType,parents,createdTime,modifiedTime)")
 	log.Println("Loading file entries from gdrive")
 	r, err := fs.srv.Files.List().
 		OrderBy("createdTime").
@@ -301,6 +300,19 @@ func (fs *GDriveFS) ReadDir(ctx context.Context, op *fuseops.ReadDirOp) (err err
 
 // SetInodeAttributes Not sure what attributes gdrive support we just leave this blank for now
 func (fs *GDriveFS) SetInodeAttributes(ctx context.Context, op *fuseops.SetInodeAttributesOp) (err error) {
+
+	if op.Attributes.Size == 0 { // Request to truncate i supose?
+		f := fs.root.FindByInode(op.Inode, true)
+		err = fs.srv.Files.Delete(f.GFile.Id).Do() // XXX: Careful on this
+		createdFile, err := fs.srv.Files.Create(&drive.File{Name: f.GFile.Name}).Do()
+		if err != nil {
+			return fuse.EINVAL
+		}
+		f.SetGFile(createdFile) // Set new file
+
+	}
+	// Hack to truncate file?
+
 	return
 }
 
@@ -458,8 +470,7 @@ func (fs *GDriveFS) CreateFile(ctx context.Context, op *fuseops.CreateFileOp) (e
 func (fs *GDriveFS) WriteFile(ctx context.Context, op *fuseops.WriteFileOp) (err error) {
 	lf, ok := fs.fileHandles[op.Handle]
 	if !ok {
-		err = fuse.EIO
-		return
+		return fuse.EIO
 	}
 
 	localFile := lf.entry.Cache()
@@ -477,16 +488,35 @@ func (fs *GDriveFS) WriteFile(ctx context.Context, op *fuseops.WriteFileOp) (err
 	return
 }
 
+// FlushFile just returns no error, maybe upload should be handled here
+func (fs *GDriveFS) FlushFile(ctx context.Context, op *fuseops.FlushFileOp) (err error) {
+	lf, ok := fs.fileHandles[op.Handle]
+	if !ok {
+		return fuse.EIO
+	}
+	if lf.entry.tempFile == nil {
+		return
+	}
+	if lf.uploadOnDone { // or if content changed basically
+		err = lf.entry.Sync()
+		if err != nil {
+			return fuse.EINVAL
+		}
+	}
+
+	return
+}
+
 // ReleaseFileHandle closes and deletes any temporary files, upload in case if changed locally
 func (fs *GDriveFS) ReleaseFileHandle(ctx context.Context, op *fuseops.ReleaseFileHandleOp) (err error) {
 	lf := fs.fileHandles[op.Handle]
 
-	if lf.uploadOnDone {
+	/*if lf.uploadOnDone {
 		err = lf.entry.Sync()
 		if err != nil {
 			return fuse.EINVAL
 		}
-	}
+	}*/
 	lf.entry.ClearCache()
 	delete(fs.fileHandles, op.Handle)
 
@@ -517,11 +547,6 @@ func (fs *GDriveFS) Unlink(ctx context.Context, op *fuseops.UnlinkOp) (err error
 	return
 }
 
-// FlushFile just returns no error, maybe upload should be handled here
-func (fs *GDriveFS) FlushFile(ctx context.Context, op *fuseops.FlushFileOp) (err error) {
-	return
-}
-
 // MkDir creates a directory on a parent dir
 func (fs *GDriveFS) MkDir(ctx context.Context, op *fuseops.MkDirOp) (err error) {