mirror of
https://github.com/gbdev/rgbds.git
synced 2025-11-21 18:52:07 +00:00
Merge pull request #451 from rednex/atomic_output
Make RGBASM overwrite output files atomically
This commit is contained in:
@@ -16,6 +16,23 @@
|
|||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <libgen.h>
|
||||||
|
|
||||||
|
#ifdef _WIN32
|
||||||
|
#include <windows.h>
|
||||||
|
#include <winbase.h>
|
||||||
|
/*
|
||||||
|
* The semantics of `rename` on Windows differ from POSIX in that it errors out
|
||||||
|
* when the target file exists, instead of overwriting it.
|
||||||
|
* Thus, shim `rename` with similar semantics (note that the return value of
|
||||||
|
* `MoveFileExA` is inverted, and it uses `GetLastError` instead of `errno`)
|
||||||
|
*/
|
||||||
|
#define rename(oldname, newname) \
|
||||||
|
MoveFileExA(oldname, newname, MOVEFILE_WRITE_THROUGH | \
|
||||||
|
MOVEFILE_REPLACE_EXISTING) \
|
||||||
|
? 0 : (errno = GetLastError(), -1)
|
||||||
|
#endif
|
||||||
|
|
||||||
#include "asm/asm.h"
|
#include "asm/asm.h"
|
||||||
#include "asm/charmap.h"
|
#include "asm/charmap.h"
|
||||||
@@ -531,13 +548,25 @@ static void checksectionoverflow(uint32_t delta_size)
|
|||||||
*/
|
*/
|
||||||
void out_WriteObject(void)
|
void out_WriteObject(void)
|
||||||
{
|
{
|
||||||
|
/* Write to a temporary file in the target's folder */
|
||||||
|
char *objectNameCopy = strdup(tzObjectname);
|
||||||
|
char const *dirPath = dirname(objectNameCopy);
|
||||||
|
char tmpFileName[strlen(dirPath) + 1 + 16 + 1];
|
||||||
|
|
||||||
|
sprintf(tmpFileName, "%s/rgbasm_tmpXXXXXX", dirPath);
|
||||||
|
free(objectNameCopy);
|
||||||
|
int fd = mkstemp(tmpFileName);
|
||||||
|
|
||||||
FILE *f;
|
FILE *f;
|
||||||
struct PatchSymbol *pSym;
|
struct PatchSymbol *pSym;
|
||||||
struct Section *pSect;
|
struct Section *pSect;
|
||||||
|
|
||||||
|
if (fd == -1)
|
||||||
|
err(1, "Couldn't create temporary file");
|
||||||
|
|
||||||
addexports();
|
addexports();
|
||||||
|
|
||||||
f = fopen(tzObjectname, "wb");
|
f = fdopen(fd, "wb");
|
||||||
if (f == NULL)
|
if (f == NULL)
|
||||||
fatalerror("Couldn't write file '%s'\n", tzObjectname);
|
fatalerror("Couldn't write file '%s'\n", tzObjectname);
|
||||||
|
|
||||||
@@ -560,6 +589,11 @@ void out_WriteObject(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
fclose(f);
|
fclose(f);
|
||||||
|
close(fd);
|
||||||
|
|
||||||
|
if (rename(tmpFileName, tzObjectname) != 0)
|
||||||
|
err(1, "Couldn't create object file (temp file kept as %s)",
|
||||||
|
tmpFileName);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
Reference in New Issue
Block a user