diff --git a/app/model_manager.py b/app/model_manager.py index ab36bca74..5688cbd0b 100644 --- a/app/model_manager.py +++ b/app/model_manager.py @@ -117,10 +117,27 @@ class ModelFileManager: # TODO use settings include_hidden_files = False + # Check if we should skip following symlinks + from comfy.cli_args import args + skip_symlinks = getattr(args, 'skip_symlink_scan', False) + followlinks = not skip_symlinks + result: list[str] = [] dirs: dict[str, float] = {} - for dirpath, subdirs, filenames in os.walk(directory, followlinks=True, topdown=True): + for dirpath, subdirs, filenames in os.walk(directory, followlinks=followlinks, topdown=True): + # If skipping symlinks, filter out symlinked directories + if skip_symlinks: + filtered_subdirs = [] + for d in subdirs: + subdir_path = os.path.join(dirpath, d) + # Check if it's a symlink + if os.path.islink(subdir_path): + logging.debug(f"Skipping symlink: {subdir_path}") + continue + filtered_subdirs.append(d) + subdirs[:] = filtered_subdirs + subdirs[:] = [d for d in subdirs if d not in excluded_dir_names] if not include_hidden_files: subdirs[:] = [d for d in subdirs if not d.startswith(".")] diff --git a/comfy/cli_args.py b/comfy/cli_args.py index 3d5bc7c90..f3c880114 100644 --- a/comfy/cli_args.py +++ b/comfy/cli_args.py @@ -216,6 +216,8 @@ database_default_path = os.path.abspath( ) parser.add_argument("--database-url", type=str, default=f"sqlite:///{database_default_path}", help="Specify the database URL, e.g. for an in-memory database you can use 'sqlite:///:memory:'.") +parser.add_argument("--skip-symlink-scan", action="store_true", help="Skip following symlinks when scanning directories. Useful when symlinks point to network paths with many files. Specifying a filename directly will still work.") + if comfy.options.args_parsing: args = parser.parse_args() else: diff --git a/folder_paths.py b/folder_paths.py index f110d832b..d4dde894e 100644 --- a/folder_paths.py +++ b/folder_paths.py @@ -240,6 +240,10 @@ def recursive_search(directory: str, excluded_dir_names: list[str] | None=None) if excluded_dir_names is None: excluded_dir_names = [] + # Check if we should skip following symlinks + skip_symlinks = getattr(args, 'skip_symlink_scan', False) + followlinks = not skip_symlinks + result = [] dirs = {} @@ -254,7 +258,19 @@ def recursive_search(directory: str, excluded_dir_names: list[str] | None=None) subdirs: list[str] filenames: list[str] - for dirpath, subdirs, filenames in os.walk(directory, followlinks=True, topdown=True): + for dirpath, subdirs, filenames in os.walk(directory, followlinks=followlinks, topdown=True): + # If skipping symlinks, filter out symlinked directories + if skip_symlinks: + filtered_subdirs = [] + for d in subdirs: + subdir_path = os.path.join(dirpath, d) + # Check if it's a symlink + if os.path.islink(subdir_path): + logging.debug(f"Skipping symlink: {subdir_path}") + continue + filtered_subdirs.append(d) + subdirs[:] = filtered_subdirs + subdirs[:] = [d for d in subdirs if d not in excluded_dir_names] for file_name in filenames: try: