int
tr_blocklistFileSetContent (tr_blocklistFile * b, const char * filename)
{
- FILE * in;
- FILE * out;
+ tr_sys_file_t in;
+ tr_sys_file_t out;
int inCount = 0;
char line[2048];
const char * err_fmt = _("Couldn't read \"%1$s\": %2$s");
struct tr_ipv4_range * ranges = NULL;
size_t ranges_alloc = 0;
size_t ranges_count = 0;
+ tr_error * error = NULL;
if (!filename)
{
return 0;
}
- in = fopen (filename, "rb");
- if (in == NULL)
+ in = tr_sys_file_open (filename, TR_SYS_FILE_READ, 0, &error);
+ if (in == TR_BAD_SYS_FILE)
{
- tr_logAddError (err_fmt, filename, tr_strerror (errno));
+ tr_logAddError (err_fmt, filename, error->message);
+ tr_error_free (error);
return 0;
}
blocklistClose (b);
- out = fopen (b->filename, "wb+");
- if (out == NULL)
+ out = tr_sys_file_open (b->filename,
+ TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE,
+ 0666, &error);
+ if (out == TR_BAD_SYS_FILE)
{
- tr_logAddError (err_fmt, b->filename, tr_strerror (errno));
- fclose (in);
+ tr_logAddError (err_fmt, b->filename, error->message);
+ tr_error_free (error);
+ tr_sys_file_close (in, NULL);
return 0;
}
/* load the rules into memory */
- while (fgets (line, sizeof (line), in) != NULL)
+ while (tr_sys_file_read_line (in, line, sizeof (line), NULL))
{
- char * walk;
struct tr_ipv4_range range;
++inCount;
- /* zap the linefeed */
- if ((walk = strchr (line, '\r'))) *walk = '\0';
- if ((walk = strchr (line, '\n'))) *walk = '\0';
-
if (!parseLine (line, &range))
{
/* don't try to display the actual lines - it causes issues */
#endif
}
- if (fwrite (ranges, sizeof (struct tr_ipv4_range), ranges_count, out) != ranges_count)
+ if (!tr_sys_file_write (out, ranges, sizeof (struct tr_ipv4_range) * ranges_count, NULL, &error))
{
- tr_logAddError (_("Couldn't save file \"%1$s\": %2$s"), b->filename, tr_strerror (errno));
+ tr_logAddError (_("Couldn't save file \"%1$s\": %2$s"), b->filename, error->message);
+ tr_error_free (error);
}
else
{
}
tr_free (ranges);
- fclose (out);
- fclose (in);
+ tr_sys_file_close (out, NULL);
+ tr_sys_file_close (in, NULL);
blocklistLoad (b);
return l;
}
-void*
+tr_sys_file_t
tr_logGetFile (void)
{
static bool initialized = false;
- static FILE * file = NULL;
+ static tr_sys_file_t file = TR_BAD_SYS_FILE;
if (!initialized)
{
switch (fd)
{
case 1:
- file = stdout;
+ file = tr_sys_file_get_std (TR_STD_SYS_FILE_OUT, NULL);
break;
case 2:
- file = stderr;
- break;
-
- default:
- file = NULL;
+ file = tr_sys_file_get_std (TR_STD_SYS_FILE_ERR, NULL);
break;
}
while (NULL != list)
{
next = list->next;
- free (list->message);
- free (list->name);
- free (list);
+ tr_free (list->message);
+ tr_free (list->name);
+ tr_free (list);
list = next;
}
}
static int8_t deepLoggingIsActive = -1;
if (deepLoggingIsActive < 0)
- deepLoggingIsActive = IsDebuggerPresent () || (tr_logGetFile ()!=NULL);
+ deepLoggingIsActive = IsDebuggerPresent () || (tr_logGetFile () != TR_BAD_SYS_FILE);
return deepLoggingIsActive != 0;
}
const char * fmt,
...)
{
- FILE * fp = tr_logGetFile ();
- if (fp || IsDebuggerPresent ())
+ const tr_sys_file_t fp = tr_logGetFile ();
+ if (fp != TR_BAD_SYS_FILE || IsDebuggerPresent ())
{
va_list args;
char timestr[64];
va_start (args, fmt);
evbuffer_add_vprintf (buf, fmt, args);
va_end (args);
- evbuffer_add_printf (buf, " (%s:%d)\n", base, line);
+ evbuffer_add_printf (buf, " (%s:%d)", base, line);
/* FIXME (libevent2) ifdef this out for nonwindows platforms */
message = evbuffer_free_to_str (buf);
OutputDebugStringA (message);
- if (fp)
- fputs (message, fp);
+ OutputDebugStringA (TR_NATIVE_EOL_STR);
+ if (fp != TR_BAD_SYS_FILE)
+ tr_sys_file_write_line (fp, message, NULL);
tr_free (message);
tr_free (base);
}
else
{
- FILE * fp;
+ tr_sys_file_t fp;
char timestr[64];
fp = tr_logGetFile ();
- if (fp == NULL)
- fp = stderr;
+ if (fp == TR_BAD_SYS_FILE)
+ fp = tr_sys_file_get_std (TR_STD_SYS_FILE_ERR, NULL);
tr_logGetTimeStr (timestr, sizeof (timestr));
if (name)
- fprintf (fp, "[%s] %s: %s\n", timestr, name, buf);
+ tr_sys_file_write_fmt (fp, "[%s] %s: %s" TR_NATIVE_EOL_STR, NULL, timestr, name, buf);
else
- fprintf (fp, "[%s] %s\n", timestr, buf);
- fflush (fp);
+ tr_sys_file_write_fmt (fp, "[%s] %s" TR_NATIVE_EOL_STR, NULL, timestr, buf);
+ tr_sys_file_flush (fp, NULL);
}
}
#define TR_LOG_H 1
#include <stddef.h> /* size_t */
+
+#include "file.h" /* tr_sys_file_t */
#include "utils.h" /* TR_GNUC_PRINTF, TR_GNUC_NONNULL */
#ifdef __cplusplus
-void* tr_logGetFile (void);
+tr_sys_file_t tr_logGetFile (void);
/** @brief return true if deep logging has been enabled by the user; false otherwise */
bool tr_logGetDeepEnabled (void);
const struct tr_peerMsgs * msgs,
const char * fmt, ...)
{
- FILE * fp = tr_logGetFile ();
+ const tr_sys_file_t fp = tr_logGetFile ();
- if (fp)
+ if (fp != TR_BAD_SYS_FILE)
{
va_list args;
char timestr[64];
va_start (args, fmt);
evbuffer_add_vprintf (buf, fmt, args);
va_end (args);
- evbuffer_add_printf (buf, " (%s:%d)\n", base, line);
+ evbuffer_add_printf (buf, " (%s:%d)", base, line);
message = evbuffer_free_to_str (buf);
- fputs (message, fp);
+ tr_sys_file_write_line (fp, message, NULL);
tr_free (base);
tr_free (message);
/* posix */
#include <signal.h> /* sig_atomic_t */
#include <sys/time.h>
-#include <unistd.h> /* close () */
#ifdef _WIN32
#include <inttypes.h>
#define _WIN32_WINNT 0x0501 /* freeaddrinfo (),getaddrinfo (),getnameinfo () */
/* libT */
#include "transmission.h"
#include "crypto.h"
+#include "file.h"
#include "log.h"
#include "net.h"
#include "peer-mgr.h" /* tr_peerMgrCompactToPex () */
if (!bootstrap_done (cl->session, 0)) {
char *bootstrap_file;
- FILE *f = NULL;
+ tr_sys_file_t f = TR_BAD_SYS_FILE;
bootstrap_file =
tr_buildPath (cl->session->configDir, "dht.bootstrap", NULL);
if (bootstrap_file)
- f = fopen (bootstrap_file, "rb");
- if (f != NULL) {
+ f = tr_sys_file_open (bootstrap_file, TR_SYS_FILE_READ, 0, NULL);
+ if (f != TR_BAD_SYS_FILE) {
tr_logAddNamedInfo ("DHT", "Attempting manual bootstrap");
for (;;) {
char buf[201];
char *p;
int port = 0;
- p = fgets (buf, 200, f);
- if (p == NULL)
+ if (!tr_sys_file_read_line (f, buf, 200, NULL))
break;
p = memchr (buf, ' ', strlen (buf));
if (bootstrap_done (cl->session, 0))
break;
}
- fclose (f);
+ tr_sys_file_close (f, NULL);
}
tr_free (bootstrap_file);