]> granicus.if.org Git - transmission/commitdiff
Rename `tr_sys_file_prefetch` to `tr_sys_file_advise`
authorMike Gelfand <mikedld@mikedld.com>
Sat, 8 Jul 2017 13:09:37 +0000 (16:09 +0300)
committerMike Gelfand <mikedld@mikedld.com>
Sat, 8 Jul 2017 13:09:37 +0000 (16:09 +0300)
New function now supports two flags: will need and don't need.

libtransmission/file-posix.c
libtransmission/file-win32.c
libtransmission/file.h
libtransmission/inout.c
libtransmission/verify.c

index a5fbe998e5b31e953900ac54b78d2ca7ee2e49d4..b857be514df1863941e541f376bed35bb270615a 100644 (file)
@@ -743,16 +743,22 @@ bool tr_sys_file_truncate(tr_sys_file_t handle, uint64_t size, tr_error** error)
     return ret;
 }
 
-bool tr_sys_file_prefetch(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_error** error)
+bool tr_sys_file_advise(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_sys_file_advice_t advice, tr_error** error)
 {
     TR_ASSERT(handle != TR_BAD_SYS_FILE);
     TR_ASSERT(size > 0);
+    TR_ASSERT(advice == TR_SYS_FILE_ADVICE_WILL_NEED || advice == TR_SYS_FILE_ADVICE_DONT_NEED);
 
     bool ret = true;
 
 #if defined(HAVE_POSIX_FADVISE)
 
-    int code = posix_fadvise(handle, offset, size, POSIX_FADV_WILLNEED);
+    int const native_advice = advice == TR_SYS_FILE_ADVICE_WILL_NEED ? POSIX_FADV_WILLNEED :
+        (advice == TR_SYS_FILE_ADVICE_DONT_NEED ? POSIX_FADV_DONTNEED : POSIX_FADV_NORMAL);
+
+    TR_ASSERT(native_advice != POSIX_FADV_NORMAL);
+
+    int const code = posix_fadvise(handle, offset, size, native_advice);
 
     if (code != 0)
     {
@@ -762,9 +768,16 @@ bool tr_sys_file_prefetch(tr_sys_file_t handle, uint64_t offset, uint64_t size,
 
 #elif defined(__APPLE__)
 
-    struct radvisory radv;
-    radv.ra_offset = offset;
-    radv.ra_count = size;
+    if (advice != TR_SYS_FILE_ADVICE_WILL_NEED)
+    {
+        goto skip_darwin_fcntl;
+    }
+
+    struct radvisory const radv =
+    {
+        .ra_offset = offset,
+        .ra_count = size
+    };
 
     ret = fcntl(handle, F_RDADVISE, &radv) != -1;
 
@@ -773,6 +786,16 @@ bool tr_sys_file_prefetch(tr_sys_file_t handle, uint64_t offset, uint64_t size,
         set_system_error(error, errno);
     }
 
+skip_darwin_fcntl:
+
+#else
+
+    (void)handle;
+    (void)offset;
+    (void)size;
+    (void)advice;
+    (void)error;
+
 #endif
 
     return ret;
index 8bcb660d6cbae3769e730b16cd3be525f521ab3b..448fa3f40ac539a872ae8792a11601e8b90fcd00 100644 (file)
@@ -1098,14 +1098,16 @@ bool tr_sys_file_truncate(tr_sys_file_t handle, uint64_t size, tr_error** error)
     return ret;
 }
 
-bool tr_sys_file_prefetch(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_error** error)
+bool tr_sys_file_advise(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_sys_file_advice_t advice, tr_error** error)
 {
     TR_ASSERT(handle != TR_BAD_SYS_FILE);
     TR_ASSERT(size > 0);
+    TR_ASSERT(advice == TR_SYS_FILE_ADVICE_WILL_NEED || advice == TR_SYS_FILE_ADVICE_DONT_NEED);
 
     (void)handle;
     (void)offset;
     (void)size;
+    (void)advice;
     (void)error;
 
     bool ret = true;
index f69e4de6b232f74e00030181b2bffa268bb22ac0..a878c8e7cdbafc34884dac486cd2d8b32886d099 100644 (file)
@@ -96,6 +96,13 @@ typedef enum
 }
 tr_sys_path_get_info_flags_t;
 
+typedef enum
+{
+    TR_SYS_FILE_ADVICE_WILL_NEED,
+    TR_SYS_FILE_ADVICE_DONT_NEED
+}
+tr_sys_file_advice_t;
+
 typedef enum
 {
     TR_SYS_FILE_PREALLOC_SPARSE = (1 << 0)
@@ -435,7 +442,7 @@ bool tr_sys_file_flush(tr_sys_file_t handle, struct tr_error** error);
 bool tr_sys_file_truncate(tr_sys_file_t handle, uint64_t size, struct tr_error** error);
 
 /**
- * @brief Tell system to prefetch some part of file which is to be read soon.
+ * @brief Tell system to prefetch or discard some part of file which is [not] to be read soon.
  *
  * @param[in]  handle Valid file descriptor.
  * @param[in]  offset Offset in file to prefetch from.
@@ -445,7 +452,8 @@ bool tr_sys_file_truncate(tr_sys_file_t handle, uint64_t size, struct tr_error**
  *
  * @return `True` on success, `false` otherwise (with `error` set accordingly).
  */
-bool tr_sys_file_prefetch(tr_sys_file_t handle, uint64_t offset, uint64_t size, struct tr_error** error);
+bool tr_sys_file_advise(tr_sys_file_t handle, uint64_t offset, uint64_t size, tr_sys_file_advice_t advice,
+    struct tr_error** error);
 
 /**
  * @brief Preallocate file to specified size in full or sparse mode.
index c392a685e832ffad1af50063c2ae5de06d1bc9b9..78e8730a1563ab55d11c8dc4ed730f90377d451c 100644 (file)
@@ -134,7 +134,7 @@ static int readOrWriteBytes(tr_session* session, tr_torrent* tor, int ioMode, tr
         }
         else if (ioMode == TR_IO_PREFETCH)
         {
-            tr_sys_file_prefetch(fd, fileOffset, buflen, NULL);
+            tr_sys_file_advise(fd, fileOffset, buflen, TR_SYS_FILE_ADVICE_WILL_NEED, NULL);
         }
         else
         {
index 1fc2530791f7ed55d05953fbce63eee896fa767e..b4f0d605549268af9ab33faaf63431362e6fd613 100644 (file)
@@ -6,18 +6,9 @@
  *
  */
 
-#if defined(HAVE_POSIX_FADVISE) && (!defined(_XOPEN_SOURCE) || _XOPEN_SOURCE < 600)
-#undef _XOPEN_SOURCE
-#define _XOPEN_SOURCE 600
-#endif
-
 #include <string.h> /* memcmp() */
 #include <stdlib.h> /* free() */
 
-#ifdef HAVE_POSIX_FADVISE
-#include <fcntl.h> /* posix_fadvise() */
-#endif
-
 #include "transmission.h"
 #include "completion.h"
 #include "crypto-utils.h"
@@ -99,9 +90,7 @@ static bool verifyTorrent(tr_torrent* tor, bool* stopFlag)
             {
                 bytesThisPass = numRead;
                 tr_sha1_update(sha, buffer, bytesThisPass);
-#if defined HAVE_POSIX_FADVISE && defined POSIX_FADV_DONTNEED
-                (void)posix_fadvise(fd, filePos, bytesThisPass, POSIX_FADV_DONTNEED);
-#endif
+                tr_sys_file_advise(fd, filePos, bytesThisPass, TR_SYS_FILE_ADVICE_DONT_NEED, NULL);
             }
         }