mirror of
				https://github.com/StuffAnThings/qbit_manage.git
				synced 2025-11-04 12:07:19 +08:00 
			
		
		
		
	Merge pull request #289 from buthed010203/patch-1
Fix remove_orphans crashing in certain cases
This commit is contained in:
		
						commit
						7182b2a341
					
				
					 1 changed files with 16 additions and 13 deletions
				
			
		| 
						 | 
				
			
			@ -307,19 +307,22 @@ def copy_files(src, dest):
 | 
			
		|||
def remove_empty_directories(pathlib_root_dir, pattern):
 | 
			
		||||
    """Remove empty directories recursively."""
 | 
			
		||||
    pathlib_root_dir = Path(pathlib_root_dir)
 | 
			
		||||
    # list all directories recursively and sort them by path,
 | 
			
		||||
    # longest first
 | 
			
		||||
    longest = sorted(
 | 
			
		||||
        pathlib_root_dir.glob(pattern),
 | 
			
		||||
        key=lambda p: len(str(p)),
 | 
			
		||||
        reverse=True,
 | 
			
		||||
    )
 | 
			
		||||
    longest.append(pathlib_root_dir)
 | 
			
		||||
    for pdir in longest:
 | 
			
		||||
        try:
 | 
			
		||||
            pdir.rmdir()  # remove directory if empty
 | 
			
		||||
        except OSError:
 | 
			
		||||
            continue  # catch and continue if non-empty
 | 
			
		||||
    try:
 | 
			
		||||
        # list all directories recursively and sort them by path,
 | 
			
		||||
        # longest first
 | 
			
		||||
        longest = sorted(
 | 
			
		||||
            pathlib_root_dir.glob(pattern),
 | 
			
		||||
            key=lambda p: len(str(p)),
 | 
			
		||||
            reverse=True,
 | 
			
		||||
        )
 | 
			
		||||
        longest.append(pathlib_root_dir)  # delete the folder itself if it's empty
 | 
			
		||||
        for pdir in longest:
 | 
			
		||||
            try:
 | 
			
		||||
                pdir.rmdir()  # remove directory if empty
 | 
			
		||||
            except (FileNotFoundError, OSError):
 | 
			
		||||
                continue  # catch and continue if non-empty, folders within could already be deleted if run in parallel
 | 
			
		||||
    except FileNotFoundError:
 | 
			
		||||
        pass  # if this is being run in parallel, pathlib_root_dir could already be deleted
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
def nohardlink(file, notify):
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
	Add table
		
		Reference in a new issue