Changeset 14491


Ignore:
Timestamp:
Apr 11, 2015, 10:51:59 AM (6 years ago)
Author:
mikedld
Message:

#5908: Check for tr_loadFile return value instead of errno in tr_variantFromFile

Seems like there could be a defect in uClibc making errno not
thread-local. Don't rely on errno value but check function return value
instead which is a better failure indicator.

Return errors from tr_loadFile and tr_variantFromFile via tr_error.
Fix tr_sessionLoadSettings to not fail on Windows if settings.json
does not exist.

Location:
trunk
Files:
19 edited

Legend:

Unmodified
Added
Removed
  • trunk/cli/cli.c

    r14337 r14491  
    288288  ctor = tr_ctorNew (h);
    289289
    290   fileContents = tr_loadFile (torrentPath, &fileLength);
     290  fileContents = tr_loadFile (torrentPath, &fileLength, NULL);
    291291  tr_ctorSetPaused (ctor, TR_FORCE, false);
    292292  if (fileContents != NULL)
  • trunk/daemon/remote.c

    r14489 r14491  
    533533    size_t    len = 0;
    534534    char *    b64 = NULL;
    535     uint8_t * buf = tr_loadFile (filename, &len);
     535    uint8_t * buf = tr_loadFile (filename, &len, NULL);
    536536
    537537    if (buf)
  • trunk/libtransmission/error-types.h

    r14369 r14491  
    1515#include <windows.h>
    1616
     17#define TR_ERROR_IS_ENOENT(code) ((code) == ERROR_FILE_NOT_FOUND || \
     18                                  (code) == ERROR_PATH_NOT_FOUND)
    1719#define TR_ERROR_IS_ENOSPC(code) ((code) == ERROR_DISK_FULL)
    1820
    1921#define TR_ERROR_EINVAL ERROR_INVALID_PARAMETER
     22#define TR_ERROR_EISDIR ERROR_DIRECTORY_NOT_SUPPORTED
    2023
    2124#else /* _WIN32 */
     
    2326#include <errno.h>
    2427
     28#define TR_ERROR_IS_ENOENT(code) ((code) == ENOENT)
    2529#define TR_ERROR_IS_ENOSPC(code) ((code) == ENOSPC)
    2630
    2731#define TR_ERROR_EINVAL EINVAL
     32#define TR_ERROR_EISDIR EISDIR
    2833
    2934#endif /* _WIN32 */
  • trunk/libtransmission/platform.c

    r14490 r14491  
    376376
    377377      /* read in user-dirs.dirs and look for the download dir entry */
    378       content = (char *) tr_loadFile (config_file, &content_len);
     378      content = (char *) tr_loadFile (config_file, &content_len, NULL);
    379379      if (content && content_len>0)
    380380        {
  • trunk/libtransmission/rename-test.c

    r14428 r14491  
    5454      assert (tr_sys_path_exists (path, NULL));
    5555
    56       contents = tr_loadFile (path, &contents_len);
    57 
    58       success = (str_len == contents_len)
     56      contents = tr_loadFile (path, &contents_len, NULL);
     57
     58      success = contents != NULL
     59             && (str_len == contents_len)
    5960             && (!memcmp (contents, str, contents_len));
    6061
  • trunk/libtransmission/resume.c

    r14320 r14491  
    1212#include "transmission.h"
    1313#include "completion.h"
     14#include "error.h"
    1415#include "file.h"
    1516#include "log.h"
     
    707708  uint64_t fieldsLoaded = 0;
    708709  const bool wasDirty = tor->isDirty;
     710  tr_error * error = NULL;
    709711
    710712  assert (tr_isTorrent (tor));
     
    712714  filename = getResumeFilename (tor);
    713715
    714   if (tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename))
    715     {
    716       tr_logAddTorDbg (tor, "Couldn't read \"%s\"", filename);
     716  if (!tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename, &error))
     717    {
     718      tr_logAddTorDbg (tor, "Couldn't read \"%s\": %s", filename, error->message);
     719      tr_error_free (error);
    717720
    718721      tr_free (filename);
  • trunk/libtransmission/rpc-server.c

    r14428 r14491  
    430430      void * file;
    431431      size_t file_len;
    432       struct evbuffer * content;
    433       const int error = errno;
    434 
    435       errno = 0;
     432      tr_error * error = NULL;
     433
    436434      file_len = 0;
    437       file = tr_loadFile (filename, &file_len);
    438       content = evbuffer_new ();
    439       evbuffer_add_reference (content, file, file_len, evbuffer_ref_cleanup_tr_free, file);
    440 
    441       if (errno)
    442         {
    443           char * tmp = tr_strdup_printf ("%s (%s)", filename, tr_strerror (errno));
     435      file = tr_loadFile (filename, &file_len, &error);
     436
     437      if (file == NULL)
     438        {
     439          char * tmp = tr_strdup_printf ("%s (%s)", filename, error->message);
    444440          send_simple_response (req, HTTP_NOTFOUND, tmp);
    445441          tr_free (tmp);
     442          tr_error_free (error);
    446443        }
    447444      else
    448445        {
     446          struct evbuffer * content;
    449447          struct evbuffer * out;
    450448          const time_t now = tr_time ();
    451449
    452           errno = error;
     450          content = evbuffer_new ();
     451          evbuffer_add_reference (content, file, file_len, evbuffer_ref_cleanup_tr_free, file);
     452
    453453          out = evbuffer_new ();
    454454          evhttp_add_header (req->output_headers, "Content-Type", mimetype_guess (filename));
     
    459459
    460460          evbuffer_free (out);
    461         }
    462 
    463       evbuffer_free (content);
     461          evbuffer_free (content);
     462        }
    464463    }
    465464}
  • trunk/libtransmission/session.c

    r14479 r14491  
    3333#include "cache.h"
    3434#include "crypto-utils.h"
     35#include "error.h"
     36#include "error-types.h"
    3537#include "fdlimit.h"
    3638#include "file.h"
     
    457459tr_sessionLoadSettings (tr_variant * dict, const char * configDir, const char * appName)
    458460{
    459   int err = 0;
    460461  char * filename;
    461462  tr_variant fileSettings;
    462463  tr_variant sessionDefaults;
    463464  tr_variant tmp;
    464   bool success = false;
     465  bool success;
     466  tr_error * error = NULL;
    465467
    466468  assert (tr_variantIsDict (dict));
     
    481483  /* file settings override the defaults */
    482484  filename = tr_buildPath (configDir, "settings.json", NULL);
    483   err = tr_variantFromFile (&fileSettings, TR_VARIANT_FMT_JSON, filename);
    484   if (!err)
     485  if (tr_variantFromFile (&fileSettings, TR_VARIANT_FMT_JSON, filename, &error))
    485486    {
    486487      tr_variantMergeDicts (dict, &fileSettings);
    487488      tr_variantFree (&fileSettings);
     489      success = true;
     490    }
     491  else
     492    {
     493      success = TR_ERROR_IS_ENOENT (error->code);
     494      tr_error_free (error);
    488495    }
    489496
     
    491498  tr_variantFree (&sessionDefaults);
    492499  tr_free (filename);
    493   success = (err==0) || (err==ENOENT);
    494500  return success;
    495501}
     
    510516  {
    511517    tr_variant fileSettings;
    512     const int err = tr_variantFromFile (&fileSettings, TR_VARIANT_FMT_JSON, filename);
    513     if (!err)
     518    if (tr_variantFromFile (&fileSettings, TR_VARIANT_FMT_JSON, filename, NULL))
    514519      {
    515520        tr_variantMergeDicts (&settings, &fileSettings);
  • trunk/libtransmission/stats.c

    r14241 r14491  
    5151
    5252  filename = getFilename (session);
    53   loaded = !tr_variantFromFile (&top, TR_VARIANT_FMT_JSON, filename);
     53  loaded = tr_variantFromFile (&top, TR_VARIANT_FMT_JSON, filename, NULL);
    5454  tr_free (filename);
    5555
     
    5757    {
    5858      filename = getOldFilename (session);
    59       loaded = !tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename);
     59      loaded = tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename, NULL);
    6060      tr_free (filename);
    6161    }
  • trunk/libtransmission/torrent-ctor.c

    r14320 r14491  
    136136    int       err;
    137137
    138     metainfo = tr_loadFile (filename, &len);
     138    metainfo = tr_loadFile (filename, &len, NULL);
    139139    if (metainfo && len)
    140140        err = tr_ctorSetMetainfo (ctor, metainfo, len);
  • trunk/libtransmission/torrent-magnet.c

    r14355 r14491  
    107107
    108108  /* load the file, and find the info dict's offset inside the file */
    109   if ((fileContents = tr_loadFile (tor->info.torrent, &fileLen)))
     109  if ((fileContents = tr_loadFile (tor->info.torrent, &fileLen, NULL)))
    110110    {
    111111      tr_variant top;
     
    254254              char * path = tr_strdup (tor->info.torrent);
    255255
    256               if (!tr_variantFromFile (&newMetainfo, TR_VARIANT_FMT_BENC, path))
     256              if (tr_variantFromFile (&newMetainfo, TR_VARIANT_FMT_BENC, path, NULL))
    257257                {
    258258                  bool hasInfo;
  • trunk/libtransmission/torrent.c

    r14480 r14491  
    26612661
    26622662  /* save to the .torrent file */
    2663   if (ok && !tr_variantFromFile (&metainfo, TR_VARIANT_FMT_BENC, tor->info.torrent))
     2663  if (ok && tr_variantFromFile (&metainfo, TR_VARIANT_FMT_BENC, tor->info.torrent, NULL))
    26642664    {
    26652665      bool hasInfo;
  • trunk/libtransmission/tr-dht.c

    r14481 r14491  
    282282
    283283    dat_file = tr_buildPath (ss->configDir, "dht.dat", NULL);
    284     rc = tr_variantFromFile (&benc, TR_VARIANT_FMT_BENC, dat_file);
     284    rc = tr_variantFromFile (&benc, TR_VARIANT_FMT_BENC, dat_file, NULL) ? 0 : -1;
    285285    tr_free (dat_file);
    286286    if (rc == 0) {
  • trunk/libtransmission/utils.c

    r14471 r14491  
    214214
    215215uint8_t *
    216 tr_loadFile (const char * path,
    217              size_t     * size)
     216tr_loadFile (const char  * path,
     217             size_t      * size,
     218             tr_error   ** error)
    218219{
    219220  uint8_t * buf;
    220221  tr_sys_path_info info;
    221222  tr_sys_file_t fd;
    222   tr_error * error = NULL;
     223  tr_error * my_error = NULL;
    223224  const char * const err_fmt = _("Couldn't read \"%1$s\": %2$s");
    224225
    225226  /* try to stat the file */
    226   if (!tr_sys_path_get_info (path, 0, &info, &error))
    227     {
    228       const int err = error->code;
    229       tr_logAddDebug (err_fmt, path, error->message);
    230       tr_error_free (error);
    231       errno = err;
     227  if (!tr_sys_path_get_info (path, 0, &info, &my_error))
     228    {
     229      tr_logAddDebug (err_fmt, path, my_error->message);
     230      tr_error_propagate (error, &my_error);
    232231      return NULL;
    233232    }
     
    236235    {
    237236      tr_logAddError (err_fmt, path, _("Not a regular file"));
    238       errno = EISDIR;
     237      tr_error_set_literal (error, TR_ERROR_EISDIR, _("Not a regular file"));
    239238      return NULL;
    240239    }
     
    245244
    246245  /* Load the torrent file into our buffer */
    247   fd = tr_sys_file_open (path, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &error);
     246  fd = tr_sys_file_open (path, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &my_error);
    248247  if (fd == TR_BAD_SYS_FILE)
    249248    {
    250       const int err = error->code;
    251       tr_logAddError (err_fmt, path, error->message);
    252       tr_error_free (error);
    253       errno = err;
     249      tr_logAddError (err_fmt, path, my_error->message);
     250      tr_error_propagate (error, &my_error);
    254251      return NULL;
    255252    }
     253
    256254  buf = tr_malloc (info.size + 1);
    257   if (!buf)
    258     {
    259       const int err = errno;
    260       tr_logAddError (err_fmt, path, _("Memory allocation failed"));
    261       tr_sys_file_close (fd, NULL);
    262       errno = err;
    263       return NULL;
    264     }
    265   if (!tr_sys_file_read (fd, buf, info.size, NULL, &error))
    266     {
    267       const int err = error->code;
    268       tr_logAddError (err_fmt, path, error->message);
     255
     256  if (!tr_sys_file_read (fd, buf, info.size, NULL, &my_error))
     257    {
     258      tr_logAddError (err_fmt, path, my_error->message);
    269259      tr_sys_file_close (fd, NULL);
    270260      free (buf);
    271       tr_error_free (error);
    272       errno = err;
     261      tr_error_propagate (error, &my_error);
    273262      return NULL;
    274263    }
  • trunk/libtransmission/utils.h

    r14481 r14491  
    133133 * On failure, NULL is returned and errno is set.
    134134 */
    135 uint8_t* tr_loadFile (const char * filename, size_t * size) TR_GNUC_MALLOC
    136                                                              TR_GNUC_NONNULL (1);
     135uint8_t * tr_loadFile (const char  * filename,
     136                       size_t      * size,
     137                       tr_error   ** error) TR_GNUC_MALLOC TR_GNUC_NONNULL (1);
    137138
    138139
  • trunk/libtransmission/variant.c

    r14428 r14491  
    12191219***/
    12201220
    1221 int
     1221bool
    12221222tr_variantFromFile (tr_variant      * setme,
    12231223                    tr_variant_fmt    fmt,
    1224                     const char      * filename)
    1225 {
    1226   int err;
     1224                    const char      * filename,
     1225                    tr_error       ** error)
     1226{
     1227  bool ret = false;
     1228  uint8_t * buf;
    12271229  size_t buflen;
    1228   uint8_t * buf;
    1229   const int old_errno = errno;
    1230 
    1231   errno = 0;
    1232   buf = tr_loadFile (filename, &buflen);
    1233 
    1234   if (errno)
    1235     err = errno;
    1236   else
    1237     err = tr_variantFromBuf (setme, fmt, buf, buflen, filename, NULL);
    1238 
    1239   tr_free (buf);
    1240   errno = old_errno;
    1241   return err;
     1230
     1231  buf = tr_loadFile (filename, &buflen, error);
     1232  if (buf != NULL)
     1233    {
     1234      if (tr_variantFromBuf (setme, fmt, buf, buflen, filename, NULL) == 0)
     1235        ret = true;
     1236      else
     1237        tr_error_set_literal (error, 0, _("Unable to parse file content"));
     1238
     1239      tr_free (buf);
     1240    }
     1241
     1242  return ret;
    12421243}
    12431244
  • trunk/libtransmission/variant.h

    r14428 r14491  
    1919
    2020struct evbuffer;
     21
     22struct tr_error;
    2123
    2224/**
     
    121123
    122124/* TR_VARIANT_FMT_JSON_LEAN and TR_VARIANT_FMT_JSON are equivalent here. */
    123 int tr_variantFromFile (tr_variant      * setme,
    124                         tr_variant_fmt    fmt,
    125                         const char      * filename);
     125bool tr_variantFromFile (tr_variant       * setme,
     126                         tr_variant_fmt     fmt,
     127                         const char       * filename,
     128                         struct tr_error ** error);
    126129
    127130/* TR_VARIANT_FMT_JSON_LEAN and TR_VARIANT_FMT_JSON are equivalent here. */
  • trunk/qt/prefs.cc

    r14466 r14491  
    259259  tr_variant file_settings;
    260260  const QFile file (QDir(myConfigDir).absoluteFilePath(QLatin1String ("settings.json")));
    261   if (tr_variantFromFile (&file_settings, TR_VARIANT_FMT_JSON, file.fileName().toUtf8().constData()))
     261  if (!tr_variantFromFile (&file_settings, TR_VARIANT_FMT_JSON, file.fileName().toUtf8().constData(), NULL))
    262262    tr_variantInitDict (&file_settings, PREFS_COUNT);
    263263  tr_variantMergeDicts (&file_settings, &current_settings);
  • trunk/utils/edit.c

    r14337 r14491  
    1515
    1616#include <libtransmission/transmission.h>
     17#include <libtransmission/error.h>
    1718#include <libtransmission/tr-getopt.h>
    1819#include <libtransmission/utils.h>
     
    329330      bool changed = false;
    330331      const char * filename = files[i];
     332      tr_error * error = NULL;
    331333
    332334      printf ("%s\n", filename);
    333335
    334       if (tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename))
    335         {
    336           printf ("\tError reading file\n");
     336      if (!tr_variantFromFile (&top, TR_VARIANT_FMT_BENC, filename, &error))
     337        {
     338          printf ("\tError reading file: %s\n", error->message);
     339          tr_error_free (error);
    337340          continue;
    338341        }
Note: See TracChangeset for help on using the changeset viewer.