Changeset 14335


Ignore:
Timestamp:
Sep 21, 2014, 6:03:13 PM (6 years ago)
Author:
jordan
Message:

#4160: mike.dld patch: 4160-06-misc.patch

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/daemon/daemon.c

    r14320 r14335  
    6262#define SPEED_T_STR "TB/s"
    6363
    64 #define LOGFILE_MODE_STR "a+"
    65 
    6664static bool paused = false;
    6765static bool seenHUP = false;
    6866static const char *logfileName = NULL;
    69 static FILE *logfile = NULL;
     67static tr_sys_file_t logfile = TR_BAD_SYS_FILE;
    7068static tr_session * mySession = NULL;
    7169static tr_quark key_pidfile = 0;
     
    145143}
    146144
     145static bool
     146reopen_log_file (const char *filename)
     147{
     148    tr_error * error = NULL;
     149    const tr_sys_file_t old_log_file = logfile;
     150    const tr_sys_file_t new_log_file = tr_sys_file_open (filename,
     151                                                         TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_APPEND,
     152                                                         0666, &error);
     153
     154    if (new_log_file == TR_BAD_SYS_FILE)
     155    {
     156        fprintf (stderr, "Couldn't (re)open log file \"%s\": %s\n", filename, error->message);
     157        tr_error_free (error);
     158        return false;
     159    }
     160
     161    logfile = new_log_file;
     162
     163    if (old_log_file != TR_BAD_SYS_FILE)
     164        tr_sys_file_close (old_log_file, NULL);
     165
     166    return true;
     167}
     168
    147169static void
    148170gotsig (int sig)
     
    163185
    164186                /* reopen the logfile to allow for log rotation */
    165                 if (logfileName) {
    166                     logfile = freopen (logfileName, LOGFILE_MODE_STR, logfile);
    167                     if (!logfile)
    168                         fprintf (stderr, "Couldn't reopen \"%s\": %s\n", logfileName, tr_strerror (errno));
     187                if (logfileName != NULL)
     188                {
     189                    reopen_log_file (logfileName);
    169190                }
    170191
     
    307328
    308329static void
    309 printMessage (FILE * logfile, int level, const char * name, const char * message, const char * file, int line)
    310 {
    311     if (logfile != NULL)
     330printMessage (tr_sys_file_t logfile, int level, const char * name, const char * message, const char * file, int line)
     331{
     332    if (logfile != TR_BAD_SYS_FILE)
    312333    {
    313334        char timestr[64];
    314335        tr_logGetTimeStr (timestr, sizeof (timestr));
    315336        if (name)
    316             fprintf (logfile, "[%s] %s %s (%s:%d)\n", timestr, name, message, file, line);
     337            tr_sys_file_write_fmt (logfile, "[%s] %s %s (%s:%d)" TR_NATIVE_EOL_STR,
     338                                   NULL, timestr, name, message, file, line);
    317339        else
    318             fprintf (logfile, "[%s] %s (%s:%d)\n", timestr, message, file, line);
     340            tr_sys_file_write_fmt (logfile, "[%s] %s (%s:%d)" TR_NATIVE_EOL_STR,
     341                                   NULL, timestr, message, file, line);
    319342    }
    320343#ifdef HAVE_SYSLOG
     
    339362
    340363static void
    341 pumpLogMessages (FILE * logfile)
     364pumpLogMessages (tr_sys_file_t logfile)
    342365{
    343366    const tr_log_message * l;
     
    347370        printMessage (logfile, l->level, l->name, l->message, l->file, l->line);
    348371
    349     if (logfile != NULL)
    350         fflush (logfile);
     372    if (logfile != TR_BAD_SYS_FILE)
     373        tr_sys_file_flush (logfile, NULL);
    351374
    352375    tr_logFreeQueue (list);
     
    440463            case 'd': dumpSettings = true;
    441464                      break;
    442             case 'e': logfile = fopen (optarg, LOGFILE_MODE_STR);
    443                       if (logfile)
     465            case 'e': if (reopen_log_file (optarg))
    444466                          logfileName = optarg;
    445                       else
    446                           fprintf (stderr, "Couldn't open \"%s\": %s\n", optarg, tr_strerror (errno));
    447467                      break;
    448468            case 'f': foreground = true;
     
    519539    }
    520540
    521     if (foreground && !logfile)
    522         logfile = stderr;
     541    if (foreground && logfile == TR_BAD_SYS_FILE)
     542        logfile = tr_sys_file_get_std (TR_STD_SYS_FILE_ERR, NULL);
    523543
    524544    if (!loaded)
     
    569589    if (pid_filename && *pid_filename)
    570590    {
    571         FILE * fp = fopen (pid_filename, "w+");
    572         if (fp != NULL)
     591        tr_error * error = NULL;
     592        tr_sys_file_t fp = tr_sys_file_open (pid_filename,
     593                                             TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE,
     594                                             0666, &error);
     595        if (fp != TR_BAD_SYS_FILE)
    573596        {
    574             fprintf (fp, "%d", (int)getpid ());
    575             fclose (fp);
     597            tr_sys_file_write_fmt (fp, "%d", NULL, (int)getpid ());
     598            tr_sys_file_close (fp, NULL);
    576599            tr_logAddInfo ("Saved pidfile \"%s\"", pid_filename);
    577600            pidfile_created = true;
    578601        }
    579602        else
    580             tr_logAddError ("Unable to save pidfile \"%s\": %s", pid_filename, tr_strerror (errno));
     603        {
     604            tr_logAddError ("Unable to save pidfile \"%s\": %s", pid_filename, error->message);
     605            tr_error_free (error);
     606        }
    581607    }
    582608
  • trunk/libtransmission/platform-quota.c

    r14316 r14335  
    3030 #ifdef HAVE_GETMNTENT
    3131  #ifdef __sun
    32    #include <sys/types.h>
    33    #include <sys/stat.h>
    3432   #include <fcntl.h>
    3533   #include <stdio.h>
     
    367365#ifdef _WIN32
    368366
    369   uint64_t freeBytesAvailable = 0;
    370   return GetDiskFreeSpaceEx (path, &freeBytesAvailable, NULL, NULL)
    371     ? (int64_t)freeBytesAvailable
    372     : -1;
     367  int64_t ret = -1;
     368  wchar_t * wide_path;
     369
     370  wide_path = tr_win32_utf8_to_native (path, -1);
     371
     372  if (wide_path != NULL)
     373    {
     374      ULARGE_INTEGER freeBytesAvailable;
     375      if (GetDiskFreeSpaceExW (wide_path, &freeBytesAvailable, NULL, NULL))
     376        ret = freeBytesAvailable.QuadPart;
     377
     378      tr_free (wide_path);
     379    }
     380
     381  return ret;
    373382
    374383#elif defined(HAVE_STATVFS)
  • trunk/libtransmission/platform.c

    r14331 r14335  
    1414
    1515#include <assert.h>
    16 #include <stdio.h>
    1716#include <stdlib.h>
    1817#include <string.h>
    19 #include <unistd.h> /* getuid(), close() */
     18#include <unistd.h> /* getuid() */
     19
     20#ifdef __HAIKU__
     21 #include <limits.h> /* PATH_MAX */
     22#endif
    2023
    2124#ifdef _WIN32
     
    4447#include "platform.h"
    4548#include "session.h"
     49#include "utils.h"
    4650
    4751/***
     
    224228#endif
    225229
     230#ifdef _WIN32
     231
     232char *
     233win32_get_special_folder (int folder_id)
     234{
     235  wchar_t path[MAX_PATH]; /* SHGetFolderPath () requires MAX_PATH */
     236  *path = L'\0';
     237  SHGetFolderPathW (NULL, folder_id, NULL, 0, path);
     238  return tr_win32_native_to_utf8 (path, -1);
     239}
     240
     241#endif
     242
    226243static const char *
    227244getHomeDir (void)
     
    236253        {
    237254#ifdef _WIN32
    238           char appdata[MAX_PATH]; /* SHGetFolderPath () requires MAX_PATH */
    239           *appdata = '\0';
    240           SHGetFolderPath (NULL, CSIDL_PERSONAL, NULL, 0, appdata);
    241           home = tr_strdup (appdata);
     255          home = win32_get_special_folder (CSIDL_PERSONAL);
    242256#else
    243257          struct passwd * pw = getpwuid (getuid ());
     
    316330          s = tr_buildPath (getHomeDir (), "Library", "Application Support", appname, NULL);
    317331#elif defined (_WIN32)
    318           char appdata[TR_PATH_MAX]; /* SHGetFolderPath () requires MAX_PATH */
    319           SHGetFolderPath (NULL, CSIDL_APPDATA, NULL, 0, appdata);
     332          char * appdata = win32_get_special_folder (CSIDL_APPDATA);
    320333          s = tr_buildPath (appdata, appname, NULL);
     334          tr_free (appdata);
    321335#elif defined (__HAIKU__)
    322           char buf[TR_PATH_MAX];
     336          char buf[PATH_MAX];
    323337          find_directory (B_USER_SETTINGS_DIRECTORY, -1, true, buf, sizeof (buf));
    324338          s = tr_buildPath (buf, appname, NULL);
     
    397411***/
    398412
    399 static int
     413static bool
    400414isWebClientDir (const char * path)
    401415{
     
    460474#elif defined (_WIN32)
    461475
    462           /* SHGetFolderPath explicitly requires MAX_PATH length */
    463           char dir[MAX_PATH];
    464 
    465476          /* Generally, Web interface should be stored in a Web subdir of
    466477           * calling executable dir. */
     
    468479          if (s == NULL) /* check personal AppData/Transmission/Web */
    469480            {
    470               SHGetFolderPath (NULL, CSIDL_COMMON_APPDATA, NULL, 0, dir);
     481              char * dir = win32_get_special_folder (CSIDL_COMMON_APPDATA);
    471482              s = tr_buildPath (dir, "Transmission", "Web", NULL);
     483              tr_free (dir);
    472484              if (!isWebClientDir (s))
    473485                {
     
    479491          if (s == NULL) /* check personal AppData */
    480492            {
    481               SHGetFolderPath (NULL, CSIDL_APPDATA, NULL, 0, dir);
     493              char * dir = win32_get_special_folder (CSIDL_APPDATA);
    482494              s = tr_buildPath (dir, "Transmission", "Web", NULL);
     495              tr_free (dir);
    483496              if (!isWebClientDir (s))
    484497                {
     
    490503            if (s == NULL) /* check calling module place */
    491504              {
    492                 char * tmp;
    493                 GetModuleFileName (GetModuleHandle (NULL), dir, sizeof (dir));
    494                 tmp = tr_sys_path_dirname (dir, NULL);
    495                 s = tr_buildPath (tmp, "Web", NULL);
    496                 tr_free (tmp);
     505                wchar_t wide_module_path[MAX_PATH];
     506                char * module_path;
     507                char * dir;
     508                GetModuleFileNameW (NULL, wide_module_path,
     509                                    sizeof (wide_module_path) / sizeof (*wide_module_path));
     510                module_path = tr_win32_native_to_utf8 (wide_module_path, -1);
     511                dir = tr_sys_path_dirname (module_path, NULL);
     512                tr_free (module_path);
     513                s = tr_buildPath (dir, "Web", NULL);
     514                tr_free (dir);
    497515                if (!isWebClientDir (s))
    498516                  {
Note: See TracChangeset for help on using the changeset viewer.