Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
snake-4 committed Aug 4, 2021
1 parent 0421b56 commit 55ad81e
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 31 deletions.
2 changes: 1 addition & 1 deletion anet.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
* POSSIBILITY OF SUCH DAMAGE.
*/

#include <sys/types.h>
#ifdef _WIN32
# include <winsock2.h>
# include <ws2def.h>
Expand All @@ -70,6 +69,7 @@
# include <netdb.h>
# define p_setsockopt_optval_t void*
#endif
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <string.h>
Expand Down
12 changes: 7 additions & 5 deletions dump1090.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,13 @@

// ============================= Include files ==========================

#ifdef _WIN32
//WinSocks2 must be included before Windows.h
# include <winsock2.h>
#else
# include <sys/ioctl.h>
#endif

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
Expand All @@ -76,11 +83,6 @@
#include <fcntl.h>
#include <ctype.h>
#include <sys/stat.h>
#ifdef _WIN32
# include <winsock.h>
#else
# include <sys/ioctl.h>
#endif
#include <string.h>
#include <time.h>
#include <limits.h>
Expand Down
52 changes: 27 additions & 25 deletions net_io.c
Original file line number Diff line number Diff line change
Expand Up @@ -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*))
{
Expand All @@ -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)
Expand All @@ -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));
}
Expand Down

0 comments on commit 55ad81e

Please sign in to comment.