From d5ce0abc3ece376f2dc6571310d6944fc71bcb0b Mon Sep 17 00:00:00 2001 From: Patrick McDermott Date: Wed, 05 Jul 2023 16:09:27 -0400 Subject: mknod: Fix specials file writing --- (limited to 'helpers') diff --git a/helpers/mknod.c b/helpers/mknod.c index 23683d8..e9e9737 100644 --- a/helpers/mknod.c +++ b/helpers/mknod.c @@ -251,51 +251,51 @@ main(int argc, char *argv[]) return EXIT_FAILURE; opened: - specials_file = fdopen(specials_fd, "a"); + close(specials_fd); + + specials_file = fopen(specials_file_name, "a"); if (specials_file == NULL) { _err(_("Failed to open specials file stream")); - close(specials_fd); + unlink(specials_lock_name); free(specials_file_name); unlink(name); return EXIT_FAILURE; } - /* If "specials" doesn't exist, write header. */ - if (stat(specials_file_name, &stat_buf) != 0) { - if (errno != ENOENT) { - _err(_("Failed to stat specials file")); - fclose(specials_file); - free(specials_file_name); - unlink(name); - return EXIT_FAILURE; - } + /* If "specials" is empty, write header. */ + if (fstat(fileno(specials_file), &stat_buf) != 0) { + _err(_("Failed to stat specials file")); + fclose(specials_file); + unlink(specials_lock_name); + free(specials_file_name); + unlink(name); + return EXIT_FAILURE; + } + if (stat_buf.st_size == 0) { if (fputs("version=1\n", specials_file) < 0 || fputs("type major minor name\n", specials_file) < 0) { _err(_("Failed to write specials file")); fclose(specials_file); + unlink(specials_lock_name); free(specials_file_name); unlink(name); return EXIT_FAILURE; } } + if (fprintf(specials_file, "%c %8ld %8ld %s\n", type, major, minor, name) < 0) { _err(_("Failed to write specials file")); fclose(specials_file); - free(specials_file_name); - unlink(name); - return EXIT_FAILURE; - } - fclose(specials_file); - - if (rename(specials_lock_name, specials_file_name) != 0) { - _err(_("Failed to move specials file")); + unlink(specials_lock_name); free(specials_file_name); unlink(name); return EXIT_FAILURE; } + fclose(specials_file); + unlink(specials_lock_name); free(specials_file_name); return EXIT_SUCCESS; -- cgit v0.9.1