Whamcloud - gitweb
AOSP: ext2simg: fix same_file() with symlinks
authorEric Biggers <ebiggers@google.com>
Thu, 23 Mar 2023 02:52:09 +0000 (02:52 +0000)
committerTheodore Ts'o <tytso@mit.edu>
Mon, 20 May 2024 17:07:50 +0000 (13:07 -0400)
Fix same_file() to use stat() instead of lstat() when checking the
paths, so that symlinks are dereferenced.  This is needed to be
consistent with how the paths are actually accessed later.  Otherwise,
not all cases where the input and output file are the same are detected.

Also just use the stat() result to check whether the output file exists,
instead of using a separate call to access().

Fixes: db6f320912cf ("AOSP: android: add the ext2simg tool")
Change-Id: Ie36981f9dbc19494732f518488a75fb92c0f0343
Signed-off-by: Eric Biggers <ebiggers@google.com>
From AOSP commit: 08c122f12fc231029a74c24b969e337203c7b6e2

contrib/android/ext2simg.c

index d1b5dc4..2bf76b9 100644 (file)
@@ -188,13 +188,13 @@ static bool same_file(const char *in, const char *out)
 {
        struct stat st1, st2;
 
-       if (access(out, F_OK) == -1)
-               return false;
-
-       if (lstat(in, &st1) == -1)
+       if (stat(in, &st1) == -1)
                ext2fs_fatal(errno, "stat %s\n", in);
-       if (lstat(out, &st2) == -1)
+       if (stat(out, &st2) == -1) {
+               if (errno == ENOENT)
+                       return false;
                ext2fs_fatal(errno, "stat %s\n", out);
+       }
        return st1.st_dev == st2.st_dev && st1.st_ino == st2.st_ino;
 }