Changeset 6afc9d7 in mainline for uspace/lib/c/generic/io/io.c


Ignore:
Timestamp:
2015-10-06T19:01:36Z (9 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0328987
Parents:
f1f7584
Message:

UNIX-like I/O functions should use errno to return error code for many reasons.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/generic/io/io.c

    rf1f7584 r6afc9d7  
    231231        if (stream->buf == NULL) {
    232232                errno = ENOMEM;
    233                 return -1;
     233                return EOF;
    234234        }
    235235       
     
    365365 * @param nmemb  Number of records to read.
    366366 * @param stream Pointer to the stream.
     367 *
     368 * @return Number of elements successfully read. On error this is less than
     369 *         nmemb, stream error indicator is set and errno is set.
    367370 */
    368371static size_t _fread(void *buf, size_t size, size_t nmemb, FILE *stream)
     
    379382                ssize_t rd = read(stream->fd, buf + done, left);
    380383               
    381                 if (rd < 0)
     384                if (rd < 0) {
     385                        /* errno was set by read() */
    382386                        stream->error = true;
    383                 else if (rd == 0)
     387                } else if (rd == 0) {
    384388                        stream->eof = true;
    385                 else {
     389                } else {
    386390                        left -= rd;
    387391                        done += rd;
     
    398402 * @param nmemb  Number of records to write.
    399403 * @param stream Pointer to the stream.
     404 *
     405 * @return Number of elements successfully written. On error this is less than
     406 *         nmemb, stream error indicator is set and errno is set.
    400407 */
    401408static size_t _fwrite(const void *buf, size_t size, size_t nmemb, FILE *stream)
     
    403410        size_t left;
    404411        size_t done;
     412        int rc;
    405413
    406414        if (size == 0 || nmemb == 0)
     
    412420        while ((left > 0) && (!stream->error)) {
    413421                ssize_t wr;
     422                size_t uwr;
    414423               
    415                 if (stream->kio)
    416                         wr = kio_write(buf + done, left);
    417                 else
     424                if (stream->kio) {
     425                        uwr = 0;
     426                        rc = kio_write(buf + done, left, &uwr);
     427                        if (rc != EOK)
     428                                errno = rc;
     429                } else {
    418430                        wr = write(stream->fd, buf + done, left);
     431                        if (wr >= 0) {
     432                                uwr = (size_t)wr;
     433                                rc = EOK;
     434                        } else {
     435                                /* errno was set by write */
     436                                uwr = 0;
     437                                rc = errno;
     438                        }
     439                }
    419440               
    420                 if (wr <= 0)
     441                if (rc != EOK) {
     442                        /* errno was set above */
    421443                        stream->error = true;
    422                 else {
    423                         left -= wr;
    424                         done += wr;
     444                } else {
     445                        left -= uwr;
     446                        done += uwr;
    425447                }
    426448        }
     
    432454}
    433455
    434 /** Read some data in stream buffer. */
     456/** Read some data in stream buffer.
     457 *
     458 * On error, stream error indicator is set and errno is set.
     459 */
    435460static void _ffillbuf(FILE *stream)
    436461{
     
    441466        rc = read(stream->fd, stream->buf, stream->buf_size);
    442467        if (rc < 0) {
     468                /* errno was set by read() */
    443469                stream->error = true;
    444470                return;
     
    465491
    466492        /* If buffer has prefetched read data, we need to seek back. */
    467         if (bytes_used > 0 && stream->buf_state == _bs_read)
    468                 lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR);
     493        if (bytes_used > 0 && stream->buf_state == _bs_read) {
     494                off64_t rc;
     495                rc = lseek(stream->fd, - (ssize_t) bytes_used, SEEK_CUR);
     496                if (rc == (off64_t)-1) {
     497                        /* errno was set by lseek */
     498                        stream->error = 1;
     499                        return;
     500                }
     501        }
    469502
    470503        /* If buffer has unwritten data, we need to write them out. */
    471         if (bytes_used > 0 && stream->buf_state == _bs_write)
     504        if (bytes_used > 0 && stream->buf_state == _bs_write) {
    472505                (void) _fwrite(stream->buf_tail, 1, bytes_used, stream);
     506                /* On error stream error indicator and errno are set by _fwrite */
     507                if (stream->error)
     508                        return;
     509        }
    473510
    474511        stream->buf_head = stream->buf;
     
    528565                        _ffillbuf(stream);
    529566
    530                 if (stream->error || stream->eof)
     567                if (stream->error || stream->eof) {
     568                        /* On error errno was set by _ffillbuf() */
    531569                        break;
     570                }
    532571
    533572                data_avail = stream->buf_head - stream->buf_tail;
     
    584623        if (stream->buf_state == _bs_read)
    585624                _fflushbuf(stream);
    586 
    587625
    588626        /* Perform lazy allocation of stream buffer. */
     
    622660                        /* Only need to drain buffer. */
    623661                        _fflushbuf(stream);
    624                         need_flush = false;
     662                        if (!stream->error)
     663                                need_flush = false;
    625664                }
    626665        }
     
    734773        off64_t rc;
    735774
     775        if (stream->error)
     776                return EOF;
     777
    736778        _fflushbuf(stream);
     779        if (stream->error) {
     780                /* errno was set by _fflushbuf() */
     781                return EOF;
     782        }
     783
    737784        stream->ungetc_chars = 0;
    738785
    739786        rc = lseek(stream->fd, offset, whence);
    740787        if (rc == (off64_t) (-1)) {
    741                 /* errno has been set by lseek64. */
    742                 return -1;
     788                /* errno has been set by lseek() */
     789                return EOF;
    743790        }
    744791
     
    749796off64_t ftell(FILE *stream)
    750797{
     798        off64_t pos;
     799       
     800        if (stream->error)
     801                return EOF;
     802       
    751803        _fflushbuf(stream);
    752         return lseek(stream->fd, 0, SEEK_CUR) - stream->ungetc_chars;
     804        if (stream->error) {
     805                /* errno was set by _fflushbuf() */
     806                return EOF;
     807        }
     808
     809        pos = lseek(stream->fd, 0, SEEK_CUR);
     810        if (pos == (off64_t) -1) {
     811                /* errno was set by lseek */
     812                return (off64_t) -1;
     813        }
     814       
     815        return pos - stream->ungetc_chars;
    753816}
    754817
     
    760823int fflush(FILE *stream)
    761824{
     825        if (stream->error)
     826                return EOF;
     827       
    762828        _fflushbuf(stream);
     829        if (stream->error) {
     830                /* errno was set by _fflushbuf() */
     831                return EOF;
     832        }
    763833       
    764834        if (stream->kio) {
    765835                kio_update();
    766                 return EOK;
     836                return 0;
    767837        }
    768838       
     
    773843                 */
    774844                stream->need_sync = false;
    775                 return fsync(stream->fd);
    776         }
    777        
    778         return ENOENT;
     845                if (fsync(stream->fd) != 0) {
     846                        /* errno was set by fsync() */
     847                        return EOF;
     848                }
     849
     850                return 0;
     851        }
     852       
     853        return 0;
    779854}
    780855
     
    799874        if (stream->kio) {
    800875                errno = EBADF;
    801                 return -1;
     876                return EOF;
    802877        }
    803878       
     
    805880}
    806881
    807 async_sess_t *fsession(FILE *stream, iface_t iface)
     882async_sess_t *vfs_fsession(FILE *stream, iface_t iface)
    808883{
    809884        if (stream->fd >= 0) {
    810885                if (stream->sess == NULL)
    811                         stream->sess = fd_session(stream->fd, iface);
     886                        stream->sess = vfs_fd_session(stream->fd, iface);
    812887               
    813888                return stream->sess;
     
    817892}
    818893
    819 int fhandle(FILE *stream, int *handle)
     894int vfs_fhandle(FILE *stream, int *handle)
    820895{
    821896        if (stream->fd >= 0) {
Note: See TracChangeset for help on using the changeset viewer.