diff --git a/src/os/unix/ngx_files.c b/src/os/unix/ngx_files.c index bb30c1e16..d4bf0436f 100644 --- a/src/os/unix/ngx_files.c +++ b/src/os/unix/ngx_files.c @@ -59,26 +59,33 @@ ngx_read_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset) ssize_t ngx_write_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset) { - ssize_t n; + ssize_t n, written; ngx_log_debug4(NGX_LOG_DEBUG_CORE, file->log, 0, "write: %d, %p, %uz, %O", file->fd, buf, size, offset); + written = 0; + #if (NGX_HAVE_PWRITE) - n = pwrite(file->fd, buf, size, offset); + for ( ;; ) { + n = pwrite(file->fd, buf, size, offset); - if (n == -1) { - ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno, - "pwrite() \"%s\" failed", file->name.data); - return NGX_ERROR; - } + if (n == -1) { + ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno, + "pwrite() \"%s\" failed", file->name.data); + return NGX_ERROR; + } - if ((size_t) n != size) { - ngx_log_error(NGX_LOG_CRIT, file->log, 0, - "pwrite() \"%s\" has written only %z of %uz", - file->name.data, n, size); - return NGX_ERROR; + file->offset += n; + written += n; + + if ((size_t) n == size) { + return written; + } + + offset += n; + size -= n; } #else @@ -93,28 +100,25 @@ ngx_write_file(ngx_file_t *file, u_char *buf, size_t size, off_t offset) file->sys_offset = offset; } - n = write(file->fd, buf, size); + for ( ;; ) { + n = write(file->fd, buf, size); - if (n == -1) { - ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno, - "write() \"%s\" failed", file->name.data); - return NGX_ERROR; + if (n == -1) { + ngx_log_error(NGX_LOG_CRIT, file->log, ngx_errno, + "write() \"%s\" failed", file->name.data); + return NGX_ERROR; + } + + file->offset += n; + written += n; + + if ((size_t) n == size) { + return written; + } + + size -= n; } - - if ((size_t) n != size) { - ngx_log_error(NGX_LOG_CRIT, file->log, 0, - "write() \"%s\" has written only %z of %uz", - file->name.data, n, size); - return NGX_ERROR; - } - - file->sys_offset += n; - #endif - - file->offset += n; - - return n; }