Changeset 14327


Ignore:
Timestamp:
Jul 28, 2014, 4:13:38 AM (8 years ago)
Author:
jordan
Message:

(trunk, libt) #4160 - the slow slog to catch trunk up to mike.dld's 4160 diff continues. This step applies 4160-03b-file.patch, which replaces native file operations with the tr_sys_file_*() portability wrappers added in r14321.

Location:
trunk/libtransmission
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/blocklist-test.c

    r14320 r14327  
    1010#include <assert.h>
    1111#include <stdio.h>
     12#include <string.h> /* strlen () */
    1213#include <unistd.h> /* sync() */
    1314
     
    3738create_text_file (const char * path, const char * contents)
    3839{
    39   FILE * fp;
     40  tr_sys_file_t fd;
    4041  char * dir;
    4142
     
    4445  tr_free (dir);
    4546
    46   tr_sys_path_remove (path, NULL);
    47   fp = fopen (path, "w+");
    48   fprintf (fp, "%s", contents);
    49   fclose (fp);
     47  fd = tr_sys_file_open (path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL);
     48  tr_sys_file_write (fd, contents, strlen (contents), NULL, NULL);
     49  tr_sys_file_close (fd, NULL);
    5050
    5151  sync ();
  • trunk/libtransmission/blocklist.c

    r14320 r14327  
    1414#include <string.h>
    1515
    16 #include <unistd.h> /* close () */
    17 
    18 #ifdef _WIN32
    19  #include <w32api.h>
    20  #define WINVER  WindowsXP
    21  #include <windows.h>
    22  #define PROT_READ      PAGE_READONLY
    23  #define MAP_PRIVATE    FILE_MAP_COPY
    24 #endif
    25 
    26 #ifndef _WIN32
    27  #include <sys/mman.h>
    28 #endif
    29 #include <fcntl.h>
    30 
    3116#include "transmission.h"
    3217#include "blocklist.h"
     18#include "error.h"
    3319#include "file.h"
    3420#include "log.h"
     
    3622#include "utils.h"
    3723
    38 #ifndef O_BINARY
    39  #define O_BINARY 0
    40 #endif
    41 
    4224
    4325/***
     
    5436{
    5537  bool                   isEnabled;
    56   int                    fd;
     38  tr_sys_file_t          fd;
    5739  size_t                 ruleCount;
    58   size_t                 byteCount;
     40  uint64_t               byteCount;
    5941  char *                 filename;
    6042  struct tr_ipv4_range * rules;
     
    6648  if (b->rules != NULL)
    6749    {
    68       munmap (b->rules, b->byteCount);
    69       close (b->fd);
     50      tr_sys_file_unmap (b->rules, b->byteCount, NULL);
     51      tr_sys_file_close (b->fd, NULL);
    7052      b->rules = NULL;
    7153      b->ruleCount = 0;
    7254      b->byteCount = 0;
    73       b->fd = -1;
     55      b->fd = TR_BAD_SYS_FILE;
    7456    }
    7557}
     
    7860blocklistLoad (tr_blocklistFile * b)
    7961{
    80   int fd;
    81   size_t byteCount;
     62  tr_sys_file_t fd;
     63  uint64_t byteCount;
    8264  tr_sys_path_info info;
    8365  char * base;
     66  tr_error * error = NULL;
    8467  const char * err_fmt = _("Couldn't read \"%1$s\": %2$s");
    8568
     
    8972    return;
    9073
    91   byteCount = (size_t) info.size;
     74  byteCount = info.size;
    9275  if (byteCount == 0)
    9376    return;
    9477
    95   fd = open (b->filename, O_RDONLY | O_BINARY);
    96   if (fd == -1)
    97     {
    98       tr_logAddError (err_fmt, b->filename, tr_strerror (errno));
     78  fd = tr_sys_file_open (b->filename, TR_SYS_FILE_READ, 0, &error);
     79  if (fd == TR_BAD_SYS_FILE)
     80    {
     81      tr_logAddError (err_fmt, b->filename, error->message);
     82      tr_error_free (error);
    9983      return;
    10084    }
    10185
    102   b->rules = mmap (NULL, byteCount, PROT_READ, MAP_PRIVATE, fd, 0);
     86  b->rules = tr_sys_file_map_for_reading (fd, 0, byteCount, &error);
    10387  if (!b->rules)
    10488    {
    105       tr_logAddError (err_fmt, b->filename, tr_strerror (errno));
    106       close (fd);
     89      tr_logAddError (err_fmt, b->filename, error->message);
     90      tr_sys_file_close (fd, NULL);
     91      tr_error_free (error);
    10792      return;
    10893    }
     
    152137
    153138  b = tr_new0 (tr_blocklistFile, 1);
    154   b->fd = -1;
     139  b->fd = TR_BAD_SYS_FILE;
    155140  b->filename = tr_strdup (filename);
    156141  b->isEnabled = isEnabled;
  • trunk/libtransmission/fdlimit.c

    r14320 r14327  
    88 */
    99
    10 #ifdef HAVE_POSIX_FADVISE
    11  #ifdef _XOPEN_SOURCE
    12   #undef _XOPEN_SOURCE
    13  #endif
    14  #define _XOPEN_SOURCE 600
    15 #endif
    16 
    1710#include <assert.h>
    1811#include <errno.h>
    1912#include <inttypes.h>
    2013#include <string.h>
    21 #ifdef __APPLE__
    22  #include <fcntl.h>
    23 #endif
    24 
    25 #ifdef HAVE_FALLOCATE64
    26   /* FIXME can't find the right #include voodoo to pick up the declaration.. */
    27   extern int fallocate64 (int fd, int mode, uint64_t offset, uint64_t len);
    28 #endif
    29 
    30 #ifdef HAVE_XFS_XFS_H
    31  #include <xfs/xfs.h>
    32 #endif
    33 
    34 #include <sys/types.h>
    35 #include <sys/stat.h>
     14
    3615#include <sys/time.h> /* getrlimit */
    3716#include <sys/resource.h> /* getrlimit */
    38 #include <fcntl.h> /* O_LARGEFILE posix_fadvise */
    39 #include <unistd.h> /* lseek (), write (), ftruncate (), pread (), pwrite (), etc */
    4017
    4118#include "transmission.h"
     19#include "error.h"
    4220#include "fdlimit.h"
    4321#include "file.h"
     
    6038***/
    6139
    62 #ifndef O_LARGEFILE
    63  #define O_LARGEFILE 0
    64 #endif
    65 
    66 #ifndef O_BINARY
    67  #define O_BINARY 0
    68 #endif
    69 
    70 #ifndef O_SEQUENTIAL
    71  #define O_SEQUENTIAL 0
    72 #endif
    73 
    74 
    7540static bool
    76 preallocate_file_sparse (int fd, uint64_t length)
     41preallocate_file_sparse (tr_sys_file_t fd, uint64_t length)
    7742{
    7843  const char zero = '\0';
     
    8247    success = true;
    8348
    84 #ifdef HAVE_FALLOCATE64
    85   if (!success) /* fallocate64 is always preferred, so try it first */
    86     success = !fallocate64 (fd, 0, 0, length);
    87 #endif
     49  if (!success)
     50    success = tr_sys_file_preallocate (fd, length, TR_SYS_FILE_PREALLOC_SPARSE, NULL);
    8851
    8952  if (!success) /* fallback: the old-style seek-and-write */
    90     success = (lseek (fd, length-1, SEEK_SET) != -1)
    91            && (write (fd, &zero, 1) != -1)
    92            && (ftruncate (fd, length) != -1);
     53    {
     54      /* seek requires signed offset, so length should be in mod range */
     55      assert (length < 0x7FFFFFFFFFFFFFFFULL);
     56
     57      success = tr_sys_file_seek (fd, length - 1, TR_SEEK_SET, NULL, NULL) &&
     58                tr_sys_file_write (fd, &zero, 1, NULL, NULL) &&
     59                tr_sys_file_truncate (fd, length, NULL);
     60    }
    9361
    9462  return success;
     
    10068  bool success = 0;
    10169
    102 #ifdef _WIN32
    103 
    104   HANDLE hFile = CreateFile (filename, GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_FLAG_RANDOM_ACCESS, 0);
    105   if (hFile != INVALID_HANDLE_VALUE)
    106     {
    107       LARGE_INTEGER li;
    108       li.QuadPart = length;
    109       success = SetFilePointerEx (hFile, li, NULL, FILE_BEGIN) && SetEndOfFile (hFile);
    110       CloseHandle (hFile);
    111     }
    112 
    113 #else
    114 
    115   int flags = O_RDWR | O_CREAT | O_LARGEFILE;
    116   int fd = open (filename, flags, 0666);
    117   if (fd >= 0)
    118     {
    119 # ifdef HAVE_FALLOCATE64
    120       if (!success)
    121         success = !fallocate64 (fd, 0, 0, length);
    122 # endif
    123 # ifdef HAVE_XFS_XFS_H
    124       if (!success && platform_test_xfs_fd (fd))
    125         {
    126           xfs_flock64_t fl;
    127           fl.l_whence = 0;
    128           fl.l_start = 0;
    129           fl.l_len = length;
    130           success = !xfsctl (NULL, fd, XFS_IOC_RESVSP64, &fl);
    131         }
    132 # endif
    133 # ifdef __APPLE__
    134       if (!success)
    135         {
    136           fstore_t fst;
    137           fst.fst_flags = F_ALLOCATECONTIG;
    138           fst.fst_posmode = F_PEOFPOSMODE;
    139           fst.fst_offset = 0;
    140           fst.fst_length = length;
    141           fst.fst_bytesalloc = 0;
    142           success = !fcntl (fd, F_PREALLOCATE, &fst);
    143         }
    144 # endif
    145 # ifdef HAVE_POSIX_FALLOCATE
    146       if (!success)
    147         success = !posix_fallocate (fd, 0, length);
    148 # endif
     70  tr_sys_file_t fd = tr_sys_file_open (filename, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE, 0666, NULL);
     71  if (fd != TR_BAD_SYS_FILE)
     72    {
     73      success = tr_sys_file_preallocate (fd, length, 0, NULL);
    14974
    15075      if (!success) /* if nothing else works, do it the old-fashioned way */
     
    15580          while (success && (length > 0))
    15681            {
    157               const int thisPass = MIN (length, sizeof (buf));
    158               success = write (fd, buf, thisPass) == thisPass;
     82              const uint64_t thisPass = MIN (length, sizeof (buf));
     83              uint64_t bytes_written;
     84              success = tr_sys_file_write (fd, buf, thisPass, &bytes_written, NULL) && bytes_written == thisPass;
    15985              length -= thisPass;
    16086            }
    16187        }
    16288
    163       close (fd);
    164     }
    165 
    166 #endif
     89      tr_sys_file_close (fd, NULL);
     90    }
    16791
    16892  return success;
    169 }
    170 
    171 
    172 /* portability wrapper for fsync (). */
    173 int
    174 tr_fsync (int fd)
    175 {
    176 #ifdef _WIN32
    177   return _commit (fd);
    178 #else
    179   return fsync (fd);
    180 #endif
    181 }
    182 
    183 
    184 /* Like pread and pwrite, except that the position is undefined afterwards.
    185    And of course they are not thread-safe. */
    186 
    187 /* don't use pread/pwrite on old versions of uClibc because they're buggy.
    188  * https://trac.transmissionbt.com/ticket/3826 */
    189 #ifdef __UCLIBC__
    190 #define TR_UCLIBC_CHECK_VERSION(major,minor,micro) \
    191   (__UCLIBC_MAJOR__ > (major) || \
    192    (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ > (minor)) || \
    193    (__UCLIBC_MAJOR__ == (major) && __UCLIBC_MINOR__ == (minor) && \
    194       __UCLIBC_SUBLEVEL__ >= (micro)))
    195 #if !TR_UCLIBC_CHECK_VERSION (0,9,28)
    196  #undef HAVE_PREAD
    197  #undef HAVE_PWRITE
    198 #endif
    199 #endif
    200 
    201 #ifdef __APPLE__
    202  #define HAVE_PREAD
    203  #define HAVE_PWRITE
    204 #endif
    205 
    206 ssize_t
    207 tr_pread (int fd, void *buf, size_t count, off_t offset)
    208 {
    209 #ifdef HAVE_PREAD
    210   return pread (fd, buf, count, offset);
    211 #else
    212   const off_t lrc = lseek (fd, offset, SEEK_SET);
    213   if (lrc < 0)
    214     return -1;
    215   return read (fd, buf, count);
    216 #endif
    217 }
    218 
    219 ssize_t
    220 tr_pwrite (int fd, const void *buf, size_t count, off_t offset)
    221 {
    222 #ifdef HAVE_PWRITE
    223   return pwrite (fd, buf, count, offset);
    224 #else
    225   const off_t lrc = lseek (fd, offset, SEEK_SET);
    226   if (lrc < 0)
    227     return -1;
    228   return write (fd, buf, count);
    229 #endif
    230 }
    231 
    232 int
    233 tr_prefetch (int fd UNUSED, off_t offset UNUSED, size_t count UNUSED)
    234 {
    235 #ifdef HAVE_POSIX_FADVISE
    236   return posix_fadvise (fd, offset, count, POSIX_FADV_WILLNEED);
    237 #elif defined (__APPLE__)
    238   struct radvisory radv;
    239   radv.ra_offset = offset;
    240   radv.ra_count = count;
    241   return fcntl (fd, F_RDADVISE, &radv);
    242 #else
    243   return 0;
    244 #endif
    245 }
    246 
    247 void
    248 tr_set_file_for_single_pass (int fd)
    249 {
    250   if (fd >= 0)
    251     {
    252       /* Set hints about the lookahead buffer and caching. It's okay
    253          for these to fail silently, so don't let them affect errno */
    254       const int err = errno;
    255 #ifdef HAVE_POSIX_FADVISE
    256       posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
    257 #endif
    258 #ifdef __APPLE__
    259       fcntl (fd, F_RDAHEAD, 1);
    260       fcntl (fd, F_NOCACHE, 1);
    261 #endif
    262       errno = err;
    263     }
    264 }
    265 
    266 static int
    267 open_local_file (const char * filename, int flags)
    268 {
    269   const int fd = open (filename, flags, 0666);
    270   tr_set_file_for_single_pass (fd);
    271   return fd;
    272 }
    273 int
    274 tr_open_file_for_writing (const char * filename)
    275 {
    276   return open_local_file (filename, O_LARGEFILE|O_BINARY|O_CREAT|O_WRONLY);
    277 }
    278 int
    279 tr_open_file_for_scanning (const char * filename)
    280 {
    281   return open_local_file (filename, O_LARGEFILE|O_BINARY|O_SEQUENTIAL|O_RDONLY);
    282 }
    283 
    284 void
    285 tr_close_file (int fd)
    286 {
    287   close (fd);
    28893}
    28994
     
    297102{
    298103  bool is_writable;
    299   int fd;
     104  tr_sys_file_t fd;
    300105  int torrent_id;
    301106  tr_file_index_t file_index;
     
    308113  assert (o != NULL);
    309114
    310   return o->fd >= 0;
     115  return o->fd != TR_BAD_SYS_FILE;
    311116}
    312117
     
    316121  assert (cached_file_is_open (o));
    317122
    318   tr_close_file (o->fd);
    319   o->fd = -1;
     123  tr_sys_file_close (o->fd, NULL);
     124  o->fd = TR_BAD_SYS_FILE;
    320125}
    321126
     
    323128 * returns 0 on success, or an errno value on failure.
    324129 * errno values include ENOENT if the parent folder doesn't exist,
    325  * plus the errno values set by tr_mkdirp () and open ().
     130 * plus the errno values set by tr_mkdirp () and tr_sys_file_open ().
    326131 */
    327132static int
     
    336141  bool already_existed;
    337142  bool resize_needed;
     143  tr_error * error = NULL;
    338144
    339145  /* create subfolders, if any */
     
    362168
    363169  /* open the file */
    364   flags = writable ? (O_RDWR | O_CREAT) : O_RDONLY;
    365   flags |= O_LARGEFILE | O_BINARY | O_SEQUENTIAL;
    366   o->fd = open (filename, flags, 0666);
    367 
    368   if (o->fd == -1)
    369     {
    370       const int err = errno;
    371       tr_logAddError (_("Couldn't open \"%1$s\": %2$s"), filename, tr_strerror (err));
     170  flags = writable ? (TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE) : 0;
     171  flags |= TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL;
     172  o->fd = tr_sys_file_open (filename, flags, 0666, &error);
     173
     174  if (o->fd == TR_BAD_SYS_FILE)
     175    {
     176      const int err = error->code;
     177      tr_logAddError (_("Couldn't open \"%1$s\": %2$s"), filename, error->message);
     178      tr_error_free (error);
    372179      return err;
    373180    }
     
    379186   * https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
    380187   */
    381   if (resize_needed && (ftruncate (o->fd, file_size) == -1))
    382     {
    383       const int err = errno;
    384       tr_logAddError (_("Couldn't truncate \"%1$s\": %2$s"), filename, tr_strerror (err));
     188  if (resize_needed && !tr_sys_file_truncate (o->fd, file_size, &error))
     189    {
     190      const int err = error->code;
     191      tr_logAddError (_("Couldn't truncate \"%1$s\": %2$s"), filename, error->message);
     192      tr_error_free (error);
    385193      return err;
    386194    }
     
    388196  if (writable && !already_existed && (allocation == TR_PREALLOCATE_SPARSE))
    389197    preallocate_file_sparse (o->fd, file_size);
    390 
    391   /* Many (most?) clients request blocks in ascending order,
    392    * so increase the readahead buffer.
    393    * Also, disable OS-level caching because "inactive memory" angers users. */
    394   tr_set_file_for_single_pass (o->fd);
    395198
    396199  return 0;
     
    411214{
    412215  struct tr_cached_file * o;
    413   const struct tr_cached_file TR_CACHED_FILE_INIT = { 0, -1, 0, 0, 0 };
     216  const struct tr_cached_file TR_CACHED_FILE_INIT = { false, TR_BAD_SYS_FILE, 0, 0, 0 };
    414217
    415218  set->begin = tr_new (struct tr_cached_file, n);
     
    568371       * up-to-date when this function returns to the caller... */
    569372      if (o->is_writable)
    570         tr_fsync (o->fd);
     373        tr_sys_file_flush (o->fd, NULL);
    571374
    572375      cached_file_close (o);
     
    574377}
    575378
    576 int
     379tr_sys_file_t
    577380tr_fdFileGetCached (tr_session * s, int torrent_id, tr_file_index_t i, bool writable)
    578381{
     
    580383
    581384  if (!o || (writable && !o->is_writable))
    582     return -1;
     385    return TR_BAD_SYS_FILE;
    583386
    584387  o->used_at = tr_time ();
     
    586389}
    587390
    588 #ifdef __APPLE__
    589  #define TR_STAT_MTIME(sb)((sb).st_mtimespec.tv_sec)
    590 #else
    591  #define TR_STAT_MTIME(sb)((sb).st_mtime)
    592 #endif
    593 
    594391bool
    595392tr_fdFileGetCachedMTime (tr_session * s, int torrent_id, tr_file_index_t i, time_t * mtime)
    596393{
    597394  bool success;
    598   struct stat sb;
     395  tr_sys_path_info info;
    599396  struct tr_cached_file * o = fileset_lookup (get_fileset (s), torrent_id, i);
    600397
    601   if ((success = (o != NULL) && !fstat (o->fd, &sb)))
    602     *mtime = TR_STAT_MTIME (sb);
     398  if ((success = (o != NULL) && tr_sys_file_get_info (o->fd, &info, NULL)))
     399    *mtime = info.last_modified_at;
    603400
    604401  return success;
     
    613410}
    614411
    615 /* returns an fd on success, or a -1 on failure and sets errno */
    616 int
     412/* returns an fd on success, or a TR_BAD_SYS_FILE on failure and sets errno */
     413tr_sys_file_t
    617414tr_fdFileCheckout (tr_session             * session,
    618415                   int                      torrent_id,
     
    637434        {
    638435          errno = err;
    639           return -1;
     436          return TR_BAD_SYS_FILE;
    640437        }
    641438
  • trunk/libtransmission/fdlimit.h

    r14241 r14327  
    1313
    1414#include "transmission.h"
     15#include "file.h"
    1516#include "net.h"
    1617
     
    2324****
    2425***/
    25 
    26 void tr_set_file_for_single_pass (int fd);
    27 
    28 int tr_open_file_for_scanning (const char * filename);
    29 
    30 int tr_open_file_for_writing (const char * filename);
    31 
    32 void tr_close_file (int fd);
    33 
    34 int tr_fsync (int fd);
    35 
    36 ssize_t tr_pread (int fd, void *buf, size_t count, off_t offset);
    37 ssize_t tr_pwrite (int fd, const void *buf, size_t count, off_t offset);
    38 int tr_prefetch (int fd, off_t offset, size_t count);
    39 
    4026
    4127/**
     
    5036 *
    5137 * on success, a file descriptor >= 0 is returned.
    52  * on failure, a -1 is returned and errno is set.
     38 * on failure, a TR_BAD_SYS_FILE is returned and errno is set.
    5339 *
    5440 * @see tr_fdFileClose
    5541 */
    56 int  tr_fdFileCheckout (tr_session             * session,
    57                         int                      torrent_id,
    58                         tr_file_index_t          file_num,
    59                         const char             * filename,
    60                         bool                     do_write,
    61                         tr_preallocation_mode    preallocation_mode,
    62                         uint64_t                 preallocation_file_size);
     42tr_sys_file_t  tr_fdFileCheckout (tr_session             * session,
     43                                  int                      torrent_id,
     44                                  tr_file_index_t          file_num,
     45                                  const char             * filename,
     46                                  bool                     do_write,
     47                                  tr_preallocation_mode    preallocation_mode,
     48                                  uint64_t                 preallocation_file_size);
    6349
    64 int tr_fdFileGetCached (tr_session             * session,
    65                         int                      torrent_id,
    66                         tr_file_index_t          file_num,
    67                         bool                  doWrite);
     50tr_sys_file_t tr_fdFileGetCached (tr_session             * session,
     51                                  int                      torrent_id,
     52                                  tr_file_index_t          file_num,
     53                                  bool                     doWrite);
    6854
    6955bool tr_fdFileGetCachedMTime (tr_session       * session,
  • trunk/libtransmission/inout.c

    r14241 r14327  
    1717#include "transmission.h"
    1818#include "cache.h" /* tr_cacheReadBlock () */
     19#include "error.h"
    1920#include "fdlimit.h"
     21#include "file.h"
    2022#include "inout.h"
    2123#include "log.h"
     
    4749                  size_t             buflen)
    4850{
    49   int fd;
     51  tr_sys_file_t fd;
    5052  int err = 0;
    5153  const bool doWrite = ioMode >= TR_IO_WRITE;
     
    6567
    6668  fd = tr_fdFileGetCached (session, tr_torrentId (tor), fileIndex, doWrite);
    67   if (fd < 0)
     69  if (fd == TR_BAD_SYS_FILE)
    6870    {
    6971      /* it's not cached, so open/create it now */
     
    9597          if (((fd = tr_fdFileCheckout (session, tor->uniqueId, fileIndex,
    9698                                        filename, doWrite,
    97                                         prealloc, file->length))) < 0)
     99                                        prealloc, file->length))) == TR_BAD_SYS_FILE)
    98100            {
    99101              err = errno;
     
    119121  if (!err)
    120122    {
     123      tr_error * error = NULL;
     124
    121125      if (ioMode == TR_IO_READ)
    122126        {
    123           const int rc = tr_pread (fd, buf, buflen, fileOffset);
    124           if (rc < 0)
     127          if (!tr_sys_file_read_at (fd, buf, buflen, fileOffset, NULL, &error))
    125128            {
    126               err = errno;
    127               tr_logAddTorErr (tor, "read failed for \"%s\": %s", file->name, tr_strerror (err));
     129              err = error->code;
     130              tr_logAddTorErr (tor, "read failed for \"%s\": %s", file->name, error->message);
     131              tr_error_free (error);
    128132            }
    129133        }
    130134      else if (ioMode == TR_IO_WRITE)
    131135        {
    132           const int rc = tr_pwrite (fd, buf, buflen, fileOffset);
    133           if (rc < 0)
     136          if (!tr_sys_file_write_at (fd, buf, buflen, fileOffset, NULL, &error))
    134137            {
    135               err = errno;
    136               tr_logAddTorErr (tor, "write failed for \"%s\": %s", file->name, tr_strerror (err));
     138              err = error->code;
     139              tr_logAddTorErr (tor, "write failed for \"%s\": %s", file->name, error->message);
     140              tr_error_free (error);
    137141            }
    138142        }
    139143      else if (ioMode == TR_IO_PREFETCH)
    140144        {
    141           tr_prefetch (fd, fileOffset, buflen);
     145          tr_sys_file_prefetch (fd, fileOffset, buflen, NULL);
    142146        }
    143147      else
  • trunk/libtransmission/libtransmission-test.c

    r14320 r14327  
    1515
    1616#include "transmission.h"
     17#include "error.h"
    1718#include "file.h"
    1819#include "platform.h" /* TR_PATH_DELIMETER */
     
    374375      int err;
    375376      uint64_t j;
    376       FILE * fp;
     377      tr_sys_file_t fd;
    377378      char * path;
    378379      char * dirname;
     
    385386      dirname = tr_sys_path_dirname (path, NULL);
    386387      tr_mkdirp (dirname, 0700);
    387       fp = fopen (path, "wb+");
     388      fd = tr_sys_file_open (path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL);
    388389      for (j=0; j<file->length; ++j)
    389         fputc (((!complete) && (i==0) && (j<tor->info.pieceSize)) ? '\1' : '\0', fp);
    390       fclose (fp);
     390        tr_sys_file_write (fd, ((!complete) && (i==0) && (j<tor->info.pieceSize)) ? "\1" : "\0", 1, NULL, NULL);
     391      tr_sys_file_close (fd, NULL);
    391392
    392393      tr_free (dirname);
     
    451452libtest_create_file_with_contents (const char* path, const void* payload, size_t n)
    452453{
    453   FILE * fp;
     454  tr_sys_file_t fd;
    454455  const int tmperr = errno;
    455456
    456457  build_parent_dir (path);
    457458
    458   tr_sys_path_remove (path, NULL);
    459   fp = fopen (path, "wb");
    460   fwrite (payload, 1, n, fp);
    461   fclose (fp);
     459  fd = tr_sys_file_open (path, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0600, NULL);
     460  tr_sys_file_write (fd, payload, n, NULL, NULL);
     461  tr_sys_file_close (fd, NULL);
    462462
    463463  sync ();
     
    475475libtest_create_tmpfile_with_contents (char* tmpl, const void* payload, size_t n)
    476476{
    477   int fd;
     477  tr_sys_file_t fd;
    478478  const int tmperr = errno;
    479   size_t n_left = n;
     479  uint64_t n_left = n;
     480  tr_error * error = NULL;
    480481
    481482  build_parent_dir (tmpl);
    482483
    483   fd = mkstemp (tmpl);
     484  fd = tr_sys_file_open_temp (tmpl, NULL);
    484485  while (n_left > 0)
    485486    {
    486       const ssize_t n = write (fd, payload, n_left);
    487       if (n == -1)
     487      uint64_t n;
     488      if (!tr_sys_file_write (fd, payload, n_left, &n, &error))
    488489        {
    489           fprintf (stderr, "Error writing '%s': %s\n", tmpl, tr_strerror(errno));
     490          fprintf (stderr, "Error writing '%s': %s\n", tmpl, error->message);
     491          tr_error_free (error);
    490492          break;
    491493        }
    492494      n_left -= n;
    493495    }
    494   close (fd);
     496  tr_sys_file_close (fd, NULL);
    495497
    496498  sync ();
  • trunk/libtransmission/makemeta.c

    r14320 r14327  
    1010#include <assert.h>
    1111#include <errno.h>
    12 #include <stdio.h> /* FILE, stderr */
    1312#include <stdlib.h> /* qsort */
    1413#include <string.h> /* strcmp, strlen */
    1514
    16 #include <unistd.h> /* read () */
    1715#include <dirent.h>
    1816
     
    2220#include "crypto.h" /* tr_sha1 */
    2321#include "error.h"
    24 #include "fdlimit.h" /* tr_open_file_for_scanning () */
    2522#include "file.h"
    2623#include "log.h"
     
    229226  uint64_t totalRemain;
    230227  uint64_t off = 0;
    231   int fd;
     228  tr_sys_file_t fd;
     229  tr_error * error = NULL;
    232230
    233231  if (!b->totalSize)
     
    237235  b->pieceIndex = 0;
    238236  totalRemain = b->totalSize;
    239   fd = tr_open_file_for_scanning (b->files[fileIndex].filename);
    240   if (fd < 0)
    241     {
    242       b->my_errno = errno;
     237  fd = tr_sys_file_open (b->files[fileIndex].filename, TR_SYS_FILE_READ |
     238                         TR_SYS_FILE_SEQUENTIAL, 0, &error);
     239  if (fd == TR_BAD_SYS_FILE)
     240    {
     241      b->my_errno = error->code;
    243242      tr_strlcpy (b->errfile,
    244243                  b->files[fileIndex].filename,
     
    247246      tr_free (buf);
    248247      tr_free (ret);
     248      tr_error_free (error);
    249249      return NULL;
    250250    }
     
    254254      uint8_t * bufptr = buf;
    255255      const uint32_t thisPieceSize = (uint32_t) MIN (b->pieceSize, totalRemain);
    256       uint32_t leftInPiece = thisPieceSize;
     256      uint64_t leftInPiece = thisPieceSize;
    257257
    258258      assert (b->pieceIndex < b->pieceCount);
     
    260260      while (leftInPiece)
    261261        {
    262           const size_t n_this_pass = (size_t) MIN ((b->files[fileIndex].size - off), leftInPiece);
    263           const ssize_t n_read = read (fd, bufptr, n_this_pass);
     262          const uint64_t n_this_pass = MIN (b->files[fileIndex].size - off, leftInPiece);
     263          uint64_t n_read = 0;
     264          tr_sys_file_read (fd, bufptr, n_this_pass, &n_read, NULL);
    264265          bufptr += n_read;
    265266          off += n_read;
     
    268269            {
    269270              off = 0;
    270               tr_close_file (fd);
    271               fd = -1;
     271              tr_sys_file_close (fd, NULL);
     272              fd = TR_BAD_SYS_FILE;
    272273              if (++fileIndex < b->fileCount)
    273274                {
    274                   fd = tr_open_file_for_scanning (b->files[fileIndex].filename);
    275                   if (fd < 0)
     275                  fd = tr_sys_file_open (b->files[fileIndex].filename, TR_SYS_FILE_READ |
     276                                         TR_SYS_FILE_SEQUENTIAL, 0, &error);
     277                  if (fd == TR_BAD_SYS_FILE)
    276278                    {
    277                       b->my_errno = errno;
     279                      b->my_errno = error->code;
    278280                      tr_strlcpy (b->errfile,
    279281                                  b->files[fileIndex].filename,
     
    282284                      tr_free (buf);
    283285                      tr_free (ret);
     286                      tr_error_free (error);
    284287                      return NULL;
    285288                    }
     
    307310  assert (b->abortFlag || !totalRemain);
    308311
    309   if (fd >= 0)
    310     tr_close_file (fd);
     312  if (fd != TR_BAD_SYS_FILE)
     313    tr_sys_file_close (fd, NULL);
    311314
    312315  tr_free (buf);
  • trunk/libtransmission/platform.c

    r14320 r14327  
    566566  return s;
    567567}
    568 
    569 
    570 #ifdef _WIN32
    571 
    572 /* The following mmap functions are by Joerg Walter, and were taken from
    573  * his paper at: http://www.genesys-e.de/jwalter/mix4win.htm */
    574 
    575 #if defined (_MSC_VER)
    576 __declspec (align (4)) static LONG volatile g_sl;
    577 #else
    578 static LONG volatile g_sl __attribute__((aligned (4)));
    579 #endif
    580 
    581 /* Wait for spin lock */
    582 static int
    583 slwait (LONG volatile *sl)
    584 {
    585   while (InterlockedCompareExchange (sl, 1, 0) != 0)
    586     Sleep (0);
    587 
    588   return 0;
    589 }
    590 
    591 /* Release spin lock */
    592 static int
    593 slrelease (LONG volatile *sl)
    594 {
    595   InterlockedExchange (sl, 0);
    596   return 0;
    597 }
    598 
    599 /* getpagesize for windows */
    600 static long
    601 getpagesize (void)
    602 {
    603   static long g_pagesize = 0;
    604 
    605   if (!g_pagesize)
    606     {
    607       SYSTEM_INFO system_info;
    608       GetSystemInfo (&system_info);
    609       g_pagesize = system_info.dwPageSize;
    610     }
    611 
    612   return g_pagesize;
    613 }
    614 
    615 static long
    616 getregionsize (void)
    617 {
    618   static long g_regionsize = 0;
    619 
    620   if (!g_regionsize)
    621     {
    622       SYSTEM_INFO system_info;
    623       GetSystemInfo (&system_info);
    624       g_regionsize = system_info.dwAllocationGranularity;
    625     }
    626 
    627   return g_regionsize;
    628 }
    629 
    630 void *
    631 mmap (void *ptr, long  size, long  prot, long  type, long  handle, long  arg)
    632 {
    633   static long g_pagesize;
    634   static long g_regionsize;
    635 
    636   /* Wait for spin lock */
    637   slwait (&g_sl);
    638 
    639   /* First time initialization */
    640   if (!g_pagesize)
    641     g_pagesize = getpagesize ();
    642   if (!g_regionsize)
    643     g_regionsize = getregionsize ();
    644 
    645   /* Allocate this */
    646   ptr = VirtualAlloc (ptr, size, MEM_RESERVE | MEM_COMMIT | MEM_TOP_DOWN, PAGE_READWRITE);
    647   if (!ptr)
    648     {
    649       ptr = (void *) -1;
    650       goto mmap_exit;
    651     }
    652 
    653 mmap_exit:
    654   /* Release spin lock */
    655   slrelease (&g_sl);
    656   return ptr;
    657 }
    658 
    659 long
    660 munmap (void *ptr, long size)
    661 {
    662   static long g_pagesize;
    663   static long g_regionsize;
    664   int rc = -1;
    665 
    666   /* Wait for spin lock */
    667   slwait (&g_sl);
    668 
    669   /* First time initialization */
    670   if (!g_pagesize)
    671     g_pagesize = getpagesize ();
    672   if (!g_regionsize)
    673     g_regionsize = getregionsize ();
    674 
    675   /* Free this */
    676   if (!VirtualFree (ptr, 0, MEM_RELEASE))
    677     goto munmap_exit;
    678 
    679   rc = 0;
    680 
    681 munmap_exit:
    682   /* Release spin lock */
    683   slrelease (&g_sl);
    684   return rc;
    685 }
    686 
    687 #endif
  • trunk/libtransmission/platform.h

    r14316 r14327  
    7878int tr_lockHave (const tr_lock *);
    7979
    80 #ifdef _WIN32
    81 void * mmap (void *ptr, long  size, long  prot, long  type, long  handle, long  arg);
    82 
    83 long munmap (void *ptr, long  size);
    84 #endif
    85 
    8680/* @} */
    8781
  • trunk/libtransmission/rpc-server.c

    r14322 r14327  
    1111#include <errno.h>
    1212#include <string.h> /* memcpy */
    13 
    14 #include <unistd.h>    /* close */
    1513
    1614#include <zlib.h>
  • trunk/libtransmission/rpcimpl.c

    r14322 r14327  
    2020#include "transmission.h"
    2121#include "completion.h"
     22#include "error.h"
    2223#include "fdlimit.h"
    2324#include "file.h"
     
    14881489  else /* successfully fetched the blocklist... */
    14891490    {
    1490       int fd;
     1491      tr_sys_file_t fd;
    14911492      int err;
    14921493      char * filename;
     
    14951496      const size_t buflen = 1024 * 128; /* 128 KiB buffer */
    14961497      uint8_t * buf = tr_valloc (buflen);
     1498      tr_error * error = NULL;
    14971499
    14981500      /* this is an odd Magic Number required by zlib to enable gz support.
     
    15071509      inflateInit2 (&stream, windowBits);
    15081510
    1509       filename = tr_buildPath (configDir, "blocklist.tmp", NULL);
    1510       fd = tr_open_file_for_writing (filename);
    1511       if (fd < 0)
    1512         tr_snprintf (result, sizeof (result), _("Couldn't save file \"%1$s\": %2$s"), filename, tr_strerror (errno));
     1511      filename = tr_buildPath (configDir, "blocklist.tmp.XXXXXX", NULL);
     1512      fd = tr_sys_file_open_temp (filename, &error);
     1513      if (fd == TR_BAD_SYS_FILE)
     1514        {
     1515          tr_snprintf (result, sizeof (result), _("Couldn't save file \"%1$s\": %2$s"), filename, error->message);
     1516          tr_error_clear (&error);
     1517        }
    15131518
    15141519      for (;;)
     
    15201525          if (stream.avail_out < buflen)
    15211526            {
    1522               const int e = write (fd, buf, buflen - stream.avail_out);
    1523               if (e < 0)
     1527              if (!tr_sys_file_write (fd, buf, buflen - stream.avail_out, NULL, &error))
    15241528                {
    1525                   tr_snprintf (result, sizeof (result), _("Couldn't save file \"%1$s\": %2$s"), filename, tr_strerror (errno));
     1529                  tr_snprintf (result, sizeof (result), _("Couldn't save file \"%1$s\": %2$s"), filename, error->message);
     1530                  tr_error_clear (&error);
    15261531                  break;
    15271532                }
     
    15391544
    15401545      if (err == Z_DATA_ERROR) /* couldn't inflate it... it's probably already uncompressed */
    1541         if (write (fd, response, response_byte_count) < 0)
    1542           tr_snprintf (result, sizeof (result), _("Couldn't save file \"%1$s\": %2$s"), filename, tr_strerror (errno));
    1543 
    1544       tr_close_file(fd);
     1546        if (!tr_sys_file_write (fd, response, response_byte_count, NULL, &error))
     1547          {
     1548            tr_snprintf (result, sizeof (result), _("Couldn't save file \"%1$s\": %2$s"), filename, error->message);
     1549            tr_error_clear (&error);
     1550          }
     1551
     1552      tr_sys_file_close (fd, NULL);
    15451553
    15461554      if (*result)
  • trunk/libtransmission/torrent-magnet.c

    r14320 r14327  
    156156  if (tr_torrentHasMetadata (tor))
    157157    {
    158       FILE * fp;
     158      tr_sys_file_t fd;
    159159
    160160      ensureInfoDictOffsetIsCached (tor);
     
    163163      assert (tor->infoDictOffset >= 0);
    164164
    165       fp = fopen (tor->info.torrent, "rb");
    166       if (fp != NULL)
     165      fd = tr_sys_file_open (tor->info.torrent, TR_SYS_FILE_READ, 0, NULL);
     166      if (fd != TR_BAD_SYS_FILE)
    167167        {
    168168          const int o = piece  * METADATA_PIECE_SIZE;
    169169
    170           if (!fseek (fp, tor->infoDictOffset + o, SEEK_SET))
     170          if (tr_sys_file_seek (fd, tor->infoDictOffset + o, TR_SEEK_SET, NULL, NULL))
    171171            {
    172172              const int l = o + METADATA_PIECE_SIZE <= tor->infoDictLength
     
    177177                {
    178178                  char * buf = tr_new (char, l);
    179                   const int n = fread (buf, 1, l, fp);
    180                   if (n == l)
     179                  uint64_t n;
     180                  if (tr_sys_file_read (fd, buf, l, &n, NULL) && n == (unsigned int) l)
    181181                    {
    182182                      *len = l;
     
    189189            }
    190190
    191           fclose (fp);
     191          tr_sys_file_close (fd, NULL);
    192192        }
    193193    }
  • trunk/libtransmission/utils.c

    r14320 r14327  
    2929#include <string.h> /* strerror (), memset (), memmem () */
    3030#include <time.h> /* nanosleep () */
     31#include <sys/types.h>
     32#include <sys/stat.h>
    3133
    3234#ifdef HAVE_ICONV_OPEN
     
    3436#endif
    3537#include <sys/time.h>
    36 #include <sys/types.h>
    37 #include <sys/stat.h>
    38 #include <unistd.h> /* stat (), getpagesize () */
     38#include <unistd.h> /* getpagesize () */
    3939
    4040#include <event2/buffer.h>
     
    4949#include "transmission.h"
    5050#include "error.h"
    51 #include "fdlimit.h"
    5251#include "file.h"
    5352#include "ConvertUTF.h"
     
    222221  uint8_t * buf;
    223222  tr_sys_path_info info;
    224   int fd;
    225   ssize_t n;
     223  tr_sys_file_t fd;
    226224  tr_error * error = NULL;
    227225  const char * const err_fmt = _("Couldn't read \"%1$s\": %2$s");
     
    249247
    250248  /* Load the torrent file into our buffer */
    251   fd = tr_open_file_for_scanning (path);
    252   if (fd < 0)
    253     {
    254       const int err = errno;
    255       tr_logAddError (err_fmt, path, tr_strerror (errno));
     249  fd = tr_sys_file_open (path, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, &error);
     250  if (fd == TR_BAD_SYS_FILE)
     251    {
     252      const int err = error->code;
     253      tr_logAddError (err_fmt, path, error->message);
     254      tr_error_free (error);
    256255      errno = err;
    257256      return NULL;
     
    262261      const int err = errno;
    263262      tr_logAddError (err_fmt, path, _("Memory allocation failed"));
    264       tr_close_file (fd);
     263      tr_sys_file_close (fd, NULL);
    265264      errno = err;
    266265      return NULL;
    267266    }
    268   n = read (fd, buf, (size_t)info.size);
    269   if (n == -1)
    270     {
    271       const int err = errno;
    272       tr_logAddError (err_fmt, path, tr_strerror (errno));
    273       tr_close_file (fd);
     267  if (!tr_sys_file_read (fd, buf, info.size, NULL, &error))
     268    {
     269      const int err = error->code;
     270      tr_logAddError (err_fmt, path, error->message);
     271      tr_sys_file_close (fd, NULL);
    274272      free (buf);
     273      tr_error_free (error);
    275274      errno = err;
    276275      return NULL;
    277276    }
    278277
    279   tr_close_file (fd);
     278  tr_sys_file_close (fd, NULL);
    280279  buf[info.size] = '\0';
    281280  *size = info.size;
     
    15451544tr_moveFile (const char * oldpath, const char * newpath, bool * renamed)
    15461545{
    1547   int in;
    1548   int out;
     1546  tr_sys_file_t in;
     1547  tr_sys_file_t out;
    15491548  char * buf;
    15501549  tr_sys_path_info info;
    1551   off_t bytesLeft;
     1550  uint64_t bytesLeft;
    15521551  const size_t buflen = 1024 * 128; /* 128 KiB buffer */
    15531552  tr_error * error = NULL;
     
    15871586
    15881587  /* copy the file */
    1589   in = tr_open_file_for_scanning (oldpath);
    1590   out = tr_open_file_for_writing (newpath);
     1588  in = tr_sys_file_open (oldpath, TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, NULL);
     1589  out = tr_sys_file_open (newpath, TR_SYS_FILE_WRITE | TR_SYS_FILE_CREATE | TR_SYS_FILE_TRUNCATE, 0666, NULL);
    15911590  buf = tr_valloc (buflen);
    15921591  while (bytesLeft > 0)
    15931592    {
    1594       ssize_t bytesWritten;
    1595       const off_t bytesThisPass = MIN (bytesLeft, (off_t)buflen);
    1596       const int numRead = read (in, buf, bytesThisPass);
    1597       if (numRead < 0)
     1593      const uint64_t bytesThisPass = MIN (bytesLeft, buflen);
     1594      uint64_t numRead, bytesWritten;
     1595      if (!tr_sys_file_read (in, buf, bytesThisPass, &numRead, NULL))
    15981596        break;
    1599       bytesWritten = write (out, buf, numRead);
    1600       if (bytesWritten < 0)
     1597      if (!tr_sys_file_write (out, buf, numRead, &bytesWritten, NULL))
    16011598        break;
    16021599      bytesLeft -= bytesWritten;
     
    16051602  /* cleanup */
    16061603  tr_free (buf);
    1607   tr_close_file (out);
    1608   tr_close_file (in);
     1604  tr_sys_file_close (out, NULL);
     1605  tr_sys_file_close (in, NULL);
    16091606  if (bytesLeft != 0)
    16101607    return -1;
  • trunk/libtransmission/variant.c

    r14320 r14327  
    1010#include <assert.h>
    1111#include <errno.h>
    12 #include <stdlib.h> /* strtod(), realloc(), qsort(), mkstemp() */
     12#include <stdlib.h> /* strtod(), realloc(), qsort() */
    1313#include <string.h>
    1414
    15 #ifdef _WIN32 /* tr_mkstemp() */
    16  #include <fcntl.h>
     15#ifdef _WIN32
    1716 #include <share.h>
    18  #include <sys/stat.h>
    1917#endif
    2018
    2119#include <locale.h> /* setlocale() */
    22 #include <unistd.h> /* write() */
    2320
    2421#include <event2/buffer.h>
     
    2724#include "transmission.h"
    2825#include "ConvertUTF.h" 
    29 #include "fdlimit.h" /* tr_close_file() */
    3026#include "error.h"
    3127#include "file.h"
     
    11261122}
    11271123
    1128 /* portability wrapper for mkstemp(). */
    1129 static int
    1130 tr_mkstemp (char * template)
    1131 {
    1132 #ifdef _WIN32
    1133 
    1134   const int n = strlen (template) + 1;
    1135   const int flags = O_RDWR | O_BINARY | O_CREAT | O_EXCL | _O_SHORT_LIVED;
    1136   const mode_t mode = _S_IREAD | _S_IWRITE;
    1137   wchar_t templateUTF16[n];
    1138 
    1139   if (MultiByteToWideChar(CP_UTF8, 0, template, -1, templateUTF16, n))
    1140     {
    1141       _wmktemp(templateUTF16);
    1142       WideCharToMultiByte(CP_UTF8, 0, templateUTF16, -1, template, n, NULL, NULL);
    1143       return _wopen(chkFilename(templateUTF16), flags, mode);
    1144     }
    1145   errno = EINVAL;
    1146   return -1;
    1147 
    1148 #else
    1149 
    1150   return mkstemp (template);
    1151 
    1152 #endif
    1153 }
    1154 
    11551124int
    11561125tr_variantToFile (const tr_variant  * v,
     
    11591128{
    11601129  char * tmp;
    1161   int fd;
     1130  tr_sys_file_t fd;
    11621131  int err = 0;
    11631132  char * real_filename;
     1133  tr_error * error = NULL;
    11641134
    11651135  /* follow symlinks to find the "real" file, to make sure the temporary
    1166    * we build with tr_mkstemp() is created on the right partition */
     1136   * we build with tr_sys_file_open_temp() is created on the right partition */
    11671137  if ((real_filename = tr_sys_path_resolve (filename, NULL)) != NULL)
    11681138    filename = real_filename;
     
    11701140  /* if the file already exists, try to move it out of the way & keep it as a backup */
    11711141  tmp = tr_strdup_printf ("%s.tmp.XXXXXX", filename);
    1172   fd = tr_mkstemp (tmp);
    1173   tr_set_file_for_single_pass (fd);
    1174   if (fd >= 0)
    1175     {
    1176       int nleft;
     1142  fd = tr_sys_file_open_temp (tmp, &error);
     1143  if (fd != TR_BAD_SYS_FILE)
     1144    {
     1145      uint64_t nleft;
    11771146
    11781147      /* save the variant to a temporary file */
     
    11841153        while (nleft > 0)
    11851154          {
    1186             const int n = write (fd, walk, nleft);
    1187             if (n >= 0)
     1155            uint64_t n;
     1156            if (!tr_sys_file_write (fd, walk, nleft, &n, &error))
    11881157              {
    1189                 nleft -= n;
    1190                 walk += n;
    1191               }
    1192             else if (errno != EAGAIN)
    1193               {
    1194                 err = errno;
     1158                err = error->code;
    11951159                break;
    11961160              }
     1161
     1162            nleft -= n;
     1163            walk += n;
    11971164          }
    11981165
     
    12001167      }
    12011168
     1169      tr_sys_file_close (fd, NULL);
     1170
    12021171      if (nleft > 0)
    12031172        {
    1204           tr_logAddError (_("Couldn't save temporary file \"%1$s\": %2$s"), tmp, tr_strerror (err));
    1205           tr_close_file (fd);
     1173          tr_logAddError (_("Couldn't save temporary file \"%1$s\": %2$s"), tmp, error->message);
    12061174          tr_sys_path_remove (tmp, NULL);
     1175          tr_error_free (error);
    12071176        }
    12081177      else
    12091178        {
    1210           tr_error * error = NULL;
    1211 
    1212           tr_close_file (fd);
    1213 
     1179          tr_error_clear (&error);
    12141180          if (tr_sys_path_rename (tmp, filename, &error))
    12151181            {
     
    12271193  else
    12281194    {
    1229       err = errno;
    1230       tr_logAddError (_("Couldn't save temporary file \"%1$s\": %2$s"), tmp, tr_strerror (err));
     1195      err = error->code;
     1196      tr_logAddError (_("Couldn't save temporary file \"%1$s\": %2$s"), tmp, error->message);
     1197      tr_error_free (error);
    12311198    }
    12321199
  • trunk/libtransmission/verify.c

    r14241 r14327  
    2020#include "transmission.h"
    2121#include "completion.h"
    22 #include "fdlimit.h"
     22#include "file.h"
    2323#include "list.h"
    2424#include "log.h"
     
    4242  time_t end;
    4343  SHA_CTX sha;
    44   int fd = -1;
    45   int64_t filePos = 0;
     44  tr_sys_file_t fd = TR_BAD_SYS_FILE;
     45  uint64_t filePos = 0;
    4646  bool changed = 0;
    4747  bool hadPiece = 0;
     
    6161  while (!*stopFlag && (pieceIndex < tor->info.pieceCount))
    6262    {
    63       uint32_t leftInPiece;
    64       uint32_t bytesThisPass;
     63      uint64_t leftInPiece;
     64      uint64_t bytesThisPass;
    6565      uint64_t leftInFile;
    6666      const tr_file * file = &tor->info.files[fileIndex];
     
    7171
    7272      /* if we're starting a new file... */
    73       if (!filePos && (fd<0) && (fileIndex!=prevFileIndex))
     73      if (filePos == 0 && fd == TR_BAD_SYS_FILE && fileIndex != prevFileIndex)
    7474        {
    7575          char * filename = tr_torrentFindFile (tor, fileIndex);
    76           fd = filename == NULL ? -1 : tr_open_file_for_scanning (filename);
     76          fd = filename == NULL ? TR_BAD_SYS_FILE : tr_sys_file_open (filename,
     77               TR_SYS_FILE_READ | TR_SYS_FILE_SEQUENTIAL, 0, NULL);
    7778          tr_free (filename);
    7879          prevFileIndex = fileIndex;
     
    8687
    8788      /* read a bit */
    88       if (fd >= 0)
    89         {
    90           const ssize_t numRead = tr_pread (fd, buffer, bytesThisPass, filePos);
    91           if (numRead > 0)
     89      if (fd != TR_BAD_SYS_FILE)
     90        {
     91          uint64_t numRead;
     92          if (tr_sys_file_read_at (fd, buffer, bytesThisPass, filePos, &numRead, NULL) && numRead > 0)
    9293            {
    93               bytesThisPass = (uint32_t)numRead;
     94              bytesThisPass = numRead;
    9495              SHA1_Update (&sha, buffer, bytesThisPass);
    9596#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
     
    141142      if (leftInFile == 0)
    142143        {
    143           if (fd >= 0)
     144          if (fd != TR_BAD_SYS_FILE)
    144145            {
    145               tr_close_file (fd);
    146               fd = -1;
     146              tr_sys_file_close (fd, NULL);
     147              fd = TR_BAD_SYS_FILE;
    147148            }
    148149          fileIndex++;
     
    152153
    153154  /* cleanup */
    154   if (fd >= 0)
    155     tr_close_file (fd);
     155  if (fd != TR_BAD_SYS_FILE)
     156    tr_sys_file_close (fd, NULL);
    156157  free (buffer);
    157158
Note: See TracChangeset for help on using the changeset viewer.