summaryrefslogtreecommitdiffstats
path: root/helpers
diff options
context:
space:
mode:
authorPatrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 16:09:27 (EDT)
committer Patrick McDermott <patrick.mcdermott@libiquity.com>2023-07-05 16:43:13 (EDT)
commitd5ce0abc3ece376f2dc6571310d6944fc71bcb0b (patch)
tree8b09b16d25f0f0005bb0b823a9b861a8a5fd13cd /helpers
parent69bf47e45df055d3b8a818b2b8b7657767c9a171 (diff)
mknod: Fix specials file writing
Diffstat (limited to 'helpers')
-rw-r--r--helpers/mknod.c38
1 files changed, 19 insertions, 19 deletions
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;