From d553ae5bd39562bc02f791d9c353a530ca1c8a36 Mon Sep 17 00:00:00 2001 From: Eric Biggers Date: Thu, 23 Mar 2023 02:52:09 +0000 Subject: [PATCH] AOSP: ext2simg: fix same_file() with symlinks 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 From AOSP commit: 08c122f12fc231029a74c24b969e337203c7b6e2 --- contrib/android/ext2simg.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/contrib/android/ext2simg.c b/contrib/android/ext2simg.c index d1b5dc4..2bf76b9 100644 --- a/contrib/android/ext2simg.c +++ b/contrib/android/ext2simg.c @@ -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; } -- 1.8.3.1