Przeglądaj źródła

Move trash to a single root .trash folder

Trash scans used to walk the whole drive looking for per-folder .metadata/.trash directories. Each file system root now keeps one .trash folder, and a removed item keeps the folders it came from with the removal time appended:

  user:/foo/bar/a.txt -> user:/.trash/foo/bar/a.txt.<unix time>

Scans read only that folder and never look inside trashed folders. Folder names on the way that look like a trashed item ("backup.2024") get a "~" appended so the mapping stays reversible.

Changes:
- Add helpers to map between original and trash paths, scan a root's trash, and prune folders left empty.
- Rework list, scan, restore, clear, nightly expiry and recycle to use them.
- Restore now checks write permission and refuses to overwrite an existing item. It recreates the original folder if it was removed.
- Recycle rejects items already in the trash and the root folder.
- Drive storage analysis walks the new .trash location.
- Replace the old trash tests with tests for the new helpers and scan/prune behaviour.

Items trashed under the old per-folder .metadata/.trash layout are not migrated.
Toby Chui 3 tygodni temu
rodzic
commit
dee86e2296

+ 376 - 234
src/file_system.go

@@ -1212,7 +1212,7 @@ Space a single trashed entry takes up
 A folder counts for everything inside it, not the zero bytes its own entry
 reports - otherwise a user could fill the bin with folders and never reach
 the quota. Hidden files are included because they were moved along with the
