Changeset 13681


Ignore:
Timestamp:
Dec 18, 2012, 3:03:23 AM (9 years ago)
Author:
jordan
Message:

copyediting: indentation/whitespace

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/libtransmission/fdlimit.c

    r13631 r13681  
    7676preallocate_file_sparse (int fd, uint64_t length)
    7777{
    78     const char zero = '\0';
    79     bool success = 0;
    80 
    81     if (!length)
    82         success = true;
     78  const char zero = '\0';
     79  bool success = 0;
     80
     81  if (!length)
     82    success = true;
    8383
    8484#ifdef HAVE_FALLOCATE64
    85     if (!success) /* fallocate64 is always preferred, so try it first */
    86         success = !fallocate64 (fd, 0, 0, length);
    87 #endif
    88 
    89     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);
    93 
    94     return success;
     85  if (!success) /* fallocate64 is always preferred, so try it first */
     86    success = !fallocate64 (fd, 0, 0, length);
     87#endif
     88
     89  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);
     93
     94  return success;
    9595}
    9696
     
    9898preallocate_file_full (const char * filename, uint64_t length)
    9999{
    100     bool success = 0;
     100  bool success = 0;
    101101
    102102#ifdef WIN32
    103103
    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);
     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);
    111111    }
    112112
    113113#else
    114114
    115     int flags = O_RDWR | O_CREAT | O_LARGEFILE;
    116     int fd = open (filename, flags, 0666);
    117     if (fd >= 0)
     115  int flags = O_RDWR | O_CREAT | O_LARGEFILE;
     116  int fd = open (filename, flags, 0666);
     117  if (fd >= 0)
    118118    {
    119119# ifdef HAVE_FALLOCATE64
    120        if (!success)
    121        {
    122            success = !fallocate64 (fd, 0, 0, length);
    123        }
     120      if (!success)
     121        success = !fallocate64 (fd, 0, 0, length);
    124122# endif
    125123# ifdef HAVE_XFS_XFS_H
    126         if (!success && platform_test_xfs_fd (fd))
    127         {
    128             xfs_flock64_t fl;
    129             fl.l_whence = 0;
    130             fl.l_start = 0;
    131             fl.l_len = length;
    132             success = !xfsctl (NULL, fd, XFS_IOC_RESVSP64, &fl);
     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);
    133131        }
    134132# endif
    135133# ifdef SYS_DARWIN
    136         if (!success)
    137         {
    138             fstore_t fst;
    139             fst.fst_flags = F_ALLOCATECONTIG;
    140             fst.fst_posmode = F_PEOFPOSMODE;
    141             fst.fst_offset = 0;
    142             fst.fst_length = length;
    143             fst.fst_bytesalloc = 0;
    144             success = !fcntl (fd, F_PREALLOCATE, &fst);
     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);
    145143        }
    146144# endif
    147145# ifdef HAVE_POSIX_FALLOCATE
    148         if (!success)
    149         {
    150             success = !posix_fallocate (fd, 0, length);
    151         }
     146      if (!success)
     147        success = !posix_fallocate (fd, 0, length);
    152148# endif
    153149
    154         if (!success) /* if nothing else works, do it the old-fashioned way */
    155         {
    156             uint8_t buf[ 4096 ];
    157             memset (buf, 0, sizeof (buf));
    158             success = true;
    159             while (success && (length > 0))
     150      if (!success) /* if nothing else works, do it the old-fashioned way */
     151        {
     152          uint8_t buf[ 4096 ];
     153          memset (buf, 0, sizeof (buf));
     154          success = true;
     155          while (success && (length > 0))
    160156            {
    161                 const int thisPass = MIN (length, sizeof (buf));
    162                 success = write (fd, buf, thisPass) == thisPass;
    163                 length -= thisPass;
     157              const int thisPass = MIN (length, sizeof (buf));
     158              success = write (fd, buf, thisPass) == thisPass;
     159              length -= thisPass;
    164160            }
    165161        }
    166162
    167         close (fd);
    168     }
    169 
    170 #endif
    171 
    172     return success;
     163      close (fd);
     164    }
     165
     166#endif
     167
     168  return success;
    173169}
    174170
     
    179175{
    180176#ifdef WIN32
    181     return _commit (fd);
     177  return _commit (fd);
    182178#else
    183     return fsync (fd);
     179  return fsync (fd);
    184180#endif
    185181}
     
    212208{
    213209#ifdef HAVE_PREAD
    214     return pread (fd, buf, count, offset);
     210  return pread (fd, buf, count, offset);
    215211#else
    216     const off_t lrc = lseek (fd, offset, SEEK_SET);
    217     if (lrc < 0)
    218         return -1;
    219     return read (fd, buf, count);
     212  const off_t lrc = lseek (fd, offset, SEEK_SET);
     213  if (lrc < 0)
     214    return -1;
     215  return read (fd, buf, count);
    220216#endif
    221217}
     
    225221{
    226222#ifdef HAVE_PWRITE
    227     return pwrite (fd, buf, count, offset);
     223  return pwrite (fd, buf, count, offset);
    228224#else
    229     const off_t lrc = lseek (fd, offset, SEEK_SET);
    230     if (lrc < 0)
    231         return -1;
    232     return write (fd, buf, count);
     225  const off_t lrc = lseek (fd, offset, SEEK_SET);
     226  if (lrc < 0)
     227    return -1;
     228  return write (fd, buf, count);
    233229#endif
    234230}
     
    238234{
    239235#ifdef HAVE_POSIX_FADVISE
    240     return posix_fadvise (fd, offset, count, POSIX_FADV_WILLNEED);
     236  return posix_fadvise (fd, offset, count, POSIX_FADV_WILLNEED);
    241237#elif defined (SYS_DARWIN)
    242     struct radvisory radv;
    243     radv.ra_offset = offset;
    244     radv.ra_count = count;
    245     return fcntl (fd, F_RDADVISE, &radv);
     238  struct radvisory radv;
     239  radv.ra_offset = offset;
     240  radv.ra_count = count;
     241  return fcntl (fd, F_RDADVISE, &radv);
    246242#else
    247     return 0;
     243  return 0;
    248244#endif
    249245}
     
    252248tr_set_file_for_single_pass (int fd)
    253249{
    254     if (fd >= 0)
    255     {
    256         /* Set hints about the lookahead buffer and caching. It's okay
    257            for these to fail silently, so don't let them affect errno */
    258         const int err = errno;
     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;
    259255#ifdef HAVE_POSIX_FADVISE
    260         posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
     256      posix_fadvise (fd, 0, 0, POSIX_FADV_SEQUENTIAL);
    261257#endif
    262258#ifdef SYS_DARWIN
    263         fcntl (fd, F_RDAHEAD, 1);
    264         fcntl (fd, F_NOCACHE, 1);
    265 #endif
    266         errno = err;
     259      fcntl (fd, F_RDAHEAD, 1);
     260      fcntl (fd, F_NOCACHE, 1);
     261#endif
     262      errno = err;
    267263    }
    268264}
     
    271267open_local_file (const char * filename, int flags)
    272268{
    273     const int fd = open (filename, flags, 0666);
    274     tr_set_file_for_single_pass (fd);
    275     return fd;
     269  const int fd = open (filename, flags, 0666);
     270  tr_set_file_for_single_pass (fd);
     271  return fd;
    276272}
    277273int
    278274tr_open_file_for_writing (const char * filename)
    279275{
    280     return open_local_file (filename, O_LARGEFILE|O_BINARY|O_CREAT|O_WRONLY);
     276  return open_local_file (filename, O_LARGEFILE|O_BINARY|O_CREAT|O_WRONLY);
    281277}
    282278int
    283279tr_open_file_for_scanning (const char * filename)
    284280{
    285     return open_local_file (filename, O_LARGEFILE|O_BINARY|O_SEQUENTIAL|O_RDONLY);
     281  return open_local_file (filename, O_LARGEFILE|O_BINARY|O_SEQUENTIAL|O_RDONLY);
    286282}
    287283
     
    290286{
    291287#if defined (HAVE_POSIX_FADVISE)
    292     /* Set hint about not caching this file.
    293        It's okay for this to fail silently, so don't let it affect errno */
    294     const int err = errno;
    295     posix_fadvise (fd, 0, 0, POSIX_FADV_DONTNEED);
    296     errno = err;
     288  /* Set hint about not caching this file.
     289     It's okay for this to fail silently, so don't let it affect errno */
     290  const int err = errno;
     291  posix_fadvise (fd, 0, 0, POSIX_FADV_DONTNEED);
     292  errno = err;
    297293#endif
    298294#ifdef SYS_DARWIN
    299     /* it's unclear to me from the man pages if this actually flushes out the cache,
    300      * but it couldn't hurt... */
    301     fcntl (fd, F_NOCACHE, 1);
    302 #endif
    303     close (fd);
     295  /* it's unclear to me from the man pages if this actually flushes out the cache,
     296   * but it couldn't hurt... */
     297  fcntl (fd, F_NOCACHE, 1);
     298#endif
     299  close (fd);
    304300}
    305301
     
    312308struct tr_cached_file
    313309{
    314     bool            is_writable;
    315     int              fd;
    316     int              torrent_id;
    317     tr_file_index_t file_index;
    318     time_t          used_at;
     310  bool is_writable;
     311  int fd;
     312  int torrent_id;
     313  tr_file_index_t file_index;
     314  time_t used_at;
    319315};
    320316
     
    322318cached_file_is_open (const struct tr_cached_file * o)
    323319{
    324     assert (o != NULL);
    325 
    326     return o->fd >= 0;
     320  assert (o != NULL);
     321
     322  return o->fd >= 0;
    327323}
    328324
     
    330326cached_file_close (struct tr_cached_file * o)
    331327{
    332     assert (cached_file_is_open (o));
    333 
    334     tr_close_file (o->fd);
    335     o->fd = -1;
     328  assert (cached_file_is_open (o));
     329
     330  tr_close_file (o->fd);
     331  o->fd = -1;
    336332}
    337333
     
    348344                  uint64_t                 file_size)
    349345{
    350     int flags;
    351     struct stat sb;
    352     bool alreadyExisted;
    353 
    354     /* create subfolders, if any */
    355     if (writable)
    356     {
    357         char * dir = tr_dirname (filename);
    358         const int err = tr_mkdirp (dir, 0777) ? errno : 0;
    359         if (err) {
    360             tr_err (_("Couldn't create \"%1$s\": %2$s"), dir, tr_strerror (err));
    361             tr_free (dir);
    362             return err;
    363         }
    364         tr_free (dir);
    365     }
    366 
    367     alreadyExisted = !stat (filename, &sb) && S_ISREG (sb.st_mode);
    368 
    369     if (writable && !alreadyExisted && (allocation == TR_PREALLOCATE_FULL))
    370         if (preallocate_file_full (filename, file_size))
    371             tr_dbg ("Preallocated file \"%s\"", filename);
    372 
    373     /* open the file */
    374     flags = writable ? (O_RDWR | O_CREAT) : O_RDONLY;
    375     flags |= O_LARGEFILE | O_BINARY | O_SEQUENTIAL;
    376     o->fd = open (filename, flags, 0666);
    377 
    378     if (o->fd == -1)
    379     {
    380         const int err = errno;
    381         tr_err (_("Couldn't open \"%1$s\": %2$s"), filename, tr_strerror (err));
    382         return err;
    383     }
    384 
    385     /* If the file already exists and it's too large, truncate it.
    386      * This is a fringe case that happens if a torrent's been updated
    387      * and one of the updated torrent's files is smaller.
    388      * http://trac.transmissionbt.com/ticket/2228
    389      * https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
    390      */
    391     if (alreadyExisted && (file_size < (uint64_t)sb.st_size))
    392     {
    393         if (ftruncate (o->fd, file_size) == -1)
    394         {
    395             const int err = errno;
    396             tr_err (_("Couldn't truncate \"%1$s\": %2$s"), filename, tr_strerror (err));
    397             return err;
    398         }
    399     }
    400 
    401     if (writable && !alreadyExisted && (allocation == TR_PREALLOCATE_SPARSE))
    402         preallocate_file_sparse (o->fd, file_size);
    403 
    404     /* Many (most?) clients request blocks in ascending order,
    405      * so increase the readahead buffer.
    406      * Also, disable OS-level caching because "inactive memory" angers users. */
    407     tr_set_file_for_single_pass (o->fd);
    408 
    409     return 0;
     346  int flags;
     347  struct stat sb;
     348  bool alreadyExisted;
     349
     350  /* create subfolders, if any */
     351  if (writable)
     352    {
     353      char * dir = tr_dirname (filename);
     354      const int err = tr_mkdirp (dir, 0777) ? errno : 0;
     355      if (err)
     356        {
     357          tr_err (_("Couldn't create \"%1$s\": %2$s"), dir, tr_strerror (err));
     358          tr_free (dir);
     359          return err;
     360        }
     361      tr_free (dir);
     362    }
     363
     364  alreadyExisted = !stat (filename, &sb) && S_ISREG (sb.st_mode);
     365
     366  if (writable && !alreadyExisted && (allocation == TR_PREALLOCATE_FULL))
     367    if (preallocate_file_full (filename, file_size))
     368      tr_dbg ("Preallocated file \"%s\"", filename);
     369
     370  /* open the file */
     371  flags = writable ? (O_RDWR | O_CREAT) : O_RDONLY;
     372  flags |= O_LARGEFILE | O_BINARY | O_SEQUENTIAL;
     373  o->fd = open (filename, flags, 0666);
     374
     375  if (o->fd == -1)
     376    {
     377      const int err = errno;
     378      tr_err (_("Couldn't open \"%1$s\": %2$s"), filename, tr_strerror (err));
     379      return err;
     380    }
     381
     382  /* If the file already exists and it's too large, truncate it.
     383   * This is a fringe case that happens if a torrent's been updated
     384   * and one of the updated torrent's files is smaller.
     385   * http://trac.transmissionbt.com/ticket/2228
     386   * https://bugs.launchpad.net/ubuntu/+source/transmission/+bug/318249
     387   */
     388  if (alreadyExisted && (file_size < (uint64_t)sb.st_size))
     389    {
     390      if (ftruncate (o->fd, file_size) == -1)
     391        {
     392          const int err = errno;
     393          tr_err (_("Couldn't truncate \"%1$s\": %2$s"), filename, tr_strerror (err));
     394          return err;
     395        }
     396    }
     397
     398  if (writable && !alreadyExisted && (allocation == TR_PREALLOCATE_SPARSE))
     399    preallocate_file_sparse (o->fd, file_size);
     400
     401  /* Many (most?) clients request blocks in ascending order,
     402   * so increase the readahead buffer.
     403   * Also, disable OS-level caching because "inactive memory" angers users. */
     404  tr_set_file_for_single_pass (o->fd);
     405
     406  return 0;
    410407}
    411408
     
    416413struct tr_fileset
    417414{
    418     struct tr_cached_file * begin;
    419     const struct tr_cached_file * end;
     415  struct tr_cached_file * begin;
     416  const struct tr_cached_file * end;
    420417};
    421418
     
    423420fileset_construct (struct tr_fileset * set, int n)
    424421{
    425     struct tr_cached_file * o;
    426     const struct tr_cached_file TR_CACHED_FILE_INIT = { 0, -1, 0, 0, 0 };
    427 
    428     set->begin = tr_new (struct tr_cached_file, n);
    429     set->end = set->begin + n;
    430 
    431     for (o=set->begin; o!=set->end; ++o)
    432         *o = TR_CACHED_FILE_INIT;
     422  struct tr_cached_file * o;
     423  const struct tr_cached_file TR_CACHED_FILE_INIT = { 0, -1, 0, 0, 0 };
     424
     425  set->begin = tr_new (struct tr_cached_file, n);
     426  set->end = set->begin + n;
     427
     428  for (o=set->begin; o!=set->end; ++o)
     429    *o = TR_CACHED_FILE_INIT;
    433430}
    434431
     
    436433fileset_close_all (struct tr_fileset * set)
    437434{
    438     struct tr_cached_file * o;
    439 
    440     if (set != NULL)
    441         for (o=set->begin; o!=set->end; ++o)
    442             if (cached_file_is_open (o))
    443                 cached_file_close (o);
     435  struct tr_cached_file * o;
     436
     437  if (set != NULL)
     438    for (o=set->begin; o!=set->end; ++o)
     439      if (cached_file_is_open (o))
     440        cached_file_close (o);
    444441}
    445442
     
    447444fileset_destruct (struct tr_fileset * set)
    448445{
    449     fileset_close_all (set);
    450     tr_free (set->begin);
    451     set->end = set->begin = NULL;
     446  fileset_close_all (set);
     447  tr_free (set->begin);
     448  set->end = set->begin = NULL;
    452449}
    453450
     
    455452fileset_close_torrent (struct tr_fileset * set, int torrent_id)
    456453{
    457     struct tr_cached_file * o;
    458 
    459     if (set != NULL)
    460         for (o=set->begin; o!=set->end; ++o)
    461             if ((o->torrent_id == torrent_id) && cached_file_is_open (o))
    462                 cached_file_close (o);
     454  struct tr_cached_file * o;
     455
     456  if (set != NULL)
     457    for (o=set->begin; o!=set->end; ++o)
     458      if ((o->torrent_id == torrent_id) && cached_file_is_open (o))
     459        cached_file_close (o);
    463460}
    464461
     
    466463fileset_lookup (struct tr_fileset * set, int torrent_id, tr_file_index_t i)
    467464{
    468     struct tr_cached_file * o;
    469 
    470     if (set != NULL)
    471         for (o=set->begin; o!=set->end; ++o)
    472             if ((torrent_id == o->torrent_id) && (i == o->file_index) && cached_file_is_open (o))
    473                 return o;
    474 
    475     return NULL;
     465  struct tr_cached_file * o;
     466
     467  if (set != NULL)
     468    for (o=set->begin; o!=set->end; ++o)
     469      if ((torrent_id == o->torrent_id) && (i == o->file_index) && cached_file_is_open (o))
     470        return o;
     471
     472  return NULL;
    476473}
    477474
     
    479476fileset_get_empty_slot (struct tr_fileset * set)
    480477{
    481     struct tr_cached_file * cull = NULL;
    482 
    483     if (set->begin != NULL)
    484     {
    485         struct tr_cached_file * o;
    486 
    487         /* try to find an unused slot */
    488         for (o=set->begin; o!=set->end; ++o)
    489             if (!cached_file_is_open (o))
    490                 return o;
    491 
    492         /* all slots are full... recycle the least recently used */
    493         for (cull=NULL, o=set->begin; o!=set->end; ++o)
    494             if (!cull || o->used_at < cull->used_at)
    495                 cull = o;
    496 
    497         cached_file_close (cull);
    498     }
    499 
    500     return cull;
     478  struct tr_cached_file * cull = NULL;
     479
     480  if (set->begin != NULL)
     481    {
     482      struct tr_cached_file * o;
     483
     484      /* try to find an unused slot */
     485      for (o=set->begin; o!=set->end; ++o)
     486        if (!cached_file_is_open (o))
     487          return o;
     488
     489      /* all slots are full... recycle the least recently used */
     490      for (cull=NULL, o=set->begin; o!=set->end; ++o)
     491        if (!cull || o->used_at < cull->used_at)
     492          cull = o;
     493
     494      cached_file_close (cull);
     495    }
     496
     497  return cull;
    501498}
    502499
     
    509506struct tr_fdInfo
    510507{
    511     int peerCount;
    512     struct tr_fileset fileset;
     508  int peerCount;
     509  struct tr_fileset fileset;
    513510};
    514511
     
    516513ensureSessionFdInfoExists (tr_session * session)
    517514{
    518     assert (tr_isSession (session));
    519 
    520     if (session->fdInfo == NULL)
    521     {
    522         struct rlimit limit;
    523         struct tr_fdInfo * i;
    524         const int FILE_CACHE_SIZE = 32;
    525 
    526         /* Create the local file cache */
    527         i = tr_new0 (struct tr_fdInfo, 1);
    528         fileset_construct (&i->fileset, FILE_CACHE_SIZE);
    529         session->fdInfo = i;
    530 
    531         /* set the open-file limit to the largest safe size wrt FD_SETSIZE */
    532         if (!getrlimit (RLIMIT_NOFILE, &limit))
    533         {
    534             const int old_limit = (int) limit.rlim_cur;
    535             const int new_limit = MIN (limit.rlim_max, FD_SETSIZE);
    536             if (new_limit != old_limit)
     515  assert (tr_isSession (session));
     516
     517  if (session->fdInfo == NULL)
     518    {
     519      struct rlimit limit;
     520      struct tr_fdInfo * i;
     521      const int FILE_CACHE_SIZE = 32;
     522
     523      /* Create the local file cache */
     524      i = tr_new0 (struct tr_fdInfo, 1);
     525      fileset_construct (&i->fileset, FILE_CACHE_SIZE);
     526      session->fdInfo = i;
     527
     528      /* set the open-file limit to the largest safe size wrt FD_SETSIZE */
     529      if (!getrlimit (RLIMIT_NOFILE, &limit))
     530        {
     531          const int old_limit = (int) limit.rlim_cur;
     532          const int new_limit = MIN (limit.rlim_max, FD_SETSIZE);
     533          if (new_limit != old_limit)
    537534            {
    538                 limit.rlim_cur = new_limit;
    539                 setrlimit (RLIMIT_NOFILE, &limit);
    540                 getrlimit (RLIMIT_NOFILE, &limit);
    541                 tr_inf ("Changed open file limit from %d to %d", old_limit, (int)limit.rlim_cur);
     535              limit.rlim_cur = new_limit;
     536              setrlimit (RLIMIT_NOFILE, &limit);
     537              getrlimit (RLIMIT_NOFILE, &limit);
     538              tr_inf ("Changed open file limit from %d to %d", old_limit, (int)limit.rlim_cur);
    542539            }
    543540        }
     
    548545tr_fdClose (tr_session * session)
    549546{
    550     if (session && session->fdInfo)
    551     {
    552         struct tr_fdInfo * i = session->fdInfo;
    553         fileset_destruct (&i->fileset);
    554         tr_free (i);
    555         session->fdInfo = NULL;
     547  if (session && session->fdInfo)
     548    {
     549      struct tr_fdInfo * i = session->fdInfo;
     550      fileset_destruct (&i->fileset);
     551      tr_free (i);
     552      session->fdInfo = NULL;
    556553    }
    557554}
     
    564561get_fileset (tr_session * session)
    565562{
    566     if (!session)
    567         return NULL;
    568 
    569     ensureSessionFdInfoExists (session);
    570     return &session->fdInfo->fileset;
     563  if (!session)
     564    return NULL;
     565
     566  ensureSessionFdInfoExists (session);
     567  return &session->fdInfo->fileset;
    571568}
    572569
     
    574571tr_fdFileClose (tr_session * s, const tr_torrent * tor, tr_file_index_t i)
    575572{
    576     struct tr_cached_file * o;
    577 
    578     if ((o = fileset_lookup (get_fileset (s), tr_torrentId (tor), i)))
    579     {
    580         /* flush writable files so that their mtimes will be
    581          * up-to-date when this function returns to the caller... */
    582         if (o->is_writable)
    583             tr_fsync (o->fd);
    584 
    585         cached_file_close (o);
     573  struct tr_cached_file * o;
     574
     575  if ((o = fileset_lookup (get_fileset (s), tr_torrentId (tor), i)))
     576    {
     577      /* flush writable files so that their mtimes will be
     578       * up-to-date when this function returns to the caller... */
     579      if (o->is_writable)
     580        tr_fsync (o->fd);
     581
     582      cached_file_close (o);
    586583    }
    587584}
     
    590587tr_fdFileGetCached (tr_session * s, int torrent_id, tr_file_index_t i, bool writable)
    591588{
    592     struct tr_cached_file * o = fileset_lookup (get_fileset (s), torrent_id, i);
    593 
    594     if (!o || (writable && !o->is_writable))
    595         return -1;
    596 
    597     o->used_at = tr_time ();
    598     return o->fd;
     589  struct tr_cached_file * o = fileset_lookup (get_fileset (s), torrent_id, i);
     590
     591  if (!o || (writable && !o->is_writable))
     592    return -1;
     593
     594  o->used_at = tr_time ();
     595  return o->fd;
    599596}
    600597
     
    608605tr_fdFileGetCachedMTime (tr_session * s, int torrent_id, tr_file_index_t i, time_t * mtime)
    609606{
    610     bool success;
    611     struct stat sb;
    612     struct tr_cached_file * o = fileset_lookup (get_fileset (s), torrent_id, i);
    613 
    614     if ((success = (o != NULL) && !fstat (o->fd, &sb)))
    615         *mtime = TR_STAT_MTIME (sb);
    616 
    617     return success;
     607  bool success;
     608  struct stat sb;
     609  struct tr_cached_file * o = fileset_lookup (get_fileset (s), torrent_id, i);
     610
     611  if ((success = (o != NULL) && !fstat (o->fd, &sb)))
     612    *mtime = TR_STAT_MTIME (sb);
     613
     614  return success;
    618615}
    619616
     
    634631                   uint64_t                 file_size)
    635632{
    636     struct tr_fileset * set = get_fileset (session);
    637     struct tr_cached_file * o = fileset_lookup (set, torrent_id, i);
    638 
    639     if (o && writable && !o->is_writable)
    640         cached_file_close (o); /* close it so we can reopen in rw mode */
    641     else if (!o)
    642         o = fileset_get_empty_slot (set);
    643 
    644     if (!cached_file_is_open (o))
    645     {
    646         const int err = cached_file_open (o, filename, writable, allocation, file_size);
    647         if (err) {
    648             errno = err;
    649             return -1;
    650         }
    651 
    652         dbgmsg ("opened '%s' writable %c", filename, writable?'y':'n');
    653         o->is_writable = writable;
    654     }
    655 
    656     dbgmsg ("checking out '%s'", filename);
    657     o->torrent_id = torrent_id;
    658     o->file_index = i;
    659     o->used_at = tr_time ();
    660     return o->fd;
     633  struct tr_fileset * set = get_fileset (session);
     634  struct tr_cached_file * o = fileset_lookup (set, torrent_id, i);
     635
     636  if (o && writable && !o->is_writable)
     637    cached_file_close (o); /* close it so we can reopen in rw mode */
     638  else if (!o)
     639    o = fileset_get_empty_slot (set);
     640
     641  if (!cached_file_is_open (o))
     642    {
     643      const int err = cached_file_open (o, filename, writable, allocation, file_size);
     644      if (err)
     645        {
     646          errno = err;
     647          return -1;
     648        }
     649
     650      dbgmsg ("opened '%s' writable %c", filename, writable?'y':'n');
     651      o->is_writable = writable;
     652    }
     653
     654  dbgmsg ("checking out '%s'", filename);
     655  o->torrent_id = torrent_id;
     656  o->file_index = i;
     657  o->used_at = tr_time ();
     658  return o->fd;
    661659}
    662660
     
    670668tr_fdSocketCreate (tr_session * session, int domain, int type)
    671669{
    672     int s = -1;
    673     struct tr_fdInfo * gFd;
    674     assert (tr_isSession (session));
    675 
    676     ensureSessionFdInfoExists (session);
    677     gFd = session->fdInfo;
    678 
    679     if (gFd->peerCount < session->peerLimit)
    680         if ((s = socket (domain, type, 0)) < 0)
    681             if (sockerrno != EAFNOSUPPORT)
    682                 tr_err (_("Couldn't create socket: %s"), tr_strerror (sockerrno));
    683 
    684     if (s > -1)
    685         ++gFd->peerCount;
    686 
    687     assert (gFd->peerCount >= 0);
    688 
    689     if (s >= 0)
    690     {
    691         static bool buf_logged = false;
    692         if (!buf_logged)
    693         {
    694             int i;
    695             socklen_t size = sizeof (int);
    696             buf_logged = true;
    697             getsockopt (s, SOL_SOCKET, SO_SNDBUF, &i, &size);
    698             tr_dbg ("SO_SNDBUF size is %d", i);
    699             getsockopt (s, SOL_SOCKET, SO_RCVBUF, &i, &size);
    700             tr_dbg ("SO_RCVBUF size is %d", i);
    701         }
    702     }
    703 
    704     return s;
     670  int s = -1;
     671  struct tr_fdInfo * gFd;
     672  assert (tr_isSession (session));
     673
     674  ensureSessionFdInfoExists (session);
     675  gFd = session->fdInfo;
     676
     677  if (gFd->peerCount < session->peerLimit)
     678    if ((s = socket (domain, type, 0)) < 0)
     679      if (sockerrno != EAFNOSUPPORT)
     680        tr_err (_("Couldn't create socket: %s"), tr_strerror (sockerrno));
     681
     682  if (s > -1)
     683    ++gFd->peerCount;
     684
     685  assert (gFd->peerCount >= 0);
     686
     687  if (s >= 0)
     688    {
     689      static bool buf_logged = false;
     690      if (!buf_logged)
     691        {
     692          int i;
     693          socklen_t size = sizeof (int);
     694          buf_logged = true;
     695          getsockopt (s, SOL_SOCKET, SO_SNDBUF, &i, &size);
     696          tr_dbg ("SO_SNDBUF size is %d", i);
     697          getsockopt (s, SOL_SOCKET, SO_RCVBUF, &i, &size);
     698          tr_dbg ("SO_RCVBUF size is %d", i);
     699        }
     700    }
     701
     702  return s;
    705703}
    706704
     
    708706tr_fdSocketAccept (tr_session * s, int sockfd, tr_address * addr, tr_port * port)
    709707{
    710     int fd;
    711     unsigned int len;
    712     struct tr_fdInfo * gFd;
    713     struct sockaddr_storage sock;
    714 
    715     assert (tr_isSession (s));
    716     assert (addr);
    717     assert (port);
    718 
    719     ensureSessionFdInfoExists (s);
    720     gFd = s->fdInfo;
    721 
    722     len = sizeof (struct sockaddr_storage);
    723     fd = accept (sockfd, (struct sockaddr *) &sock, &len);
    724 
    725     if (fd >= 0)
    726     {
    727         if ((gFd->peerCount < s->peerLimit)
    728             && tr_address_from_sockaddr_storage (addr, port, &sock))
    729         {
    730             ++gFd->peerCount;
     708  int fd;
     709  unsigned int len;
     710  struct tr_fdInfo * gFd;
     711  struct sockaddr_storage sock;
     712
     713  assert (tr_isSession (s));
     714  assert (addr);
     715  assert (port);
     716
     717  ensureSessionFdInfoExists (s);
     718  gFd = s->fdInfo;
     719
     720  len = sizeof (struct sockaddr_storage);
     721  fd = accept (sockfd, (struct sockaddr *) &sock, &len);
     722
     723  if (fd >= 0)
     724    {
     725      if ((gFd->peerCount < s->peerLimit)
     726          && tr_address_from_sockaddr_storage (addr, port, &sock))
     727        {
     728          ++gFd->peerCount;
    731729        }
    732730        else
    733731        {
    734             tr_netCloseSocket (fd);
    735             fd = -1;
    736         }
    737     }
    738 
    739     return fd;
     732          tr_netCloseSocket (fd);
     733          fd = -1;
     734        }
     735    }
     736
     737  return fd;
    740738}
    741739
     
    743741tr_fdSocketClose (tr_session * session, int fd)
    744742{
    745     assert (tr_isSession (session));
    746 
    747     if (session->fdInfo != NULL)
    748     {
    749         struct tr_fdInfo * gFd = session->fdInfo;
    750 
    751         if (fd >= 0)
    752         {
    753             tr_netCloseSocket (fd);
    754             --gFd->peerCount;
    755         }
    756 
    757         assert (gFd->peerCount >= 0);
    758     }
    759 }
     743  assert (tr_isSession (session));
     744
     745  if (session->fdInfo != NULL)
     746    {
     747      struct tr_fdInfo * gFd = session->fdInfo;
     748
     749      if (fd >= 0)
     750        {
     751          tr_netCloseSocket (fd);
     752          --gFd->peerCount;
     753        }
     754
     755      assert (gFd->peerCount >= 0);
     756    }
     757}
Note: See TracChangeset for help on using the changeset viewer.