summaryrefslogtreecommitdiff
path: root/src/common/util.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2007-02-01 18:09:27 +0000
committerNick Mathewson <nickm@torproject.org>2007-02-01 18:09:27 +0000
commit5cb99857bc0633757f60c4b964359c5a03121b28 (patch)
tree707a2ad60ab69f54eb9e4716d0fa9df50c77686f /src/common/util.c
parent8a9a55251ffe14389a96e99bdcc7af59b0f1c8ba (diff)
downloadtor-5cb99857bc0633757f60c4b964359c5a03121b28.tar.gz
tor-5cb99857bc0633757f60c4b964359c5a03121b28.zip
r11620@catbus: nickm | 2007-02-01 13:06:27 -0500
Call stat() slightly less often; use fstat() when possible. svn:r9472
Diffstat (limited to 'src/common/util.c')
-rw-r--r--src/common/util.c46
1 files changed, 24 insertions, 22 deletions
diff --git a/src/common/util.c b/src/common/util.c
index f249b4551c..d7027b848c 100644
--- a/src/common/util.c
+++ b/src/common/util.c
@@ -1375,30 +1375,27 @@ append_bytes_to_file(const char *fname, const char *str, size_t len,
* be truncated.
*/
char *
-read_file_to_str(const char *filename, int bin, size_t *size_out)
+read_file_to_str(const char *filename, int bin, struct stat *stat_out)
{
int fd; /* router file */
struct stat statbuf;
- char *string, *f;
+ char *string;
int r;
tor_assert(filename);
- f = tor_strdup(filename);
- clean_name_for_stat(f);
- r = stat(f, &statbuf);
- tor_free(f);
- if (r < 0) {
- log_info(LD_FS,"Could not stat \"%s\".",filename);
- return NULL;
- }
-
fd = open(filename,O_RDONLY|(bin?O_BINARY:O_TEXT),0);
if (fd<0) {
log_warn(LD_FS,"Could not open \"%s\".",filename);
return NULL;
}
+ if (fstat(fd, &statbuf)<0) {
+ close(fd);
+ log_info(LD_FS,"Could not fstat \"%s\".",filename);
+ return NULL;
+ }
+
if ((uint64_t)(statbuf.st_size)+1 > SIZE_T_MAX)
return NULL;
@@ -1414,26 +1411,31 @@ read_file_to_str(const char *filename, int bin, size_t *size_out)
}
string[r] = '\0'; /* NUL-terminate the result. */
- if (bin && r != statbuf.st_size) {
- /* If we're in binary mode, then we'd better have an exact match for
- * size. Otherwise, win32 encoding may throw us off, and that's okay. */
- log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
- r, (long)statbuf.st_size,filename);
- tor_free(string);
- close(fd);
- return NULL;
- }
#ifdef MS_WINDOWS
if (!bin && strchr(string, '\r')) {
log_debug(LD_FS, "We didn't convert CRLF to LF as well as we hoped "
"when reading %s. Coping.",
filename);
tor_strstrip(string, "\r");
+ r = strlen(string);
}
+ if (!bin) {
+ statbuf.st_size = (size_t) r;
+ } else
#endif
+ if (r != statbuf.st_size) {
+ /* Unless we're using text mode on win32, we'd better have an exact
+ * match for size. */
+ log_warn(LD_FS,"Could read only %d of %ld bytes of file \"%s\".",
+ r, (long)statbuf.st_size,filename);
+ tor_free(string);
+ close(fd);
+ return NULL;
+ }
close(fd);
- if (size_out)
- *size_out = (size_t) r;
+ if (stat_out) {
+ memcpy(stat_out, &statbuf, sizeof(struct stat));
+ }
return string;
}