-rest, and the trash itself lives inside a hidden folder.
+rest, and the trash itself is a hidden folder.
 */
 func system_fs_getTrashEntrySize(fsh *filesystem.FileSystemHandler, rpath string) int64 {
 	if fsh.FileSystemAbstraction.IsDir(rpath) {
@@ -1224,13 +1224,9 @@ func system_fs_getTrashEntrySize(fsh *filesystem.FileSystemHandler, rpath string
 
 // Total bytes currently sitting in a user's trash across every file system
 func system_fs_getTrashUsage(username string) int64 {
-	files, fshs, err := system_fs_listTrash(username)
-	if err != nil {
-		return 0
-	}
 	total := int64(0)
-	for c, file := range files {
-		total += system_fs_getTrashEntrySize(fshs[c], file)
+	for _, item := range system_fs_listTrash(username) {
+		total += system_fs_getTrashEntrySize(item.Fsh, item.Realpath)
 	}
 	return total
 }
@@ -1284,12 +1280,280 @@ func system_fs_handleTrashSettings(w http.ResponseWriter, r *http.Request) {
 	utils.SendJSONResponse(w, string(js))
 }
 
+/*
+Trash layout
+
+Every file system root keeps a single .trash folder. A removed item keeps the
+folders it was removed from, and the unix time it was removed at is appended
+to its name:
+
+	user:/foo/bar/deleteme.txt  ->  user:/.trash/foo/bar/deleteme.txt.1712345678
+
+so scanning the trash only reads that one folder instead of the whole drive,
+and never looks inside a trashed folder. A folder on the way whose own name
+looks like a trashed item ("backup.2024") would be taken for one, so such a
+name gets a "~" appended (plus one for every "~" it already ends with), which
+keeps the mapping reversible.
+*/
+const trashFolderSubpath = "/.trash"
+
+// A trashed item found in the .trash folder of a file system root
+type trashItem struct {
+	Fsh             *filesystem.FileSystemHandler
+	Subpath         string //Where it sits, e.g. /.trash/foo/bar/deleteme.txt.1712345678
+	Realpath        string
+	OriginalSubpath string //Where it was removed from, e.g. /foo/bar/deleteme.txt
+	Timestamp       int64  //Unix time it was removed at
+}
+
+// Check if a name is a trashed item name: <name>.<unix time>
+func system_fs_isTrashName(name string) bool {
+	ext := path.Ext(name)
+	if len(ext) < 2 || len(ext) == len(name) {
+		return false
+	}
+	for _, c := range ext[1:] {
+		if c < '0' || c > '9' {
+			return false
+		}
+	}
+	_, err := strconv.ParseInt(ext[1:], 10, 64)
+	return err == nil
+}
+
+// Escape a folder name on the way to a trashed item so it is never taken for one
+func system_fs_escapeTrashFolder(name string) string {
+	if system_fs_isTrashName(strings.TrimRight(name, "~")) {
+		return name + "~"
+	}
+	return name
+}
+
+func system_fs_unescapeTrashFolder(name string) string {
+	if strings.HasSuffix(name, "~") && system_fs_isTrashName(strings.TrimRight(name, "~")) {
+		return strings.TrimSuffix(name, "~")
+	}
+	return name
+}
+
+// Check if a subpath is the .trash folder or anything inside it
+func system_fs_isInTrash(subpath string) bool {
+	cleaned := path.Clean("/" + filepath.ToSlash(subpath))
+	return cleaned == trashFolderSubpath || strings.HasPrefix(cleaned, trashFolderSubpath+"/")
+}
+
+/*
+Where an item removed at the given time goes inside the trash
+
+	/foo/bar/deleteme.txt -> /.trash/foo/bar/deleteme.txt.<timestamp>
+
+Returns an empty string for the root folder, which cannot be trashed.
+*/
+func system_fs_trashSubpathOf(originalSubpath string, timestamp int64) string {
+	cleaned := strings.Trim(path.Clean("/"+filepath.ToSlash(originalSubpath)), "/")
+	if cleaned == "" {
+		return ""
+	}
+	segments := strings.Split(cleaned, "/")
+	last := len(segments) - 1
+	for i := 0; i < last; i++ {
+		segments[i] = system_fs_escapeTrashFolder(segments[i])
+	}
+	segments[last] += "." + strconv.FormatInt(timestamp, 10)
+	return trashFolderSubpath + "/" + strings.Join(segments, "/")
+}
+
+/*
+Where a trashed item came from and when it was removed
+
+	/.trash/foo/bar/deleteme.txt.<timestamp> -> /foo/bar/deleteme.txt, <timestamp>
+
+ok is false for anything that is not a trashed item: a folder on the way, a
+file inside a trashed folder or a path outside the trash.
+*/
+func system_fs_originalSubpathOf(trashSubpath string) (string, int64, bool) {
+	cleaned := path.Clean("/" + filepath.ToSlash(trashSubpath))
+	if !strings.HasPrefix(cleaned, trashFolderSubpath+"/") {
+		return "", 0, false
+	}
+	segments := strings.Split(strings.TrimPrefix(cleaned, trashFolderSubpath+"/"), "/")
+	last := len(segments) - 1
+	for i := 0; i < last; i++ {
+		if system_fs_isTrashName(segments[i]) {
+			//Inside a trashed folder
+			return "", 0, false
+		}
+		segments[i] = system_fs_unescapeTrashFolder(segments[i])
+	}
+	if !system_fs_isTrashName(segments[last]) {
+		return "", 0, false
+	}
+	ext := path.Ext(segments[last])
+	timestamp, _ := strconv.ParseInt(ext[1:], 10, 64)
+	segments[last] = strings.TrimSuffix(segments[last], ext)
+	return "/" + strings.Join(segments, "/"), timestamp, true
+}
+
+/*
+Scan the .trash folder of one file system root and call fn for every trashed
+item in it. Only the folders on the way are read, never the inside of a
+trashed folder. An error returned by fn stops the scan and is returned.
+*/
+func system_fs_scanTrashOf(fsh *filesystem.FileSystemHandler, username string, fn func(trashItem) error) error {
+	fshAbs := fsh.FileSystemAbstraction
+	trashRoot, err := fshAbs.VirtualPathToRealPath(trashFolderSubpath, username)
+	if err != nil || !fshAbs.IsDir(trashRoot) {
+		return nil
+	}
+
+	var visit func(rdir string, subdir string) error
+	visit = func(rdir string, subdir string) error {
+		entries, err := fshAbs.ReadDir(rdir)
+		if err != nil {
+			return nil
+		}
+		for _, entry := range entries {
+			rpath := rdir + "/" + entry.Name()
+			subpath := subdir + "/" + entry.Name()
+			if system_fs_isTrashName(entry.Name()) {
+				originalSubpath, timestamp, ok := system_fs_originalSubpathOf(subpath)
+				if !ok {
+					continue
+				}
+				err := fn(trashItem{
+					Fsh:             fsh,
+					Subpath:         subpath,
+					Realpath:        rpath,
+					OriginalSubpath: originalSubpath,
+					Timestamp:       timestamp,
+				})
+				if err != nil {
+					return err
+				}
+			} else if entry.IsDir() {
+				//A folder on the way, or leftovers like a thumbnail .metadata folder
+				if err := visit(rpath, subpath); err != nil {
+					return err
+				}
+			}
+		}
+		return nil
+	}
+	return visit(filepath.ToSlash(trashRoot), trashFolderSubpath)
+}
+
+// The file system roots whose trash a user sees
+func system_fs_trashRootsOf(userinfo *user.User) []*filesystem.FileSystemHandler {
+	roots := []*filesystem.FileSystemHandler{}
+	for _, storage := range userinfo.GetAllFileSystemHandler() {
+		if storage.Hierarchy == "backup" {
+			//Skip this fsh
+			continue
+		}
+		roots = append(roots, storage)
+	}
+	return roots
+}
+
+// Get every trashed item of a user across all their file system roots
+func system_fs_listTrash(username string) []trashItem {
+	userinfo, err := userHandler.GetUserInfoFromUsername(username)
+	if err != nil {
+		return []trashItem{}
+	}
+	items := []trashItem{}
+	for _, fsh := range system_fs_trashRootsOf(userinfo) {
+		system_fs_scanTrashOf(fsh, username, func(item trashItem) error {
+			items = append(items, item)
+			return nil
+		})
+	}
+	return items
+}
+
+// Describe a trashed item to the client
+func system_fs_getTrashedFileInfo(item trashItem) trashedFile {
+	fshAbs := item.Fsh.FileSystemAbstraction
+	originalName := path.Base(item.OriginalSubpath)
+	isDir := fshAbs.IsDir(item.Realpath)
+	originalExt := ""
+	if !isDir {
+		originalExt = path.Ext(originalName)
+	}
+	return trashedFile{
+		Filename:         path.Base(item.Subpath),
+		Filepath:         item.Fsh.UUID + ":" + item.Subpath,
+		FileExt:          originalExt,
+		IsDir:            isDir,
+		Filesize:         int64(fshAbs.GetFileSize(item.Realpath)),
+		RemoveTimestamp:  item.Timestamp,
+		RemoveDate:       time.Unix(item.Timestamp, 0).Format("2006-01-02 15:04:05"),
+		OriginalPath:     item.Fsh.UUID + ":" + path.Dir(item.OriginalSubpath),
+		OriginalFilename: originalName,
+	}
+}
+
+// Remove a trashed item for good, along with the owner's quota record of it
+func system_fs_removeTrashItem(userinfo *user.User, item trashItem) error {
+	vpath := item.Fsh.UUID + ":" + item.Subpath
+	if userinfo.IsOwnerOfFile(item.Fsh, vpath) {
+		userinfo.RemoveOwnershipFromFile(item.Fsh, vpath)
+	}
+	return item.Fsh.FileSystemAbstraction.RemoveAll(item.Realpath)
+}
+
+/*
+Remove every folder below rdir (rdir included) that holds no trashed item,
+and report whether rdir still holds one. Leftovers such as the .metadata
+folder a thumbnail of a trashed file leaves behind, or a .DS_Store, go too.
+*/
+func system_fs_pruneTrashTree(fshAbs filesystem.FileSystemAbstraction, rdir string) bool {
+	entries, err := fshAbs.ReadDir(rdir)
+	if err != nil {
+		//Cannot tell what is in there; leave it alone
+		return true
+	}
+	kept := false
+	for _, entry := range entries {
+		if system_fs_isTrashName(entry.Name()) {
+			kept = true
+		} else if entry.IsDir() && system_fs_pruneTrashTree(fshAbs, rdir+"/"+entry.Name()) {
+			kept = true
+		}
+	}
+	if !kept {
+		fshAbs.RemoveAll(rdir)
+	}
+	return kept
+}
+
+// After an item left the trash, remove the folders it left empty, up to the .trash folder itself
+func system_fs_pruneTrashParents(fsh *filesystem.FileSystemHandler, username string, rdir string) {
+	fshAbs := fsh.FileSystemAbstraction
+	trashRoot, err := fshAbs.VirtualPathToRealPath(trashFolderSubpath, username)
+	if err != nil {
+		return
+	}
+	trashRoot = path.Clean(filepath.ToSlash(trashRoot))
+	rdir = path.Clean(filepath.ToSlash(rdir))
+	for rdir == trashRoot || strings.HasPrefix(rdir, trashRoot+"/") {
+		if !fshAbs.FileExists(rdir) {
+			//Already gone, try its parent
+		} else if system_fs_pruneTrashTree(fshAbs, rdir) {
+			return
+		}
+		if rdir == trashRoot {
+			return
+		}
+		rdir = path.Dir(rdir)
+	}
+}
+
 /*
 Nightly cleanup
 
 Removes trashed files past their owner's retention window. Users who have
-not set a retention (or set it to zero) keep their trash indefinitely, which
-is the behaviour every account had before this setting existed.
+set the retention to zero keep their trash indefinitely.
 */
 func system_fs_clearExpiredTrash() {
 	for _, username := range authAgent.ListUsers() {
@@ -1305,43 +1569,36 @@ func system_fs_clearExpiredTrash() {
 		}
 
 		cutoff := time.Now().Unix() - int64(retentionDays)*86400
-		files, fshs, err := system_fs_listTrash(username)
-		if err != nil {
-			continue
-		}
-
 		removed := 0
-		for c, file := range files {
-			if !nightlyShouldMaintainFsh(fshs[c]) {
+		for _, fsh := range system_fs_trashRootsOf(userinfo) {
+			if !nightlyShouldMaintainFsh(fsh) {
 				//Trash inside a drive shared by the cluster is emptied by the
 				//master node, so it is not purged once per member
 				continue
 			}
 
-			/*
-				The removal time is stored as the file extension when the file
-				was recycled. Anything without a parsable timestamp is left
-				alone rather than guessed at.
-			*/
-			ext := filepath.Ext(file)
-			if len(ext) < 2 {
-				continue
-			}
-			timestamp, err := utils.StringToInt64(ext[1:])
-			if err != nil || timestamp > cutoff {
+			expired := []trashItem{}
+			system_fs_scanTrashOf(fsh, username, func(item trashItem) error {
+				if item.Timestamp <= cutoff {
+					expired = append(expired, item)
+				}
+				return nil
+			})
+			if len(expired) == 0 {
 				continue
 			}
 
-			fshAbs := fshs[c].FileSystemAbstraction
-			fileVpath, err := fshAbs.RealPathToVirtualPath(file, username)
-			if err == nil && userinfo.IsOwnerOfFile(fshs[c], fileVpath) {
-				userinfo.RemoveOwnershipFromFile(fshs[c], fileVpath)
+			for _, item := range expired {
+				if system_fs_removeTrashItem(userinfo, item) == nil {
+					removed++
+				}
 			}
-			fshAbs.RemoveAll(file)
 
-			//Drop the .trash folder too once nothing is left in it
-			system_fs_removeTrashDirIfEmpty(fshAbs, filepath.Dir(file))
-			removed++
+			//Drop the folders the removed items left empty in one pass
+			trashRoot, err := fsh.FileSystemAbstraction.VirtualPathToRealPath(trashFolderSubpath, username)
+			if err == nil {
+				system_fs_pruneTrashTree(fsh.FileSystemAbstraction, filepath.ToSlash(trashRoot))
+			}
 		}
 
 		if removed > 0 {
@@ -1351,7 +1608,7 @@ func system_fs_clearExpiredTrash() {
 	}
 }
 
-// Scan all directory and get trash file and send back results with WebSocket
+// Scan the trash of every root and send back the results with WebSocket
 func system_fs_WebSocketScanTrashBin(w http.ResponseWriter, r *http.Request) {
 	//Get and check user permission
 	userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
@@ -1371,117 +1628,34 @@ func system_fs_WebSocketScanTrashBin(w http.ResponseWriter, r *http.Request) {
 		return
 	}
 
-	//Start Scanning
-	scanningRoots := []*filesystem.FileSystemHandler{}
-	//Get all roots to scan
-	for _, storage := range userinfo.GetAllFileSystemHandler() {
-		if storage.Hierarchy == "backup" {
-			//Skip this fsh
-			continue
-		}
-		scanningRoots = append(scanningRoots, storage)
-	}
-
-	for _, fsh := range scanningRoots {
-		thisFshAbs := fsh.FileSystemAbstraction
-		rootPath, err := thisFshAbs.VirtualPathToRealPath("", userinfo.Username)
-		if err != nil {
-			continue
-		}
-		err = thisFshAbs.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
-			if system_fs_isTrashEntry(path) {
-				//This is a trashbin dir.
-				file := path
-
-				//Parse the trashFile struct
-				timestamp := filepath.Ext(file)[1:]
-				originalName := strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file)))
-				originalExt := filepath.Ext(filepath.Base(originalName))
-				virtualFilepath, _ := thisFshAbs.RealPathToVirtualPath(file, userinfo.Username)
-				virtualOrgPath, _ := thisFshAbs.RealPathToVirtualPath(filepath.Dir(filepath.Dir(filepath.Dir(file))), userinfo.Username)
-				rawsize := thisFshAbs.GetFileSize(file)
-				timestampInt64, _ := utils.StringToInt64(timestamp)
-				removeTimeDate := time.Unix(timestampInt64, 0)
-				if thisFshAbs.IsDir(file) {
-					originalExt = ""
-				}
-
-				thisTrashFileObject := trashedFile{
-					Filename:         filepath.Base(file),
-					Filepath:         virtualFilepath,
-					FileExt:          originalExt,
-					IsDir:            thisFshAbs.IsDir(file),
-					Filesize:         int64(rawsize),
-					RemoveTimestamp:  timestampInt64,
-					RemoveDate:       removeTimeDate.Format("2006-01-02 15:04:05"),
-					OriginalPath:     virtualOrgPath,
-					OriginalFilename: originalName,
-				}
-
-				//Send out the result as JSON string
-				js, _ := json.Marshal(thisTrashFileObject)
-				err := c.WriteMessage(1, js)
-				if err != nil {
-					//Connection already closed
-					return err
-				}
-			}
-
-			return nil
+	for _, fsh := range system_fs_trashRootsOf(userinfo) {
+		err = system_fs_scanTrashOf(fsh, userinfo.Username, func(item trashItem) error {
+			//Send out the result as JSON string
+			js, _ := json.Marshal(system_fs_getTrashedFileInfo(item))
+			return c.WriteMessage(1, js)
 		})
 
 		if err != nil {
-			//Scan or client connection error (Connection closed?)
+			//Client connection error (Connection closed?)
 			return
 		}
 	}
 
 	//Close connection after finished
 	c.Close()
-
 }
 
-// Scan all the directory and get trash files within the system
+// Scan the trash of every root and return the trashed files within the system
 func system_fs_scanTrashBin(w http.ResponseWriter, r *http.Request) {
 	userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
 	if err != nil {
 		utils.SendErrorResponse(w, err.Error())
 		return
 	}
-	username := userinfo.Username
 
 	results := []trashedFile{}
-	files, fshs, err := system_fs_listTrash(username)
-	if err != nil {
-		utils.SendErrorResponse(w, err.Error())
-		return
-	}
-
-	//Get information of each files and process it into results
-	for c, file := range files {
-		fsAbs := fshs[c].FileSystemAbstraction
-		timestamp := filepath.Ext(file)[1:]
-		originalName := strings.TrimSuffix(filepath.Base(file), filepath.Ext(filepath.Base(file)))
-		originalExt := filepath.Ext(filepath.Base(originalName))
-		virtualFilepath, _ := fsAbs.RealPathToVirtualPath(file, userinfo.Username)
-		virtualOrgPath, _ := fsAbs.RealPathToVirtualPath(filepath.Dir(filepath.Dir(filepath.Dir(file))), userinfo.Username)
-		rawsize := fsAbs.GetFileSize(file)
-		timestampInt64, _ := utils.StringToInt64(timestamp)
-		removeTimeDate := time.Unix(timestampInt64, 0)
-		if fsAbs.IsDir(file) {
-			originalExt = ""
-		}
-		results = append(results, trashedFile{
-			Filename:         filepath.Base(file),
-			Filepath:         virtualFilepath,
-			FileExt:          originalExt,
-			IsDir:            fsAbs.IsDir(file),
-			Filesize:         int64(rawsize),
-			RemoveTimestamp:  timestampInt64,
-			RemoveDate:       removeTimeDate.Format("2006-01-02 15:04:05"),
-			OriginalPath:     virtualOrgPath,
-			OriginalFilename: originalName,
-		})
+	for _, item := range system_fs_listTrash(userinfo.Username) {
+		results = append(results, system_fs_getTrashedFileInfo(item))
 	}
 
 	//Sort the results by date, latest on top
@@ -1494,7 +1668,7 @@ func system_fs_scanTrashBin(w http.ResponseWriter, r *http.Request) {
 	utils.SendJSONResponse(w, string(jsonString))
 }
 
-// Restore a trashed file to its parent dir
+// Restore a trashed file to the folder it was removed from
 func system_fs_restoreFile(w http.ResponseWriter, r *http.Request) {
 	userinfo, err := userHandler.GetUserInfoFromRequest(w, r)
 	if err != nil {
@@ -1515,129 +1689,76 @@ func system_fs_restoreFile(w http.ResponseWriter, r *http.Request) {
 	}
 	fshAbs := fsh.FileSystemAbstraction
 
-	//Translate it to realpath
-	realpath, _ := fshAbs.VirtualPathToRealPath(subpath, userinfo.Username)
-	if !fshAbs.FileExists(realpath) {
-		utils.SendErrorResponse(w, "File not exists")
-		return
-	}
-
 	//Check if this is really a trashed file
-	if filepath.Base(filepath.Dir(realpath)) != ".trash" {
+	originalSubpath, _, ok := system_fs_originalSubpathOf(subpath)
+	if !ok {
 		utils.SendErrorResponse(w, "File not in trashbin")
 		return
 	}
 
-	//OK to proceed.
-	originalFilename := strings.TrimSuffix(filepath.Base(realpath), filepath.Ext(filepath.Base(realpath)))
-	restoreFolderRoot := filepath.Dir(filepath.Dir(filepath.Dir(realpath)))
-	targetPath := filepath.ToSlash(filepath.Join(restoreFolderRoot, originalFilename))
-	//systemWideLogger.PrintAndLog("File System", (targetPath)
-	fshAbs.Rename(realpath, targetPath)
-
-	//Check if the parent dir has no more fileds. If yes, remove it
-	system_fs_removeTrashDirIfEmpty(fshAbs, filepath.Dir(realpath))
+	realpath, err := fshAbs.VirtualPathToRealPath(subpath, userinfo.Username)
+	if err != nil || !fshAbs.FileExists(realpath) {
+		utils.SendErrorResponse(w, "File not exists")
+		return
+	}
 
-	utils.SendOK(w)
-}
+	if !userinfo.CanWrite(fsh.UUID + ":" + originalSubpath) {
+		utils.SendErrorResponse(w, "Access Denied")
+		return
+	}
 
-// Clear all trashed file in the system
-func system_fs_clearTrashBin(w http.ResponseWriter, r *http.Request) {
-	u, err := userHandler.GetUserInfoFromRequest(w, r)
+	targetPath, err := fshAbs.VirtualPathToRealPath(originalSubpath, userinfo.Username)
 	if err != nil {
-		utils.SendErrorResponse(w, "User not logged in")
+		utils.SendErrorResponse(w, err.Error())
+		return
+	}
+	if fshAbs.FileExists(targetPath) {
+		utils.SendErrorResponse(w, "An item with the same name already exists in the original location")
 		return
 	}
 
-	fileList, fshs, err := system_fs_listTrash(u.Username)
-
+	//The folder it came from may have been removed since
+	fshAbs.MkdirAll(path.Dir(filepath.ToSlash(targetPath)), 0755)
+	err = fshAbs.Rename(realpath, targetPath)
 	if err != nil {
-		utils.SendErrorResponse(w, "Unable to clear trash: "+err.Error())
+		systemWideLogger.PrintAndLog("File System", "Failed to restore file from trash", err)
+		utils.SendErrorResponse(w, "Failed to restore file from trash")
 		return
 	}
 
-	//Get list success. Remove each of them.
-	for c, file := range fileList {
-		fileVpath, _ := fshs[c].FileSystemAbstraction.RealPathToVirtualPath(file, u.Username)
-		isOwner := u.IsOwnerOfFile(fshs[c], fileVpath)
-		if isOwner {
-			//This user own this system. Remove this file from his quota
-			u.RemoveOwnershipFromFile(fshs[c], fileVpath)
-		}
-		fshAbs := fshs[c].FileSystemAbstraction
-		fshAbs.RemoveAll(file)
-		//Check if its parent directory have no files. If yes, remove the dir itself as well.
-		system_fs_removeTrashDirIfEmpty(fshAbs, filepath.Dir(file))
-	}
+	//Drop the folders in the trash it leaves empty
+	system_fs_pruneTrashParents(fsh, userinfo.Username, path.Dir(filepath.ToSlash(realpath)))
 
 	utils.SendOK(w)
 }
 
-/*
-Check if a path is a trashed item: an entry right inside a .trash folder whose
-name ends with the .<unix time> it was removed at. Anything else in there,
-like the .metadata folder a thumbnail of a trashed file leaves behind or a
-.DS_Store, is not something the user put in the trash.
-*/
-func system_fs_isTrashEntry(path string) bool {
-	if filepath.Base(filepath.Dir(path)) != ".trash" {
-		return false
-	}
-	ext := filepath.Ext(path)
-	if len(ext) < 2 {
-		return false
-	}
-	_, err := utils.StringToInt64(ext[1:])
-	return err == nil
-}
-
-// Remove a .trash folder (with the leftovers in it) once no trashed item is left inside
-func system_fs_removeTrashDirIfEmpty(fshAbs filesystem.FileSystemAbstraction, trashDir string) {
-	entries, err := fshAbs.Glob(filepath.ToSlash(trashDir) + "/*")
+// Clear all trashed file in the system
+func system_fs_clearTrashBin(w http.ResponseWriter, r *http.Request) {
+	u, err := userHandler.GetUserInfoFromRequest(w, r)
 	if err != nil {
+		utils.SendErrorResponse(w, "User not logged in")
 		return
 	}
-	for _, entry := range entries {
-		if system_fs_isTrashEntry(entry) {
-			return
-		}
-	}
-	fshAbs.RemoveAll(trashDir)
-}
 
-// Get all trash in a string list
-func system_fs_listTrash(username string) ([]string, []*filesystem.FileSystemHandler, error) {
-	userinfo, _ := userHandler.GetUserInfoFromUsername(username)
-	scanningRoots := []*filesystem.FileSystemHandler{}
-	//Get all roots to scan
-	for _, storage := range userinfo.GetAllFileSystemHandler() {
-		if storage.Hierarchy == "backup" {
-			//Skip this fsh
-			continue
-		}
-
-		scanningRoots = append(scanningRoots, storage)
-	}
-
-	files := []string{}
-	fshs := []*filesystem.FileSystemHandler{}
-	for _, thisFsh := range scanningRoots {
-		thisFshAbs := thisFsh.FileSystemAbstraction
-		rootPath, _ := thisFshAbs.VirtualPathToRealPath("", userinfo.Username)
-		err := thisFshAbs.Walk(rootPath, func(path string, info os.FileInfo, err error) error {
-			if system_fs_isTrashEntry(path) {
-				//This is a trashbin dir.
-				files = append(files, path)
-				fshs = append(fshs, thisFsh)
-			}
+	for _, fsh := range system_fs_trashRootsOf(u) {
+		items := []trashItem{}
+		system_fs_scanTrashOf(fsh, u.Username, func(item trashItem) error {
+			items = append(items, item)
 			return nil
 		})
-		if err != nil {
-			continue
+		for _, item := range items {
+			system_fs_removeTrashItem(u, item)
+		}
+
+		//Everything left in there is a leftover. Remove the whole .trash folder
+		fshAbs := fsh.FileSystemAbstraction
+		trashRoot, err := fshAbs.VirtualPathToRealPath(trashFolderSubpath, u.Username)
+		if err == nil && fshAbs.FileExists(trashRoot) {
+			fshAbs.RemoveAll(trashRoot)
 		}
 	}
 
-	return files, fshs, nil
+	utils.SendOK(w)
 }
 
 /*
@@ -2627,13 +2748,13 @@ func system_fs_handleOpr(w http.ResponseWriter, r *http.Request) {
 					return
 				}
 
-				//Deleted from the trash bin. Drop the .trash folder once it is empty
-				if filepath.Base(filepath.Dir(rsrcFile)) == ".trash" {
-					system_fs_removeTrashDirIfEmpty(srcFshAbs, filepath.Dir(rsrcFile))
+				//Deleted from the trash bin. Drop the folders it left empty there
+				if system_fs_isInTrash(subpath) {
+					system_fs_pruneTrashParents(srcFsh, userinfo.Username, path.Dir(filepath.ToSlash(rsrcFile)))
 				}
 
 			} else if operation == "recycle" {
-				//Put it into a subfolder named trash and allow it to to be removed later
+				//Move it into the .trash folder of its root and allow it to to be removed later
 				if !srcFshAbs.FileExists(rsrcFile) {
 					//Check if it is a non escapted file instead
 					utils.SendErrorResponse(w, "Source file not exists")
@@ -2647,6 +2768,15 @@ func system_fs_handleOpr(w http.ResponseWriter, r *http.Request) {
 					return
 				}
 
+				if system_fs_isInTrash(subpath) {
+					utils.SendErrorResponse(w, "This item is already in the trash bin")
+					return
+				}
+				if system_fs_trashSubpathOf(subpath, 0) == "" {
+					utils.SendErrorResponse(w, "The root folder cannot be moved to the trash bin")
+					return
+				}
+
 				//Check if this file has any cached files. If yes, remove it
 				metadata.RemoveCache(srcFsh, rsrcFile)
 
@@ -2674,13 +2804,25 @@ func system_fs_handleOpr(w http.ResponseWriter, r *http.Request) {
 					}
 				}
 
-				//Create a trash directory for this folder
-				trashDir := filepath.ToSlash(filepath.Dir(rsrcFile)) + "/.metadata/.trash/"
-				srcFshAbs.MkdirAll(trashDir, 0755)
-				hidden.HideFile(filepath.Dir(trashDir))
-				hidden.HideFile(trashDir)
-				err = srcFshAbs.Rename(rsrcFile, trashDir+filepath.Base(rsrcFile)+"."+utils.Int64ToString(time.Now().Unix()))
+				//Mirror the folders it was removed from inside the .trash folder of this root
+				removeTime := time.Now().Unix()
+				trashPath, err := srcFshAbs.VirtualPathToRealPath(system_fs_trashSubpathOf(subpath, removeTime), userinfo.Username)
+				for err == nil && srcFshAbs.FileExists(trashPath) {
+					//Another item of the same name was removed from there within this second
+					removeTime++
+					trashPath, err = srcFshAbs.VirtualPathToRealPath(system_fs_trashSubpathOf(subpath, removeTime), userinfo.Username)
+				}
+				trashRoot, rootErr := srcFshAbs.VirtualPathToRealPath(trashFolderSubpath, userinfo.Username)
+				if err != nil || rootErr != nil {
+					utils.SendErrorResponse(w, "Failed to move file to trash")
+					return
+				}
+				srcFshAbs.MkdirAll(path.Dir(filepath.ToSlash(trashPath)), 0755)
+				hidden.HideFile(trashRoot)
+				err = srcFshAbs.Rename(rsrcFile, trashPath)
 				if err != nil {
+					//Do not leave the folders made for it behind
+					system_fs_pruneTrashParents(srcFsh, userinfo.Username, path.Dir(filepath.ToSlash(trashPath)))
 					if srcFsh.RequireBuffer {
 						utils.SendErrorResponse(w, "Incompatible File System Type: Try SHIFT + DELETE to delete file permanently")
 					} else {

+ 278 - 40
src/file_system_trash_test.go

@@ -3,77 +3,315 @@ package main
 import (
 	"os"
 	"path/filepath"
+	"sort"
 	"testing"
 
+	"imuslab.com/arozos/mod/filesystem"
 	"imuslab.com/arozos/mod/filesystem/abstractions/localfs"
 )
 
-/*
-Only entries named <name>.<unix time> right inside a .trash folder are trashed
-items. The .metadata folder a thumbnail of a trashed file leaves behind used
-to show up in the trash as a nameless folder that could not be deleted.
-*/
-func TestIsTrashEntry(t *testing.T) {
+// Only names ending with .<unix time> are trashed item names
+func TestIsTrashName(t *testing.T) {
 	tests := []struct {
 		name string
-		path string
 		want bool
 	}{
-		{"trashed file", "/u/.metadata/.trash/report.pdf.1712345678", true},
-		{"trashed folder", "/u/.metadata/.trash/Photos.1712345678", true},
-		{"trashed hidden file", "/u/.metadata/.trash/.bashrc.1712345678", true},
-		{"thumbnail leftover", "/u/.metadata/.trash/.metadata", false},
-		{"finder leftover", "/u/.metadata/.trash/.DS_Store", false},
-		{"no timestamp", "/u/.metadata/.trash/report.pdf", false},
-		{"no extension", "/u/.metadata/.trash/report", false},
-		{"not in a trash folder", "/u/Desktop/report.pdf.1712345678", false},
-		{"inside a trashed folder", "/u/.metadata/.trash/Photos.1712345678/a.jpg", false},
+		{"report.pdf.1712345678", true},
+		{"Photos.1712345678", true},
+		{".bashrc.1712345678", true},
+		{"backup.2024", true},
+		{".metadata", false},
+		{".DS_Store", false},
+		{"report.pdf", false},
+		{"report", false},
+		{".1712345678", false},
+		{"report.-12", false},
+		{"report.+12", false},
+		{"report.12a", false},
+		{"report.99999999999999999999", false},
+		{"backup.2024~", false},
 	}
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			if got := system_fs_isTrashEntry(tt.path); got != tt.want {
-				t.Errorf("system_fs_isTrashEntry(%q) = %v, want %v", tt.path, got, tt.want)
+			if got := system_fs_isTrashName(tt.name); got != tt.want {
+				t.Errorf("system_fs_isTrashName(%q) = %v, want %v", tt.name, got, tt.want)
 			}
 		})
 	}
 }
 
-func TestRemoveTrashDirIfEmpty(t *testing.T) {
+// Folder names on the way to a trashed item survive the round trip and are never taken for one
+func TestTrashFolderEscaping(t *testing.T) {
+	tests := []struct {
+		name    string
+		escaped string
+	}{
+		{"foo", "foo"},
+		{"foo~", "foo~"},
+		{".metadata", ".metadata"},
+		{"backup.2024", "backup.2024~"},
+		{"backup.2024~", "backup.2024~~"},
+		{"backup.2024~~", "backup.2024~~~"},
+		{"~", "~"},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			escaped := system_fs_escapeTrashFolder(tt.name)
+			if escaped != tt.escaped {
+				t.Errorf("escape(%q) = %q, want %q", tt.name, escaped, tt.escaped)
+			}
+			if system_fs_isTrashName(escaped) {
+				t.Errorf("escaped name %q is taken for a trashed item", escaped)
+			}
+			if back := system_fs_unescapeTrashFolder(escaped); back != tt.name {
+				t.Errorf("unescape(%q) = %q, want %q", escaped, back, tt.name)
+			}
+		})
+	}
+}
+
+func TestTrashSubpathOf(t *testing.T) {
 	tests := []struct {
 		name     string
-		files    []string
-		wantKept bool
+		original string
+		want     string
 	}{
-		{"empty", nil, false},
-		{"only thumbnail leftovers", []string{".metadata/.cache/a.png.1712345678.jpg", ".DS_Store"}, false},
-		{"trashed item left", []string{"a.png.1712345678", ".metadata/.cache/a.png.1712345678.jpg"}, true},
+		{"nested file", "/foo/bar/deleteme.txt", "/.trash/foo/bar/deleteme.txt.1712345678"},
+		{"root file", "/deleteme.txt", "/.trash/deleteme.txt.1712345678"},
+		{"no leading slash", "foo/deleteme.txt", "/.trash/foo/deleteme.txt.1712345678"},
+		{"folder", "/foo/Photos/", "/.trash/foo/Photos.1712345678"},
+		{"folder looking like a trashed item", "/backup.2024/a.txt", "/.trash/backup.2024~/a.txt.1712345678"},
+		{"item looking like a trashed item", "/foo/backup.2024", "/.trash/foo/backup.2024.1712345678"},
+		{"root", "/", ""},
+		{"empty", "", ""},
 	}
 
 	for _, tt := range tests {
 		t.Run(tt.name, func(t *testing.T) {
-			root := t.TempDir()
-			trashDir := filepath.Join(root, ".metadata", ".trash")
-			if err := os.MkdirAll(trashDir, 0755); err != nil {
+			if got := system_fs_trashSubpathOf(tt.original, 1712345678); got != tt.want {
+				t.Errorf("system_fs_trashSubpathOf(%q) = %q, want %q", tt.original, got, tt.want)
+			}
+		})
+	}
+}
+
+func TestOriginalSubpathOf(t *testing.T) {
+	tests := []struct {
+		name         string
+		trashSubpath string
+		wantOriginal string
+		wantOK       bool
+	}{
+		{"nested file", "/.trash/foo/bar/deleteme.txt.1712345678", "/foo/bar/deleteme.txt", true},
+		{"root file", "/.trash/deleteme.txt.1712345678", "/deleteme.txt", true},
+		{"escaped folder", "/.trash/backup.2024~/a.txt.1712345678", "/backup.2024/a.txt", true},
+		{"hidden file", "/.trash/foo/.bashrc.1712345678", "/foo/.bashrc", true},
+		{"folder on the way", "/.trash/foo/bar", "", false},
+		{"inside a trashed folder", "/.trash/Photos.1712345678/a.jpg.1700000000", "", false},
+		{"thumbnail leftover", "/.trash/foo/.metadata/.cache/a.jpg.1712345678.jpg", "", false},
+		{"the trash itself", "/.trash", "", false},
+		{"outside the trash", "/foo/report.pdf.1712345678", "", false},
+		{"escapes the trash", "/.trash/../report.pdf.1712345678", "", false},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			original, timestamp, ok := system_fs_originalSubpathOf(tt.trashSubpath)
+			if ok != tt.wantOK || original != tt.wantOriginal {
+				t.Fatalf("system_fs_originalSubpathOf(%q) = %q, %v, want %q, %v", tt.trashSubpath, original, ok, tt.wantOriginal, tt.wantOK)
+			}
+			if ok && timestamp != 1712345678 {
+				t.Errorf("timestamp = %d, want 1712345678", timestamp)
+			}
+		})
+	}
+}
+
+func TestIsInTrash(t *testing.T) {
+	tests := []struct {
+		subpath string
+		want    bool
+	}{
+		{"/.trash", true},
+		{"/.trash/", true},
+		{"/.trash/foo/a.txt.1712345678", true},
+		{".trash/foo", true},
+		{"/.trashcan", false},
+		{"/foo/.trash", false},
+		{"/", false},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.subpath, func(t *testing.T) {
+			if got := system_fs_isInTrash(tt.subpath); got != tt.want {
+				t.Errorf("system_fs_isInTrash(%q) = %v, want %v", tt.subpath, got, tt.want)
+			}
+		})
+	}
+}
+
+// Write files (and folders for names ending in "/") below root
+func writeTrashFixture(t *testing.T, root string, files []string) {
+	t.Helper()
+	for _, f := range files {
+		p := filepath.Join(root, filepath.FromSlash(f))
+		if f[len(f)-1] == '/' {
+			if err := os.MkdirAll(p, 0755); err != nil {
 				t.Fatalf("mkdir: %v", err)
 			}
-			for _, f := range tt.files {
-				p := filepath.Join(trashDir, f)
-				if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
-					t.Fatalf("mkdir: %v", err)
-				}
-				if err := os.WriteFile(p, []byte("x"), 0644); err != nil {
-					t.Fatalf("write: %v", err)
-				}
+			continue
+		}
+		if err := os.MkdirAll(filepath.Dir(p), 0755); err != nil {
+			t.Fatalf("mkdir: %v", err)
+		}
+		if err := os.WriteFile(p, []byte("x"), 0644); err != nil {
+			t.Fatalf("write: %v", err)
+		}
+	}
+}
+
+// The scan finds trashed items in the root .trash folder only, without looking inside trashed folders
+func TestScanTrashOf(t *testing.T) {
+	root := t.TempDir()
+	writeTrashFixture(t, root, []string{
+		"Desktop/report.pdf.1712345678",
+		".trash/report.pdf.1712345678",
+		".trash/foo/bar/deleteme.txt.1712345678",
+		".trash/foo/Photos.1712345678/a.jpg.1700000000",
+		".trash/foo/.metadata/.cache/deleteme.txt.1712345678.jpg",
+		".trash/backup.2024~/a.txt.1712345678",
+		".trash/empty/",
+		".trash/.DS_Store",
+	})
+
+	fsh := &filesystem.FileSystemHandler{
+		UUID:                  "test",
+		FileSystemAbstraction: localfs.NewLocalFileSystemAbstraction("test", root, "public", false),
+	}
+
+	got := []string{}
+	err := system_fs_scanTrashOf(fsh, "alice", func(item trashItem) error {
+		got = append(got, item.Subpath+" -> "+item.OriginalSubpath)
+		if _, err := os.Stat(item.Realpath); err != nil {
+			t.Errorf("realpath %q of %q: %v", item.Realpath, item.Subpath, err)
+		}
+		return nil
+	})
+	if err != nil {
+		t.Fatalf("scan: %v", err)
+	}
+	sort.Strings(got)
+
+	want := []string{
+		"/.trash/backup.2024~/a.txt.1712345678 -> /backup.2024/a.txt",
+		"/.trash/foo/Photos.1712345678 -> /foo/Photos",
+		"/.trash/foo/bar/deleteme.txt.1712345678 -> /foo/bar/deleteme.txt",
+		"/.trash/report.pdf.1712345678 -> /report.pdf",
+	}
+	if len(got) != len(want) {
+		t.Fatalf("scan found %v, want %v", got, want)
+	}
+	for i := range want {
+		if got[i] != want[i] {
+			t.Errorf("item %d = %q, want %q", i, got[i], want[i])
+		}
+	}
+}
+
+func TestScanTrashOfWithoutTrash(t *testing.T) {
+	fsh := &filesystem.FileSystemHandler{
+		UUID:                  "test",
+		FileSystemAbstraction: localfs.NewLocalFileSystemAbstraction("test", t.TempDir(), "public", false),
+	}
+	calls := 0
+	err := system_fs_scanTrashOf(fsh, "alice", func(item trashItem) error {
+		calls++
+		return nil
+	})
+	if err != nil || calls != 0 {
+		t.Errorf("scan of a root without .trash = %d items, %v; want none", calls, err)
+	}
+}
+
+// Removing a trashed item drops the folders it leaves empty, up to .trash itself
+func TestPruneTrashParents(t *testing.T) {
+	tests := []struct {
+		name     string
+		files    []string
+		removed  string
+		wantGone []string
+		wantKept []string
+	}{
+		{
+			name:     "last item",
+			files:    []string{".trash/foo/bar/a.txt.1712345678", ".trash/foo/bar/.metadata/.cache/a.txt.1712345678.jpg", ".trash/.DS_Store"},
+			removed:  ".trash/foo/bar/a.txt.1712345678",
+			wantGone: []string{".trash"},
+		},
+		{
+			name:     "sibling folder left",
+			files:    []string{".trash/foo/bar/a.txt.1712345678", ".trash/foo/baz/b.txt.1712345678"},
+			removed:  ".trash/foo/bar/a.txt.1712345678",
+			wantGone: []string{".trash/foo/bar"},
+			wantKept: []string{".trash/foo/baz/b.txt.1712345678"},
+		},
+		{
+			name:     "item higher up left",
+			files:    []string{".trash/foo/bar/a.txt.1712345678", ".trash/c.txt.1712345678"},
+			removed:  ".trash/foo/bar/a.txt.1712345678",
+			wantGone: []string{".trash/foo"},
+			wantKept: []string{".trash/c.txt.1712345678"},
+		},
+		{
+			name:     "trashed .metadata folder content kept",
+			files:    []string{".trash/foo/bar/a.txt.1712345678", ".trash/foo/.metadata/x.1712345678"},
+			removed:  ".trash/foo/bar/a.txt.1712345678",
+			wantGone: []string{".trash/foo/bar"},
+			wantKept: []string{".trash/foo/.metadata/x.1712345678"},
+		},
+	}
+
+	for _, tt := range tests {
+		t.Run(tt.name, func(t *testing.T) {
+			root := t.TempDir()
+			writeTrashFixture(t, root, tt.files)
+			fsh := &filesystem.FileSystemHandler{
+				UUID:                  "test",
+				FileSystemAbstraction: localfs.NewLocalFileSystemAbstraction("test", root, "public", false),
 			}
 
-			fshAbs := localfs.NewLocalFileSystemAbstraction("test", root, "user", false)
-			system_fs_removeTrashDirIfEmpty(fshAbs, trashDir)
+			removed := filepath.Join(root, filepath.FromSlash(tt.removed))
+			if err := os.RemoveAll(removed); err != nil {
+				t.Fatalf("remove: %v", err)
+			}
+			system_fs_pruneTrashParents(fsh, "alice", filepath.ToSlash(filepath.Dir(removed)))
 
-			_, err := os.Stat(trashDir)
-			if kept := err == nil; kept != tt.wantKept {
-				t.Errorf("trash folder kept = %v, want %v", kept, tt.wantKept)
+			for _, p := range tt.wantGone {
+				if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(p))); err == nil {
+					t.Errorf("%s still exists", p)
+				}
+			}
+			for _, p := range tt.wantKept {
+				if _, err := os.Stat(filepath.Join(root, filepath.FromSlash(p))); err != nil {
+					t.Errorf("%s was removed: %v", p, err)
+				}
 			}
 		})
 	}
 }
+
+// Pruning never walks out of the .trash folder
+func TestPruneTrashParentsStaysInTrash(t *testing.T) {
+	root := t.TempDir()
+	writeTrashFixture(t, root, []string{"foo/empty/"})
+	fsh := &filesystem.FileSystemHandler{
+		UUID:                  "test",
+		FileSystemAbstraction: localfs.NewLocalFileSystemAbstraction("test", root, "public", false),
+	}
+	system_fs_pruneTrashParents(fsh, "alice", filepath.ToSlash(filepath.Join(root, "foo", "empty")))
+	if _, err := os.Stat(filepath.Join(root, "foo", "empty")); err != nil {
+		t.Errorf("folder outside the trash was removed: %v", err)
+	}
+}

+ 53 - 14
src/web/Drive/backend/storage.agi

@@ -9,8 +9,9 @@
 
     The visible tree is walked breadth first with filelib.readdir, which
     returns sizes in the same call. readdir hides dot folders, so for every
-    folder the hidden ".metadata" folder (trash, version history, thumbnail
-    caches) is walked separately.
+    folder the hidden ".metadata" folder (version history, thumbnail caches)
+    is walked separately, and the trash, which lives in a single ".trash"
+    folder at the root, is walked on its own by walkTrash.
 */
 
 requirelib("filelib");
@@ -125,6 +126,44 @@ function walkDrive(root, visit) {
     return truncated;
 }
 
+/*
+    walkTrash(visit) calls visit(vpath, entry, origin) for every file in the
+    trash. A trashed item keeps the folders it was removed from:
+
+        user:/.trash/foo/bar/deleteme.txt.<unix time>
+
+    entry is the trashed item the file belongs to (itself, or the trashed
+    folder it is inside) and origin the folder it was removed from. A folder
+    on the way whose name looks like a trashed item carries an extra "~".
+*/
+var TRASH_ROOT = "user:/.trash/";
+var TRASH_NAME = /^.+\.\d+$/;
+
+function walkTrash(visit) {
+    var files = [];
+    try {
+        if (filelib.fileExists(TRASH_ROOT) && filelib.isDir(TRASH_ROOT)) {
+            files = filelib.walk(TRASH_ROOT, "file");
+        }
+    } catch (e) {
+        files = [];
+    }
+    for (var i = 0; i < files.length; i++) {
+        var segments = String(files[i]).substring(TRASH_ROOT.length).split("/");
+        for (var k = 0; k < segments.length; k++) {
+            if (TRASH_NAME.test(segments[k])) {
+                break;
+            }
+        }
+        if (k == segments.length) {
+            //A leftover, such as a thumbnail cache
+            continue;
+        }
+        var folders = segments.slice(0, k).map(function (s) { return s.replace(/^(.+\.\d+~*)~$/, "$1"); });
+        visit(files[i], TRASH_ROOT + segments.slice(0, k + 1).join("/"), "user:/" + folders.join("/"));
+    }
+}
+
 function sizeOf(path) {
     try {
         return filelib.filesize(path);
@@ -175,18 +214,7 @@ if (opr == "analyze") {
         meta: function (path, dir) {
             var rel = path.substring(dir.length + ".metadata/".length);
             var size = sizeOf(path);
-            if (rel.indexOf(".trash/") == 0) {
-                var entry = rel.substring(7).split("/")[0];
-                trash.size += size;
-                if (!trash.entries[dir + entry]) {
-                    trash.entries[dir + entry] = true;
-                    trash.count++;
-                }
-                //Trashed names end with .<unix time>
-                var shown = entry.replace(/\.\d+$/, "");
-                //A file inside a trashed folder shows its own name
-                push({ name: rel.indexOf("/", 7) > 0 ? basename(path) : shown, path: path, size: size, mtime: 0, where: "trash", origin: dir });
-            } else if (rel.indexOf(".localver/") == 0) {
+            if (rel.indexOf(".localver/") == 0) {
                 versions.size += size;
                 versions.count++;
                 var histid = rel.substring(10).split("/")[0];
@@ -200,6 +228,17 @@ if (opr == "analyze") {
             }
         }
     });
+    walkTrash(function (path, entry, origin) {
+        var size = sizeOf(path);
+        trash.size += size;
+        if (!trash.entries[entry]) {
+            trash.entries[entry] = true;
+            trash.count++;
+        }
+        //Trashed names end with .<unix time>; a file inside a trashed folder shows its own name
+        var name = path == entry ? basename(entry).replace(/\.\d+$/, "") : basename(path);
+        push({ name: name, path: path, size: size, mtime: 0, where: "trash", origin: origin });
+    });
     delete trash.entries;
 
     var disks = [];

+ 5 - 4
src/web/SystemAO/file_system/js/explorer/trash.js

@@ -44,10 +44,11 @@ let trashKindFilter = "all";    //all | file | folder, set by the card layout ch
 /*
     Scan state
 
-    A bin on a large NAS takes a while to list: the server walks every folder
-    of every mounted root looking for .trash directories. Rather than hold a
-    blank screen for all of it, the scan is streamed and each file is drawn as
-    the server finds it - see the loading section below.
+    Every mounted root keeps its trash in one .trash folder at its top, so a
+    scan only reads those folders, but a slow network drive still takes a
+    while. Rather than hold a blank screen for all of it, the scan is streamed
+    and each file is drawn as the server finds it - see the loading section
+    below.
 */
 let trashScanning = false;      //A scan is in flight
 let trashScanGeneration = 0;    //Bumped per scan, so a stale one draws nothing