Browse Source

Fixed slash conversion on Linux bug

Toby Chui 3 years ago
parent
commit
fc79da0816
2 changed files with 30 additions and 23 deletions
  1. 8 1
      mod/filesystem/arozfs/arozfs.go
  2. 22 22
      mod/filesystem/fileOpr.go

+ 8 - 1
mod/filesystem/arozfs/arozfs.go

@@ -86,9 +86,16 @@ func ToSlash(filename string) string {
 
 func Base(filename string) string {
 	filename = ToSlash(filename)
-	if filename == "" || filename == "/" {
+	if filename == "" {
+		return "."
+	}
+	if filename == "/" {
 		return filename
 	}
+	for len(filename) > 0 && filename[len(filename)-1] == '/' {
+		filename = filename[0 : len(filename)-1]
+	}
+
 	c := strings.Split(filename, "/")
 	if len(c) == 1 {
 		return c[0]

+ 22 - 22
mod/filesystem/fileOpr.go

@@ -211,7 +211,7 @@ func ArozZipFileWithProgress(targetFshs []*FileSystemHandler, filelist []string,
 		//Local File System
 		if fshAbs.IsDir(srcpath) {
 			//This is a directory
-			topLevelFolderName := filepath.ToSlash(filepath.Base(filepath.Dir(srcpath)) + "/" + filepath.Base(srcpath))
+			topLevelFolderName := filepath.ToSlash(arozfs.Base(filepath.Dir(srcpath)) + "/" + arozfs.Base(srcpath))
 			err = fshAbs.Walk(srcpath, func(path string, info os.FileInfo, err error) error {
 				if err != nil {
 					return err
@@ -235,7 +235,7 @@ func ArozZipFileWithProgress(targetFshs []*FileSystemHandler, filelist []string,
 				if includeTopLevelFolder {
 					relativePath = topLevelFolderName + "/" + relativePath
 				} else {
-					relativePath = filepath.Base(srcpath) + "/" + relativePath
+					relativePath = arozfs.Base(srcpath) + "/" + relativePath
 				}
 
 				f, err := writer.Create(relativePath)
@@ -250,7 +250,7 @@ func ArozZipFileWithProgress(targetFshs []*FileSystemHandler, filelist []string,
 
 				//Update the zip progress
 				currentFileCount++
-				progressHandler(filepath.Base(srcpath), currentFileCount, totalFileCount, (float64(currentFileCount)/float64(totalFileCount))*float64(100))
+				progressHandler(arozfs.Base(srcpath), currentFileCount, totalFileCount, (float64(currentFileCount)/float64(totalFileCount))*float64(100))
 				return nil
 			})
 
@@ -259,13 +259,13 @@ func ArozZipFileWithProgress(targetFshs []*FileSystemHandler, filelist []string,
 			}
 		} else {
 			//This is a file
-			topLevelFolderName := filepath.Base(filepath.Dir(srcpath))
+			topLevelFolderName := arozfs.Base(filepath.Dir(srcpath))
 			thisFile, err := fshAbs.ReadStream(srcpath)
 			if err != nil {
 				return err
 			}
 			defer thisFile.Close()
-			relativePath := filepath.Base(srcpath)
+			relativePath := arozfs.Base(srcpath)
 			if includeTopLevelFolder {
 				relativePath = topLevelFolderName + "/" + relativePath
 			}
@@ -282,7 +282,7 @@ func ArozZipFileWithProgress(targetFshs []*FileSystemHandler, filelist []string,
 
 			//Update the zip progress
 			currentFileCount++
-			progressHandler(filepath.Base(srcpath), currentFileCount, totalFileCount, (float64(currentFileCount)/float64(totalFileCount))*float64(100))
+			progressHandler(arozfs.Base(srcpath), currentFileCount, totalFileCount, (float64(currentFileCount)/float64(totalFileCount))*float64(100))
 
 		}
 	}
@@ -321,7 +321,7 @@ func ArozZipFile(sourceFshs []*FileSystemHandler, filelist []string, outputFsh *
 			//Use local fs functions
 			if IsDir(srcpath) {
 				//This is a directory
-				topLevelFolderName := filepath.ToSlash(filepath.Base(filepath.Dir(srcpath)) + "/" + filepath.Base(srcpath))
+				topLevelFolderName := filepath.ToSlash(arozfs.Base(filepath.Dir(srcpath)) + "/" + arozfs.Base(srcpath))
 				err = filepath.Walk(srcpath, func(path string, info os.FileInfo, err error) error {
 					if err != nil {
 						return err
@@ -344,7 +344,7 @@ func ArozZipFile(sourceFshs []*FileSystemHandler, filelist []string, outputFsh *
 					if includeTopLevelFolder {
 						relativePath = topLevelFolderName + "/" + relativePath
 					} else {
-						relativePath = filepath.Base(srcpath) + "/" + relativePath
+						relativePath = arozfs.Base(srcpath) + "/" + relativePath
 					}
 
 					f, err := writer.Create(relativePath)
@@ -365,13 +365,13 @@ func ArozZipFile(sourceFshs []*FileSystemHandler, filelist []string, outputFsh *
 				}
 			} else {
 				//This is a file
-				topLevelFolderName := filepath.Base(filepath.Dir(srcpath))
+				topLevelFolderName := arozfs.Base(filepath.Dir(srcpath))
 				thisFile, err := os.Open(srcpath)
 				if err != nil {
 					return err
 				}
 				defer thisFile.Close()
-				relativePath := filepath.Base(srcpath)
+				relativePath := arozfs.Base(srcpath)
 				if includeTopLevelFolder {
 					relativePath = topLevelFolderName + "/" + relativePath
 				}
@@ -391,7 +391,7 @@ func ArozZipFile(sourceFshs []*FileSystemHandler, filelist []string, outputFsh *
 			fshAbs = thisFsh.FileSystemAbstraction
 			if fshAbs.IsDir(srcpath) {
 				//This is a directory
-				topLevelFolderName := filepath.ToSlash(filepath.Base(filepath.Dir(srcpath)) + "/" + filepath.Base(srcpath))
+				topLevelFolderName := filepath.ToSlash(arozfs.Base(filepath.Dir(srcpath)) + "/" + arozfs.Base(srcpath))
 				err = fshAbs.Walk(srcpath, func(path string, info os.FileInfo, err error) error {
 					if err != nil {
 						return err
@@ -417,7 +417,7 @@ func ArozZipFile(sourceFshs []*FileSystemHandler, filelist []string, outputFsh *
 					if includeTopLevelFolder {
 						relativePath = topLevelFolderName + "/" + relativePath
 					} else {
-						relativePath = filepath.Base(srcpath) + "/" + relativePath
+						relativePath = arozfs.Base(srcpath) + "/" + relativePath
 					}
 
 					f, err := writer.Create(relativePath)
@@ -438,13 +438,13 @@ func ArozZipFile(sourceFshs []*FileSystemHandler, filelist []string, outputFsh *
 				}
 			} else {
 				//This is a file
-				topLevelFolderName := filepath.Base(filepath.Dir(srcpath))
+				topLevelFolderName := arozfs.Base(filepath.Dir(srcpath))
 				thisFile, err := fshAbs.ReadStream(srcpath)
 				if err != nil {
 					return err
 				}
 				defer thisFile.Close()
-				relativePath := filepath.Base(srcpath)
+				relativePath := arozfs.Base(srcpath)
 				if includeTopLevelFolder {
 					relativePath = topLevelFolderName + "/" + relativePath
 				}
@@ -560,7 +560,7 @@ func FileCopy(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 
 		if progressUpdate != nil {
 			//Set progress to 100, leave it to upper level abstraction to handle
-			progressUpdate(100, filepath.Base(realDest))
+			progressUpdate(100, arozfs.Base(realDest))
 		}
 	}
 	return nil
@@ -586,8 +586,8 @@ func FileMove(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 	}
 
 	//Check if the target file already exists.
-	movedFilename := filepath.Base(src)
-	if destAbst.FileExists(filepath.Join(dest, filepath.Base(src))) {
+	movedFilename := arozfs.Base(src)
+	if destAbst.FileExists(filepath.Join(dest, arozfs.Base(src))) {
 		//Handle cases where file already exists
 		if mode == "" {
 			//Do not specific file exists principle
@@ -598,19 +598,19 @@ func FileMove(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 		} else if mode == "overwrite" {
 			//Continue with the following code
 			//Check if the copy and paste dest are identical
-			if filepath.ToSlash(filepath.Clean(src)) == filepath.ToSlash(filepath.Clean(filepath.Join(dest, filepath.Base(src)))) {
+			if filepath.ToSlash(filepath.Clean(src)) == filepath.ToSlash(filepath.Clean(filepath.Join(dest, arozfs.Base(src)))) {
 				//Source and target identical. Cannot overwrite.
 				return errors.New("Source and destination paths are identical.")
 			}
 
 		} else if mode == "keep" {
 			//Keep the file but saved with 'Copy' suffix
-			newFilename := strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) + " - Copy" + filepath.Ext(src)
+			newFilename := strings.TrimSuffix(arozfs.Base(src), filepath.Ext(src)) + " - Copy" + filepath.Ext(src)
 			//Check if the newFilename already exists. If yes, continue adding suffix
 			duplicateCounter := 0
 			for destAbst.FileExists(filepath.Join(dest, newFilename)) {
 				duplicateCounter++
-				newFilename = strings.TrimSuffix(filepath.Base(src), filepath.Ext(src)) + " - Copy(" + strconv.Itoa(duplicateCounter) + ")" + filepath.Ext(src)
+				newFilename = strings.TrimSuffix(arozfs.Base(src), filepath.Ext(src)) + " - Copy(" + strconv.Itoa(duplicateCounter) + ")" + filepath.Ext(src)
 				if duplicateCounter > 1024 {
 					//Maxmium loop encountered. For thread safty, terminate here
 					return errors.New("Too many copies of identical files.")
@@ -660,7 +660,7 @@ func FileMove(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 
 		//Update the progress
 		if progressUpdate != nil {
-			progressUpdate(100, filepath.Base(src))
+			progressUpdate(100, arozfs.Base(src))
 		}
 
 		f, err := srcAbst.ReadStream(src)
@@ -742,7 +742,7 @@ func dirCopy(srcFsh *FileSystemHandler, src string, destFsh *FileSystemHandler,
 
 			//Update move progress
 			if progressUpdate != nil {
-				progressUpdate(int(float64(fileCounter)/float64(totalFileCounts)*100), filepath.Base(fileSrc))
+				progressUpdate(int(float64(fileCounter)/float64(totalFileCounts)*100), arozfs.Base(fileSrc))
 			}
 
 			//Move the file using BLFC