]> granicus.if.org Git - transmission/commitdiff
(trunk libT) add an "existing dir" argument to tr_fdFileCheckout().
authorJordan Lee <jordan@transmissionbt.com>
Thu, 3 Mar 2011 19:59:12 +0000 (19:59 +0000)
committerJordan Lee <jordan@transmissionbt.com>
Thu, 3 Mar 2011 19:59:12 +0000 (19:59 +0000)
existing_dir: An ancestor of filename which must already exist and won't be created by tr_fdFileCheckout(). In implementation this is the download directory and prevents directories from being created in error, such as a mount point for an external drive when the drive is unplugged.

libtransmission/fdlimit.c
libtransmission/fdlimit.h
libtransmission/inout.c
libtransmission/inout.h

index 73edf6dbcb1e8c3b33f5e2babf3b683f5aaf5f31..f819c1c7d84c6376c96150a941e88f0343175341 100644 (file)
@@ -350,6 +350,7 @@ cached_file_close( struct tr_cached_file * o )
  */
 static int
 cached_file_open( struct tr_cached_file  * o,
+                  const char             * existing_dir,
                   const char             * filename,
                   tr_bool                  writable,
                   tr_preallocation_mode    allocation,
@@ -359,6 +360,14 @@ cached_file_open( struct tr_cached_file  * o,
     struct stat sb;
     tr_bool alreadyExisted;
 
+    /* confirm that existing_dir, if specified, exists on the disk */
+    if( existing_dir && *existing_dir && stat( existing_dir, &sb ) )
+    {
+        const int err = errno;
+        tr_err( _( "Couldn't open \"%1$s\": %2$s" ), existing_dir, tr_strerror( err ) );
+        return err;
+    }
+
     /* create subfolders, if any */
     if( writable )
     {
@@ -565,6 +574,7 @@ int
 tr_fdFileCheckout( tr_session             * session,
                    int                      torrent_id,
                    tr_file_index_t          i,
+                   const char             * existing_dir,
                    const char             * filename,
                    tr_bool                  writable,
                    tr_preallocation_mode    allocation,
@@ -580,7 +590,7 @@ tr_fdFileCheckout( tr_session             * session,
 
     if( !cached_file_is_open( o ) )
     {
-        const int err = cached_file_open( o, filename, writable, allocation, file_size );
+        const int err = cached_file_open( o, existing_dir, filename, writable, allocation, file_size );
         if( err ) {
             errno = err;
             return -1;
index 4ecf9012718f2162108a46f980cbb2ad855e8cf7..78e564bb128584247a26e24697eceefdc19cd04b 100644 (file)
@@ -54,8 +54,13 @@ int tr_prefetch(int fd, off_t offset, size_t count);
  * continually opening and closing the same files when downloading
  * piece data.
  *
- * - if doWrite is true, subfolders in torrentFile are created if necessary.
- * - if doWrite is true, the target file is created if necessary.
+ * - if do_write is true, subfolders in torrentFile are created if necessary.
+ * - if do_write is true, the target file is created if necessary.
+ *
+ * @param existing_dir An ancestor of filename which must already exist and
+ *                     won't be created by tr_fdFileCheckout(). This prevents
+ *                     directories from being created in error, such as a mount
+ *                     point for an external drive when the drive is unplugged.
  *
  * on success, a file descriptor >= 0 is returned.
  * on failure, a -1 is returned and errno is set.
@@ -63,16 +68,17 @@ int tr_prefetch(int fd, off_t offset, size_t count);
  * @see tr_fdFileClose
  */
 int  tr_fdFileCheckout( tr_session             * session,
-                        int                      torrentId,
-                        tr_file_index_t          fileNum,
-                        const char             * fileName,
-                        tr_bool                  doWrite,
-                        tr_preallocation_mode    preallocationMode,
-                        uint64_t                 desiredFileSize );
+                        int                      torrent_id,
+                        tr_file_index_t          file_num,
+                        const char             * existing_dir,
+                        const char             * filename,
+                        tr_bool                  do_write,
+                        tr_preallocation_mode    preallocation_mode,
+                        uint64_t                 preallocation_file_size );
 
 int tr_fdFileGetCached( tr_session             * session,
-                        int                      torrentId,
-                        tr_file_index_t          fileNum,
+                        int                      torrent_id,
+                        tr_file_index_t          file_num,
                         tr_bool                  doWrite );
 
 /**
@@ -85,7 +91,7 @@ int tr_fdFileGetCached( tr_session             * session,
  */
 void tr_fdFileClose( tr_session        * session,
                      const tr_torrent  * tor,
-                     tr_file_index_t     fileNo );
+                     tr_file_index_t     file_num );
 
 
 /**
index c611dea4dd7f9982185eef00652a00ccd9905d10..83c8e94850a05157997c8ce978e15dfddd05f4db 100644 (file)
@@ -120,7 +120,8 @@ readOrWriteBytes( tr_session       * session,
         {
             char * filename = tr_buildPath( base, subpath, NULL );
 
-            if( ( fd = tr_fdFileCheckout( session, tor->uniqueId, fileIndex, filename,
+            if( ( fd = tr_fdFileCheckout( session, tor->uniqueId, fileIndex,
+                                          base, filename,
                                           doWrite, preallocationMode, file->length ) ) < 0 )
             {
                 err = errno;
index dc6b8c925cc381a1415e1b1a8ed43a9052cc01b0..967def41baf5c238b9c7533316f8b828cc3510f2 100644 (file)
@@ -60,11 +60,11 @@ tr_bool tr_ioTestPiece( tr_torrent       * tor,
 /**
  * Converts a piece index + offset into a file index + offset.
  */
-void     tr_ioFindFileLocation( const tr_torrent * tor,
-                                tr_piece_index_t   pieceIndex,
-                                uint32_t           pieceOffset,
-                                tr_file_index_t *  fileIndex,
-                                uint64_t *         fileOffset );
+void tr_ioFindFileLocation( const tr_torrent  * tor,
+                             tr_piece_index_t   pieceIndex,
+                             uint32_t           pieceOffset,
+                             tr_file_index_t  * fileIndex,
+                             uint64_t         * fileOffset );
 
 
 /* @} */