summaryrefslogtreecommitdiff
path: root/src/common/compat.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2014-07-16 13:58:55 +0200
committerNick Mathewson <nickm@torproject.org>2014-07-16 13:58:55 +0200
commit867f5e6a7602fe5cf54686aa04995eaabaea4c80 (patch)
tree3954fab482ffc83324ae53a7d252c5479ddc09b9 /src/common/compat.c
parent15e170e01b3f67325f952d0becda0438fa879907 (diff)
downloadtor-867f5e6a7602fe5cf54686aa04995eaabaea4c80.tar.gz
tor-867f5e6a7602fe5cf54686aa04995eaabaea4c80.zip
Add a tor_ftruncate to replace ftruncate.
(Windows doesn't have ftruncate, and some ftruncates do not move the file pointer to the start of the file.)
Diffstat (limited to 'src/common/compat.c')
-rw-r--r--src/common/compat.c17
1 files changed, 17 insertions, 0 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 1760684cf7..dcdf78d49f 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -1004,6 +1004,23 @@ tor_fd_setpos(int fd, off_t pos)
#endif
}
+/** Replacement for ftruncate(fd, 0): move to the front of the file and remove
+ * all the rest of the file. Return -1 on error, 0 on success. */
+int
+tor_ftruncate(int fd)
+{
+ /* Rumor has it that some versions of ftruncate do not move the file pointer.
+ */
+ if (tor_fd_setpos(fd, 0) < 0)
+ return -1;
+
+#ifdef _WIN32
+ return _chsize(fd, 0);
+#else
+ return ftruncate(fd, 0);
+#endif
+}
+
#undef DEBUG_SOCKET_COUNTING
#ifdef DEBUG_SOCKET_COUNTING
/** A bitarray of all fds that should be passed to tor_socket_close(). Only