diff --git a/anet.c b/anet.c index 05204214d..7e24dc737 100644 --- a/anet.c +++ b/anet.c @@ -51,7 +51,6 @@ * POSSIBILITY OF SUCH DAMAGE. */ -#include #ifdef _WIN32 # include # include @@ -70,6 +69,7 @@ # include # define p_setsockopt_optval_t void* #endif +#include #include #include #include diff --git a/dump1090.h b/dump1090.h index 3feaf97d4..167921058 100644 --- a/dump1090.h +++ b/dump1090.h @@ -61,6 +61,13 @@ // ============================= Include files ========================== +#ifdef _WIN32 +//WinSocks2 must be included before Windows.h +# include +#else +# include +#endif + #include #include #include @@ -76,11 +83,6 @@ #include #include #include -#ifdef _WIN32 -# include -#else -# include -#endif #include #include #include diff --git a/net_io.c b/net_io.c index b68e0309a..4a82b92a8 100644 --- a/net_io.c +++ b/net_io.c @@ -2105,6 +2105,30 @@ static bool UtilMoveFile(const char *fromPath, const char *toPath) #endif } +static bool UtilCreateWritableTempFile(char* outPathBuffer, const char* prefix, const char* path) +{ +#ifdef _WIN32 + if (GetTempFileNameA(path, prefix, 0, outPathBuffer) == 0) { + return false; + } +#else + int unix_fd = 0; + mode_t mask = 0; + + snprintf(outPathBuffer, PATH_MAX, "%s/%sXXXXXX", path, prefix); + unix_fd = mkstemp(outPathBuffer); + if (unix_fd < 0) { + return NULL; + } + + mask = umask(0); + umask(mask); + fchmod(unix_fd, 0644 & ~mask); +#endif + + return true; +} + // Write JSON to file void writeJsonToFile(const char *file, char * (*generator) (const char *,int*)) { @@ -2117,33 +2141,13 @@ void writeJsonToFile(const char *file, char * (*generator) (const char *,int*)) if (!Modes.json_dir) return; -#ifdef _WIN32 - if (GetTempFileNameA(Modes.json_dir, file, 0, tmppath) == 0) { + if(!UtilCreateWritableTempFile(tmppath, file, Modes.json_dir) || + (fd = fopen(tmppath, "wb")) == NULL) { + //TODO: implement a function to get the last error on windows and linux as a string ratelimitWriteError("failed to create %s (while updating %s/%s): %s", tmppath, Modes.json_dir, file, strerror(errno)); - return; } - fd = fopen(tmppath, "wb"); -#else - int unix_fd = 0; - mode_t mask = 0; - snprintf(tmppath, PATH_MAX, "%s/%sXXXXXX", Modes.json_dir, file); - tmppath[PATH_MAX - 1] = 0; - unix_fd = mkstemp(tmppath); - if (unix_fd < 0) { - ratelimitWriteError("failed to create %s (while updating %s/%s): %s", tmppath, Modes.json_dir, file, strerror(errno)); - return; - } - - mask = umask(0); - umask(mask); - fchmod(unix_fd, 0644 & ~mask); - fd = fdopen(unix_fd, "wb"); -#endif - snprintf(pathbuf, PATH_MAX, "/data/%s", file); - pathbuf[PATH_MAX - 1] = 0; - content = generator(pathbuf, (int*)&len); if (fwrite(content, 1, len, fd) != len || fclose(fd) != 0) @@ -2156,8 +2160,6 @@ void writeJsonToFile(const char *file, char * (*generator) (const char *,int*)) } snprintf(pathbuf, PATH_MAX, "%s/%s", Modes.json_dir, file); - pathbuf[PATH_MAX - 1] = 0; - if(!UtilMoveFile(tmppath, pathbuf)){ ratelimitWriteError("failed to rename %s to %s: %s", tmppath, pathbuf, strerror(errno)); }