]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
xfs_protofile: make nondirectory arguments actually work
authorDarrick J. Wong <djwong@kernel.org>
Tue, 9 Jun 2026 16:14:46 +0000 (09:14 -0700)
committerDarrick J. Wong <djwong@kernel.org>
Thu, 2 Jul 2026 19:38:24 +0000 (12:38 -0700)
Codex points out that xfs_protofile fails if you pass it paths to
non-directories.  It's supposed to just copy them into the root
directory, but we don't actually do that.

Cc: <linux-xfs@vger.kernel.org> # v6.13.0
Fixes: 6aace700b7b82d ("mkfs: add a utility to generate protofiles")
Signed-off-by: "Darrick J. Wong" <djwong@kernel.org>
Reviewed-by: Christoph Hellwig <hch@lst.de>
mkfs/xfs_protofile.py.in

index 40d06ddb44aa300b6801ec451ffe915e288969f6..00010fa7b3b0418269f060c74d9c05784c157bcc 100644 (file)
@@ -66,46 +66,56 @@ def max_fname_len(s1):
        '''Return the length of the longest string in s1.'''
        ret = 0
        for s in s1:
+               ss = os.path.basename(s)
                if len(s) > ret:
                        ret = len(s)
        return ret
 
+def list_maybe_dir(path, depth):
+       try:
+               for fname in os.listdir(path):
+                       yield os.path.join(path, fname)
+       except NotADirectoryError as e:
+               if depth == 1:
+                       yield path
+                       return
+               raise e
+
 def walk_tree(path, depth):
        '''Walk the directory tree rooted by path.'''
        dirs = []
        files = []
 
-       for fname in os.listdir(path):
-               fullpath = os.path.join(path, fname)
+       for fullpath in list_maybe_dir(path, depth):
                sb = os.lstat(fullpath)
 
                if stat.S_ISDIR(sb.st_mode):
-                       dirs.append(fname)
+                       dirs.append(fullpath)
                        continue
                elif stat.S_ISSOCK(sb.st_mode):
                        continue
                else:
-                       files.append(fname)
+                       files.append(fullpath)
 
-       for fname in files:
-               if ' ' in fname:
+       for fullpath in files:
+               if ' ' in os.path.basename(fullpath):
                        msg = _("Spaces not allowed in file names.")
-                       raise ValueError(f'{fname}: {msg}')
-       for fname in dirs:
-               if ' ' in fname:
+                       raise ValueError(f'{fullpath}: {msg}')
+       for fullpath in dirs:
+               if ' ' in os.path.basename(fullpath):
                        msg = _("Spaces not allowed in subdirectory names.")
-                       raise Exception(f'{fname}: {msg}')
+                       raise Exception(f'{fullpath}: {msg}')
 
        fname_width = max_fname_len(files)
-       for fname in files:
-               fullpath = os.path.join(path, fname)
+       for fullpath in files:
+               fname = os.path.basename(fullpath)
                sb = os.lstat(fullpath)
                extra = stat_to_extra(sb, fullpath)
                print('%*s%-*s %s%s' % (depth, ' ', fname_width, fname, \
                                stat_to_str(sb), extra))
 
-       for fname in dirs:
-               fullpath = os.path.join(path, fname)
+       for fullpath in dirs:
+               fname = os.path.basename(fullpath)
                sb = os.lstat(fullpath)
                extra = stat_to_extra(sb, fullpath)
                print('%*s%s %s' % (depth, ' ', fname, \
@@ -138,7 +148,7 @@ def main():
                # Copy the first argument's stat to the rootdir
                statbuf = os.stat(args.paths[0])
                if not stat.S_ISDIR(statbuf.st_mode):
-                       raise NotADirectoryError(path)
+                       raise NotADirectoryError(args.paths[0])
                print(stat_to_str(statbuf))
 
                # All files under each path go in the root dir, recursively