瀏覽代碼

Updated group unzip logic, fixed nil pointer on deleting file during quota scan

Toby Chui 3 年之前
父節點
當前提交
0bb980b541
共有 2 個文件被更改,包括 19 次插入9 次删除
  1. 15 9
      mod/filesystem/fileOpr.go
  2. 4 0
      mod/quota/quota.go

+ 15 - 9
mod/filesystem/fileOpr.go

@@ -117,34 +117,40 @@ func ArozUnzipFileWithProgress(filelist []string, outputfile string, progressHan
 			defer reader.Close()
 			path := filepath.Join(outputfile, file.Name)
 
-			err = os.MkdirAll(path, os.ModePerm)
+			parentFolder := path
+			if !file.FileInfo().IsDir() {
+				//Is file. Change target to its parent dir
+				parentFolder = filepath.Dir(path)
+			}
+
+			err = os.MkdirAll(parentFolder, 0775)
 			if err != nil {
 				return err
 			}
 
 			if file.FileInfo().IsDir() {
-				//Folder extracted
-
+				//Folder is created already be the steps above.
 				//Update the progress
 				unzippedFileCount++
 				progressHandler(file.Name, unzippedFileCount, totalFileCounts, float64(unzippedFileCount)/float64(totalFileCounts)*100.0)
 				continue
 			}
 
-			err = os.Remove(path)
-			if err != nil {
-				return err
-			}
-
+			//Extrat and write to the target file
 			writer, err := os.OpenFile(path, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, file.Mode())
 			if err != nil {
 				return err
 			}
-			defer writer.Close()
 			_, err = io.Copy(writer, reader)
 			if err != nil {
+				//Extraction failed. Remove this file if exists
+				writer.Close()
+				if FileExists(path) {
+					os.Remove(path)
+				}
 				return err
 			}
+			writer.Close()
 
 			//Update the progress
 			unzippedFileCount++

+ 4 - 0
mod/quota/quota.go

@@ -131,6 +131,10 @@ func (q *QuotaHandler) CalculateQuotaUsage() {
 				continue
 			}
 			err := filepath.Walk(filepath.ToSlash(filepath.Clean(thisfs.Path))+"/users/"+q.username, func(path string, info os.FileInfo, err error) error {
+				if info == nil || err != nil {
+					//This file gone when quota is calculating. Ignore this
+					return nil
+				}
 				if !info.IsDir() {
 					totalUsedVolume += fs.GetFileSize(path)
 				}