mirror of
https://gitlab.torproject.org/tpo/core/tor.git
synced 2024-11-27 22:03:31 +01:00
Split read_all and write_all into separate functions
This commit is contained in:
parent
05040a9e84
commit
67135ca8e0
@ -1076,22 +1076,17 @@ format_time_interval(char *out, size_t out_len, long interval)
|
||||
* File helpers
|
||||
* ===== */
|
||||
|
||||
/** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. <b>isSocket</b>
|
||||
* must be 1 if fd was returned by socket() or accept(), and 0 if fd
|
||||
* was returned by open(). Return the number of bytes written, or -1
|
||||
* on error. Only use if fd is a blocking fd. */
|
||||
/** Write <b>count</b> bytes from <b>buf</b> to <b>fd</b>. Return the number
|
||||
* of bytes written, or -1 on error. Only use if fd is a blocking fd. */
|
||||
ssize_t
|
||||
write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
|
||||
write_all_to_fd(int fd, const char *buf, size_t count)
|
||||
{
|
||||
size_t written = 0;
|
||||
ssize_t result;
|
||||
raw_assert(count < SSIZE_MAX);
|
||||
|
||||
while (written != count) {
|
||||
if (isSocket)
|
||||
result = tor_socket_send(fd, buf+written, count-written, 0);
|
||||
else
|
||||
result = write((int)fd, buf+written, count-written);
|
||||
result = write(fd, buf+written, count-written);
|
||||
if (result<0)
|
||||
return -1;
|
||||
written += result;
|
||||
@ -1099,13 +1094,29 @@ write_all(tor_socket_t fd, const char *buf, size_t count, int isSocket)
|
||||
return (ssize_t)count;
|
||||
}
|
||||
|
||||
/** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes
|
||||
* or reach the end of the file. <b>isSocket</b> must be 1 if fd
|
||||
* was returned by socket() or accept(), and 0 if fd was returned by
|
||||
* open(). Return the number of bytes read, or -1 on error. Only use
|
||||
* if fd is a blocking fd. */
|
||||
/** Write <b>count</b> bytes from <b>buf</b> to <b>sock</b>. Return the number
|
||||
* of bytes written, or -1 on error. Only use if fd is a blocking fd. */
|
||||
ssize_t
|
||||
read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
|
||||
write_all_to_socket(tor_socket_t fd, const char *buf, size_t count)
|
||||
{
|
||||
size_t written = 0;
|
||||
ssize_t result;
|
||||
raw_assert(count < SSIZE_MAX);
|
||||
|
||||
while (written != count) {
|
||||
result = tor_socket_send(fd, buf+written, count-written, 0);
|
||||
if (result<0)
|
||||
return -1;
|
||||
written += result;
|
||||
}
|
||||
return (ssize_t)count;
|
||||
}
|
||||
|
||||
/** Read from <b>fd</b> to <b>buf</b>, until we get <b>count</b> bytes or
|
||||
* reach the end of the file. Return the number of bytes read, or -1 on
|
||||
* error. Only use if fd is a blocking fd. */
|
||||
ssize_t
|
||||
read_all_from_fd(int fd, char *buf, size_t count)
|
||||
{
|
||||
size_t numread = 0;
|
||||
ssize_t result;
|
||||
@ -1116,10 +1127,32 @@ read_all(tor_socket_t fd, char *buf, size_t count, int isSocket)
|
||||
}
|
||||
|
||||
while (numread < count) {
|
||||
if (isSocket)
|
||||
result = tor_socket_recv(fd, buf+numread, count-numread, 0);
|
||||
else
|
||||
result = read((int)fd, buf+numread, count-numread);
|
||||
result = read(fd, buf+numread, count-numread);
|
||||
if (result<0)
|
||||
return -1;
|
||||
else if (result == 0)
|
||||
break;
|
||||
numread += result;
|
||||
}
|
||||
return (ssize_t)numread;
|
||||
}
|
||||
|
||||
/** Read from <b>sock</b> to <b>buf</b>, until we get <b>count</b> bytes or
|
||||
* reach the end of the file. Return the number of bytes read, or -1 on
|
||||
* error. Only use if fd is a blocking fd. */
|
||||
ssize_t
|
||||
read_all_from_socket(tor_socket_t sock, char *buf, size_t count)
|
||||
{
|
||||
size_t numread = 0;
|
||||
ssize_t result;
|
||||
|
||||
if (count > SIZE_T_CEILING || count > SSIZE_MAX) {
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
|
||||
while (numread < count) {
|
||||
result = tor_socket_recv(sock, buf+numread, count-numread, 0);
|
||||
if (result<0)
|
||||
return -1;
|
||||
else if (result == 0)
|
||||
|
@ -117,8 +117,17 @@ int parse_http_time(const char *buf, struct tm *tm);
|
||||
int format_time_interval(char *out, size_t out_len, long interval);
|
||||
|
||||
/* File helpers */
|
||||
ssize_t write_all(tor_socket_t fd, const char *buf, size_t count,int isSocket);
|
||||
ssize_t read_all(tor_socket_t fd, char *buf, size_t count, int isSocket);
|
||||
ssize_t write_all_to_fd(int fd, const char *buf, size_t count);
|
||||
ssize_t write_all_to_socket(tor_socket_t fd, const char *buf, size_t count);
|
||||
ssize_t read_all_from_fd(int fd, char *buf, size_t count);
|
||||
ssize_t read_all_from_socket(tor_socket_t fd, char *buf, size_t count);
|
||||
|
||||
#define write_all(fd, buf, count, isSock) \
|
||||
((isSock) ? write_all_to_socket((fd), (buf), (count)) \
|
||||
: write_all_to_fd((int)(fd), (buf), (count)))
|
||||
#define read_all(fd, buf, count, isSock) \
|
||||
((isSock) ? read_all_from_socket((fd), (buf), (count)) \
|
||||
: read_all_from_fd((int)(fd), (buf), (count)))
|
||||
|
||||
/** Status of an I/O stream. */
|
||||
enum stream_status {
|
||||
|
@ -94,7 +94,7 @@ tor_ftruncate(int fd)
|
||||
|
||||
/** Minimal version of write_all, for use by logging. */
|
||||
int
|
||||
write_all_to_fd(int fd, const char *buf, size_t count)
|
||||
write_all_to_fd_minimal(int fd, const char *buf, size_t count)
|
||||
{
|
||||
size_t written = 0;
|
||||
raw_assert(count < SSIZE_MAX);
|
||||
|
@ -12,6 +12,6 @@ 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);
|
||||
int write_all_to_fd(int fd, const char *buf, size_t count);
|
||||
int write_all_to_fd_minimal(int fd, const char *buf, size_t count);
|
||||
|
||||
#endif /* !defined(TOR_FDIO_H) */
|
||||
|
@ -346,7 +346,7 @@ log_tor_version(logfile_t *lf, int reset)
|
||||
tor_snprintf(buf+n, sizeof(buf)-n,
|
||||
"Tor %s opening %slog file.\n", VERSION, is_new?"new ":"");
|
||||
}
|
||||
if (write_all_to_fd(lf->fd, buf, strlen(buf)) < 0) /* error */
|
||||
if (write_all_to_fd_minimal(lf->fd, buf, strlen(buf)) < 0) /* error */
|
||||
return -1; /* failed */
|
||||
return 0;
|
||||
}
|
||||
@ -560,7 +560,7 @@ logfile_deliver(logfile_t *lf, const char *buf, size_t msg_len,
|
||||
lf->callback(severity, domain, msg_after_prefix);
|
||||
}
|
||||
} else {
|
||||
if (write_all_to_fd(lf->fd, buf, msg_len) < 0) { /* error */
|
||||
if (write_all_to_fd_minimal(lf->fd, buf, msg_len) < 0) { /* error */
|
||||
/* don't log the error! mark this log entry to be blown away, and
|
||||
* continue. */
|
||||
lf->seems_dead = 1;
|
||||
|
Loading…
Reference in New Issue
Block a user