summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPali Rohár <pali.rohar@gmail.com>2017-06-17 23:10:36 +0200
committerPali Rohár <pali.rohar@gmail.com>2017-06-17 23:10:36 +0200
commit897919a70fa030399e52792fa602e53590744e04 (patch)
tree820e1fe62ab218f559808bedff78cbb3ebdeb1a2
parent76b18b9d39211dbb027b59e01570b9a384fb2b2c (diff)
download0xFFFF-897919a70fa030399e52792fa602e53590744e04.tar.bz2
main: Fix race condition between calling stat and rename
-rw-r--r--src/main.c16
1 files changed, 11 insertions, 5 deletions
diff --git a/src/main.c b/src/main.c
index 40f367a..d503ab6 100644
--- a/src/main.c
+++ b/src/main.c
@@ -1319,14 +1319,12 @@ int main(int argc, char **argv) {
for ( i = 0; i < IMAGE_COUNT; ++i ) {
- struct stat st;
+ int rename_ret;
+ int rename_errno;
if ( ! image_tmp_name(i) )
continue;
- if ( stat(image_tmp_name(i), &st) != 0 )
- continue;
-
switch ( i ) {
case IMAGE_2ND:
case IMAGE_XLOADER:
@@ -1357,10 +1355,18 @@ int main(int argc, char **argv) {
buf[0] = 0;
snprintf(buf, sizeof(buf), "%s-%s:%hd_%s", image_type_to_string(i), device_to_string(dev->detected_device), dev->detected_hwrev, ptr);
+
+ rename_ret = rename(image_tmp_name(i), buf);
+ rename_errno = errno;
+
+ if ( rename_ret < 0 && rename_errno == ENOENT )
+ continue;
+
printf("Renaming %s image file to %s...\n", image_type_to_string(i), buf);
- if ( rename(image_tmp_name(i), buf) < 0 ) {
+ if ( rename_ret < 0 ) {
+ errno = rename_errno;
ERROR_INFO("Renaming failed");
buf[0] = 0;