Changeset 14368


Ignore:
Timestamp:
Dec 10, 2014, 6:37:58 PM (8 years ago)
Author:
mikedld
Message:

Fix tr_moveFile error reporting

Remove unused renamed argument.
Use tr_error instead of errno to report errors to support Win32 file
wrappers which do not map Windows error codes to POSIX ones.
Return bool instead of int (0/-1).

Uncomment tr_error_prefix and tr_error_propagate_prefixed functions.

Location:
trunk/libtransmission
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/error-types.h

    r14367 r14368  
    1717#define TR_ERROR_IS_ENOSPC(code) ((code) == ERROR_DISK_FULL)
    1818
     19#define TR_ERROR_EINVAL ERROR_INVALID_PARAMETER
     20
    1921#else /* _WIN32 */
    2022
     
    2325#define TR_ERROR_IS_ENOSPC(code) ((code) == ENOSPC)
    2426
     27#define TR_ERROR_EINVAL EINVAL
     28
    2529#endif /* _WIN32 */
    2630
  • trunk/libtransmission/error.c

    r14299 r14368  
    136136}
    137137
    138 #if 0
    139 
    140138static void
    141139error_prefix_valist (tr_error   ** error,
     
    195193  va_end (args);
    196194}
    197 
    198 #endif /* 0 */
  • trunk/libtransmission/error.h

    r14299 r14368  
    135135void       tr_error_clear              (tr_error   ** error);
    136136
    137 #if 0
    138 
    139137/**
    140138 * @brief Prefix message of exising error object.
     
    168166                                                      ...) TR_GNUC_PRINTF (3, 4);
    169167
    170 #endif /* 0 */
    171 
    172168/** @} */
    173169
  • trunk/libtransmission/torrent.c

    r14355 r14368  
    30953095              if (do_move && !tr_sys_path_is_same (oldpath, newpath, NULL))
    30963096                {
    3097                   bool renamed = false;
    3098                   errno = 0;
     3097                  tr_error * error = NULL;
     3098
    30993099                  tr_logAddTorInfo (tor, "moving \"%s\" to \"%s\"", oldpath, newpath);
    3100                   if (tr_moveFile (oldpath, newpath, &renamed))
     3100                  if (!tr_moveFile (oldpath, newpath, &error))
    31013101                    {
    31023102                      err = true;
    31033103                      tr_logAddTorErr (tor, "error moving \"%s\" to \"%s\": %s",
    3104                                        oldpath, newpath, tr_strerror (errno));
     3104                                       oldpath, newpath, error->message);
     3105                      tr_error_free (error);
    31053106                    }
    31063107                }
  • trunk/libtransmission/utils.c

    r14361 r14368  
    4747#include "transmission.h"
    4848#include "error.h"
     49#include "error-types.h"
    4950#include "file.h"
    5051#include "ConvertUTF.h"
     
    14111412***/
    14121413
    1413 int
    1414 tr_moveFile (const char * oldpath, const char * newpath, bool * renamed)
     1414bool
     1415tr_moveFile (const char * oldpath, const char * newpath, tr_error ** error)
    14151416{
    14161417  tr_sys_file_t in;
    14171418  tr_sys_file_t out;
    1418   char * buf;
     1419  char * buf = NULL;
    14191420  tr_sys_path_info info;
    14201421  uint64_t bytesLeft;
    14211422  const size_t buflen = 1024 * 128; /* 128 KiB buffer */
    1422   tr_error * error = NULL;
    14231423
    14241424  /* make sure the old file exists */
    1425   if (!tr_sys_path_get_info (oldpath, 0, &info, &error))
    1426     {
    1427       const int err = error->code;
    1428       tr_error_free (error);
    1429       errno = err;
    1430       return -1;
     1425  if (!tr_sys_path_get_info (oldpath, 0, &info, error))
     1426    {
     1427      tr_error_prefix (error, "Unable to get information on old file: ");
     1428      return false;
    14311429    }
    14321430  if (info.type != TR_SYS_PATH_IS_FILE)
    14331431    {
    1434       errno = ENOENT;
    1435       return -1;
    1436     }
    1437   bytesLeft = info.size;
     1432      tr_error_set_literal (error, TR_ERROR_EINVAL, "Old path does not point to a file.");
     1433      return false;
     1434    }
    14381435
    14391436  /* make sure the target directory exists */
    14401437  {
    14411438    char * newdir = tr_sys_path_dirname (newpath, NULL);
    1442     const bool i = tr_sys_dir_create (newdir, TR_SYS_DIR_CREATE_PARENTS, 0777, &error);
     1439    const bool i = tr_sys_dir_create (newdir, TR_SYS_DIR_CREATE_PARENTS, 0777, error);
    14431440    tr_free (newdir);
    14441441    if (!i)
    14451442      {
    1446         const int err = error->code;
    1447         tr_error_free (error);
    1448         errno = err;
    1449         return -1;
     1443        tr_error_prefix (error, "Unable to create directory for new file: ");
     1444        return false;
    14501445      }
    14511446  }
    14521447
    14531448  /* they might be on the same filesystem... */
    1454   {
    1455     const bool i = tr_sys_path_rename (oldpath, newpath, NULL);
    1456     if (renamed != NULL)
    1457       *renamed = i;
    1458     if (i)
    1459       return 0;
    1460   }
     1449  if (tr_sys_path_rename (oldpath, newpath, NULL))
     1450    return true;
    14611451
    14621452  /* copy the file */
    1463   in = tr_sys_file_open (oldpath, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, NULL);
    1464   out = tr_sys_file_open (newpath, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666, NULL);
     1453  in = tr_sys_file_open (oldpath, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, error);
     1454  if (in == TR_BAD_SYS_FILE)
     1455    {
     1456      tr_error_prefix (error, "Unable to open old file: ");
     1457      return false;
     1458    }
     1459
     1460  out = tr_sys_file_open (newpath, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666, error);
     1461  if (out == TR_BAD_SYS_FILE)
     1462    {
     1463      tr_error_prefix (error, "Unable to open new file: ");
     1464      tr_sys_file_close (in, NULL);
     1465      return false;
     1466    }
     1467
    14651468  buf = tr_valloc (buflen);
     1469  bytesLeft = info.size;
    14661470  while (bytesLeft > 0)
    14671471    {
    14681472      const uint64_t bytesThisPass = MIN (bytesLeft, buflen);
    14691473      uint64_t numRead, bytesWritten;
    1470       if (!tr_sys_file_read (in, buf, bytesThisPass, &numRead, NULL))
     1474      if (!tr_sys_file_read (in, buf, bytesThisPass, &numRead, error))
    14711475        break;
    1472       if (!tr_sys_file_write (out, buf, numRead, &bytesWritten, NULL))
     1476      if (!tr_sys_file_write (out, buf, numRead, &bytesWritten, error))
    14731477        break;
     1478      assert (numRead == bytesWritten);
     1479      assert (bytesWritten <= bytesLeft);
    14741480      bytesLeft -= bytesWritten;
    14751481    }
     
    14791485  tr_sys_file_close (out, NULL);
    14801486  tr_sys_file_close (in, NULL);
     1487
    14811488  if (bytesLeft != 0)
    1482     return -1;
    1483 
    1484   tr_sys_path_remove (oldpath, NULL);
    1485   return 0;
     1489    {
     1490      tr_error_prefix (error, "Unable to read/write: ");
     1491      return false;
     1492    }
     1493
     1494  {
     1495    tr_error * my_error = NULL;
     1496    if (!tr_sys_path_remove (oldpath, &my_error))
     1497      {
     1498        tr_logAddError ("Unable to remove file at old path: %s", my_error->message);
     1499        tr_error_free (my_error);
     1500      }
     1501  }
     1502
     1503  return true;
    14861504}
    14871505
  • trunk/libtransmission/utils.h

    r14361 r14368  
    1616#include <time.h> /* time_t */
    1717
     18#include "error.h"
    1819
    1920#ifdef __cplusplus
     
    397398/**
    398399 * @brief move a file
    399  * @return 0 on success; otherwise, return -1 and set errno
    400  */
    401 int tr_moveFile (const char * oldpath, const char * newpath,
    402                  bool * renamed) TR_GNUC_NONNULL (1,2);
     400 * @return `True` on success, `false` otherwise (with `error` set accordingly).
     401 */
     402bool tr_moveFile (const char * oldpath, const char * newpath,
     403                  tr_error ** error) TR_GNUC_NONNULL (1,2);
    403404
    404405/** @brief convenience function to remove an item from an array */
Note: See TracChangeset for help on using the changeset viewer.