mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-12-06 19:41:15 +01:00
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.)
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -408,6 +408,7 @@ void tor_lockfile_unlock(tor_lockfile_t *lockfile);
|
||||
off_t tor_fd_getpos(int fd);
|
||||
int tor_fd_setpos(int fd, off_t pos);
|
||||
int tor_fd_seekend(int fd);
|
||||
int tor_ftruncate(int fd);
|
||||
|
||||
#ifdef _WIN32
|
||||
#define PATH_SEPARATOR "\\"
|
||||
|
||||
+6
-2
@@ -1305,6 +1305,10 @@ switch_logs_debug(void)
|
||||
void
|
||||
truncate_logs(void)
|
||||
{
|
||||
for (logfile_t *lf = logfiles; lf; lf = lf->next)
|
||||
ftruncate(lf->fd, 0);
|
||||
for (logfile_t *lf = logfiles; lf; lf = lf->next) {
|
||||
if (lf->fd >= 0) {
|
||||
tor_ftruncate(lf->fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user