From 01196c8c4f2fc66dd5afebc21a35a5cb7cef2bd2 Mon Sep 17 00:00:00 2001 From: Jordan Lee Date: Sun, 21 Sep 2014 18:01:36 +0000 Subject: [PATCH] mikedld patch: 4160-05b-file-fmt.patch --- libtransmission/blocklist.c | 39 +++++++++++++++++---------------- libtransmission/log.c | 43 +++++++++++++++++-------------------- libtransmission/log.h | 4 +++- libtransmission/peer-msgs.c | 8 +++---- libtransmission/tr-dht.c | 13 ++++++----- 5 files changed, 53 insertions(+), 54 deletions(-) diff --git a/libtransmission/blocklist.c b/libtransmission/blocklist.c index a00b38c37..e3c2c15ad 100644 --- a/libtransmission/blocklist.c +++ b/libtransmission/blocklist.c @@ -302,14 +302,15 @@ compareAddressRangesByFirstAddress (const void * va, const void * vb) 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) { @@ -317,35 +318,34 @@ tr_blocklistFileSetContent (tr_blocklistFile * b, const char * 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 */ @@ -397,9 +397,10 @@ tr_blocklistFileSetContent (tr_blocklistFile * b, const char * filename) #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 { @@ -409,8 +410,8 @@ tr_blocklistFileSetContent (tr_blocklistFile * b, const char * filename) } tr_free (ranges); - fclose (out); - fclose (in); + tr_sys_file_close (out, NULL); + tr_sys_file_close (in, NULL); blocklistLoad (b); diff --git a/libtransmission/log.c b/libtransmission/log.c index 15db6c8eb..98c451793 100644 --- a/libtransmission/log.c +++ b/libtransmission/log.c @@ -58,11 +58,11 @@ getMessageLock (void) 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) { @@ -75,15 +75,11 @@ tr_logGetFile (void) 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; } @@ -136,9 +132,9 @@ tr_logFreeQueue (tr_log_message * list) 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; } } @@ -173,7 +169,7 @@ tr_logGetDeepEnabled (void) static int8_t deepLoggingIsActive = -1; if (deepLoggingIsActive < 0) - deepLoggingIsActive = IsDebuggerPresent () || (tr_logGetFile ()!=NULL); + deepLoggingIsActive = IsDebuggerPresent () || (tr_logGetFile () != TR_BAD_SYS_FILE); return deepLoggingIsActive != 0; } @@ -185,8 +181,8 @@ tr_logAddDeep (const char * file, 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]; @@ -201,12 +197,13 @@ tr_logAddDeep (const char * file, 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); @@ -267,20 +264,20 @@ tr_logAddMessage (const char * file, } 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); } } diff --git a/libtransmission/log.h b/libtransmission/log.h index 7413a3c5e..fc07bb542 100644 --- a/libtransmission/log.h +++ b/libtransmission/log.h @@ -11,6 +11,8 @@ #define TR_LOG_H 1 #include /* size_t */ + +#include "file.h" /* tr_sys_file_t */ #include "utils.h" /* TR_GNUC_PRINTF, TR_GNUC_NONNULL */ #ifdef __cplusplus @@ -107,7 +109,7 @@ void tr_logAddMessage (const char * file, -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); diff --git a/libtransmission/peer-msgs.c b/libtransmission/peer-msgs.c index 969e3515b..23b0e1d86 100644 --- a/libtransmission/peer-msgs.c +++ b/libtransmission/peer-msgs.c @@ -282,9 +282,9 @@ myDebug (const char * file, int line, 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]; @@ -300,10 +300,10 @@ myDebug (const char * file, int line, 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); diff --git a/libtransmission/tr-dht.c b/libtransmission/tr-dht.c index abb08644b..4a44b1253 100644 --- a/libtransmission/tr-dht.c +++ b/libtransmission/tr-dht.c @@ -32,7 +32,6 @@ /* posix */ #include /* sig_atomic_t */ #include -#include /* close () */ #ifdef _WIN32 #include #define _WIN32_WINNT 0x0501 /* freeaddrinfo (),getaddrinfo (),getnameinfo () */ @@ -51,6 +50,7 @@ /* libT */ #include "transmission.h" #include "crypto.h" +#include "file.h" #include "log.h" #include "net.h" #include "peer-mgr.h" /* tr_peerMgrCompactToPex () */ @@ -195,22 +195,21 @@ dht_bootstrap (void *closure) 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)); @@ -228,7 +227,7 @@ dht_bootstrap (void *closure) if (bootstrap_done (cl->session, 0)) break; } - fclose (f); + tr_sys_file_close (f, NULL); } tr_free (bootstrap_file); -- 2.40.0