bug in function ngx_write_file?

Igor Sysoev igor at sysoev.ru
Thu Jul 1 10:00:00 MSD 2010


On Wed, Jun 30, 2010 at 11:26:46AM +0800, lu wenbin wrote:

> Hi
>         I read the code in src/os/ngx_files.c, function ngx_write_file, but
> get a little confused
>        * 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;
>         }
> 
>         file->offset += n;
>         written += n;
> 
>         if ((size_t) n == size) {
>             return written;
>         }
> 
>         offset += n;
>         size -= n;
>       }
>   *   the code seems want to write all content in buf to file(file->fd), but
> I think *n = pwrite(file->fd, buf, size, offset) *should be changed to* **n
> = pwrite(file->fd, buf+written, size, offset);
>      man pwrite:
>      **pwrite() writes up to count bytes from the buffer starting  at  buf
> to the  file  descriptor  fd  at  offset  offset.   The file offset is not
> changed.**

Yes, you are right, thank you. This bug was not catched since
partial write to a regular file is very seldom issue and currently
ngx_write_file() is used only to write a tiny PID file.


-- 
Igor Sysoev
http://sysoev.ru/en/



More information about the nginx-devel mailing list