diff options
author | Daniel Jones <admin@danieljon.es> | 2019-12-26 11:51:55 +1030 |
---|---|---|
committer | Daniel Jones <admin@danieljon.es> | 2019-12-26 11:51:55 +1030 |
commit | 7d224879505009b62849b7c90a085f5b17375c27 (patch) | |
tree | 8dde8393e6f83e4fbf685fd30d1b015b4e1ebba9 | |
parent | ba4c4ecd23386a242511953d5557f8ba24a93606 (diff) | |
download | websitegenerator-7d224879505009b62849b7c90a085f5b17375c27.tar.gz websitegenerator-7d224879505009b62849b7c90a085f5b17375c27.zip |
don't use tempfile()
instead we create a temp file in the cwd and rename it to the desired output file name and location
-rw-r--r-- | pages.c | 17 |
1 files changed, 5 insertions, 12 deletions
@@ -155,7 +155,7 @@ deletebytes(const char *file, long offset, size_t bytes) return 0; } - FILE *out = tmpfile(); + FILE *out = fopen("tmp.tmp", "w"); if (!out) { fprintf(stderr, "unable to open temporary file, unrecoverable failure\n"); @@ -178,7 +178,7 @@ deletebytes(const char *file, long offset, size_t bytes) curpos++; } - /* delete 'filename' and move our temp file 'out' to take its place (by naively rewriting the file..) */ + /* delete 'filename' and move our temp file 'out' to take its place */ fclose(in); if (remove(filename) == -1) { @@ -188,22 +188,15 @@ deletebytes(const char *file, long offset, size_t bytes) return 0; } - /* FIXME: do something better than rewriting the file, move it somehow */ - FILE *new = fopen(filename, "w"); - if (!new) + if (rename("tmp.tmp", filename) == -1) { - fprintf(stderr, "unable to open file '%s' for writing new data, unrecoverable failure\n", filename); + + fprintf(stderr, "unable to rename tmp file to '%s', unrecoverable failure\n", filename); fclose(out); return 0; } - fseek(out, 0, SEEK_SET); - while ((c = fgetc(out)) != EOF) - { - fputc(c, new); - } fclose(out); - fclose(new); return 1; } |