Skip to content

Commit 55ad81e

Browse files
committed
fix
1 parent 0421b56 commit 55ad81e

File tree

3 files changed

+35
-31
lines changed

3 files changed

+35
-31
lines changed

anet.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
* POSSIBILITY OF SUCH DAMAGE.
5252
*/
5353

54-
#include <sys/types.h>
5554
#ifdef _WIN32
5655
# include <winsock2.h>
5756
# include <ws2def.h>
@@ -70,6 +69,7 @@
7069
# include <netdb.h>
7170
# define p_setsockopt_optval_t void*
7271
#endif
72+
#include <sys/types.h>
7373
#include <sys/stat.h>
7474
#include <fcntl.h>
7575
#include <string.h>

dump1090.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@
6161

6262
// ============================= Include files ==========================
6363

64+
#ifdef _WIN32
65+
//WinSocks2 must be included before Windows.h
66+
# include <winsock2.h>
67+
#else
68+
# include <sys/ioctl.h>
69+
#endif
70+
6471
#include <stdio.h>
6572
#include <string.h>
6673
#include <stdlib.h>
@@ -76,11 +83,6 @@
7683
#include <fcntl.h>
7784
#include <ctype.h>
7885
#include <sys/stat.h>
79-
#ifdef _WIN32
80-
# include <winsock.h>
81-
#else
82-
# include <sys/ioctl.h>
83-
#endif
8486
#include <string.h>
8587
#include <time.h>
8688
#include <limits.h>

net_io.c

Lines changed: 27 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,6 +2105,30 @@ static bool UtilMoveFile(const char *fromPath, const char *toPath)
21052105
#endif
21062106
}
21072107

2108+
static bool UtilCreateWritableTempFile(char* outPathBuffer, const char* prefix, const char* path)
2109+
{
2110+
#ifdef _WIN32
2111+
if (GetTempFileNameA(path, prefix, 0, outPathBuffer) == 0) {
2112+
return false;
2113+
}
2114+
#else
2115+
int unix_fd = 0;
2116+
mode_t mask = 0;
2117+
2118+
snprintf(outPathBuffer, PATH_MAX, "%s/%sXXXXXX", path, prefix);
2119+
unix_fd = mkstemp(outPathBuffer);
2120+
if (unix_fd < 0) {
2121+
return NULL;
2122+
}
2123+
2124+
mask = umask(0);
2125+
umask(mask);
2126+
fchmod(unix_fd, 0644 & ~mask);
2127+
#endif
2128+
2129+
return true;
2130+
}
2131+
21082132
// Write JSON to file
21092133
void writeJsonToFile(const char *file, char * (*generator) (const char *,int*))
21102134
{
@@ -2117,33 +2141,13 @@ void writeJsonToFile(const char *file, char * (*generator) (const char *,int*))
21172141
if (!Modes.json_dir)
21182142
return;
21192143

2120-
#ifdef _WIN32
2121-
if (GetTempFileNameA(Modes.json_dir, file, 0, tmppath) == 0) {
2144+
if(!UtilCreateWritableTempFile(tmppath, file, Modes.json_dir) ||
2145+
(fd = fopen(tmppath, "wb")) == NULL) {
2146+
//TODO: implement a function to get the last error on windows and linux as a string
21222147
ratelimitWriteError("failed to create %s (while updating %s/%s): %s", tmppath, Modes.json_dir, file, strerror(errno));
2123-
return;
21242148
}
21252149

2126-
fd = fopen(tmppath, "wb");
2127-
#else
2128-
int unix_fd = 0;
2129-
mode_t mask = 0;
2130-
snprintf(tmppath, PATH_MAX, "%s/%sXXXXXX", Modes.json_dir, file);
2131-
tmppath[PATH_MAX - 1] = 0;
2132-
unix_fd = mkstemp(tmppath);
2133-
if (unix_fd < 0) {
2134-
ratelimitWriteError("failed to create %s (while updating %s/%s): %s", tmppath, Modes.json_dir, file, strerror(errno));
2135-
return;
2136-
}
2137-
2138-
mask = umask(0);
2139-
umask(mask);
2140-
fchmod(unix_fd, 0644 & ~mask);
2141-
fd = fdopen(unix_fd, "wb");
2142-
#endif
2143-
21442150
snprintf(pathbuf, PATH_MAX, "/data/%s", file);
2145-
pathbuf[PATH_MAX - 1] = 0;
2146-
21472151
content = generator(pathbuf, (int*)&len);
21482152

21492153
if (fwrite(content, 1, len, fd) != len || fclose(fd) != 0)
@@ -2156,8 +2160,6 @@ void writeJsonToFile(const char *file, char * (*generator) (const char *,int*))
21562160
}
21572161

21582162
snprintf(pathbuf, PATH_MAX, "%s/%s", Modes.json_dir, file);
2159-
pathbuf[PATH_MAX - 1] = 0;
2160-
21612163
if(!UtilMoveFile(tmppath, pathbuf)){
21622164
ratelimitWriteError("failed to rename %s to %s: %s", tmppath, pathbuf, strerror(errno));
21632165
}

0 commit comments

Comments
 (0)