Fork us on GitHub Follow us on Facebook Follow us on Twitter

Changeset 8fd04ba in mainline


Ignore:
Timestamp:
2011-07-24T12:48:28Z (10 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master
Children:
4022513
Parents:
e16e2ba4
Message:

read_all() and write_all().

Location:
uspace
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/taskdump/elf_core.c

    re16e2ba4 r8fd04ba  
    6767
    6868static off64_t align_foff_up(off64_t, uintptr_t, size_t);
    69 static int write_all(int, const void *, size_t);
    7069static int align_pos(int, size_t);
    7170static int write_mem_area(int, as_area_info_t *, async_sess_t *);
     
    10099
    101100        int fd;
    102         int rc;
     101        ssize_t rc;
    103102        unsigned int i;
    104103
     
    204203
    205204        rc = write_all(fd, &elf_hdr, sizeof(elf_hdr));
    206         if (rc != EOK) {
     205        if (rc != sizeof(elf_hdr)) {
    207206                printf("Failed writing ELF header.\n");
    208207                free(p_hdr);
     
    212211        for (i = 0; i < n_ph; ++i) {
    213212                rc = write_all(fd, &p_hdr[i], sizeof(p_hdr[i]));
    214                 if (rc != EOK) {
     213                if (rc != sizeof(p_hdr[i])) {
    215214                        printf("Failed writing program header.\n");
    216215                        free(p_hdr);
     
    233232
    234233        rc = write_all(fd, &note, sizeof(elf_note_t));
    235         if (rc != EOK) {
     234        if (rc != sizeof(elf_note_t)) {
    236235                printf("Failed writing note header.\n");
    237236                free(p_hdr);
     
    240239
    241240        rc = write_all(fd, "CORE", note.namesz);
    242         if (rc != EOK) {
     241        if (rc != (ssize_t) note.namesz) {
    243242                printf("Failed writing note header.\n");
    244243                free(p_hdr);
     
    254253
    255254        rc = write_all(fd, &pr_status, sizeof(elf_prstatus_t));
    256         if (rc != EOK) {
     255        if (rc != sizeof(elf_prstatus_t)) {
    257256                printf("Failed writing register data.\n");
    258257                free(p_hdr);
     
    304303        size_t total;
    305304        uintptr_t addr;
    306         int rc;
     305        ssize_t rc;
    307306
    308307        addr = area->start_addr;
     
    318317
    319318                rc = write_all(fd, buffer, to_copy);
    320                 if (rc != EOK) {
     319                if (rc != (ssize_t) to_copy) {
    321320                        printf("Failed writing memory contents.\n");
    322321                        return EIO;
     
    326325                total += to_copy;
    327326        }
    328 
    329         return EOK;
    330 }
    331 
    332 /** Write until the buffer is written in its entirety.
    333  *
    334  * This function fails if it cannot write exactly @a len bytes to the file.
    335  *
    336  * @param fd            The file to write to.
    337  * @param buf           Data, @a len bytes long.
    338  * @param len           Number of bytes to write.
    339  *
    340  * @return              EOK on error, return value from write() if writing
    341  *                      failed.
    342  */
    343 static int write_all(int fd, const void *data, size_t len)
    344 {
    345         int cnt = 0;
    346 
    347         do {
    348                 data += cnt;
    349                 len -= cnt;
    350                 cnt = write(fd, data, len);
    351         } while (cnt > 0 && (len - cnt) > 0);
    352 
    353         if (cnt < 0)
    354                 return cnt;
    355 
    356         if (len - cnt > 0)
    357                 return EIO;
    358327
    359328        return EOK;
  • uspace/app/taskdump/symtab.c

    re16e2ba4 r8fd04ba  
    5050    elf_section_header_t *shdr);
    5151static int chunk_load(int fd, off64_t start, size_t size, void **ptr);
    52 static int read_all(int fd, void *buf, size_t len);
    5352
    5453/** Load symbol table from an ELF file.
     
    9089
    9190        rc = read_all(fd, &elf_hdr, sizeof(elf_header_t));
    92         if (rc != EOK) {
     91        if (rc != sizeof(elf_header_t)) {
    9392                printf("failed reading elf header\n");
    9493                free(stab);
     
    312311
    313312        rc = read_all(fd, sec_hdr, sizeof(elf_section_header_t));
    314         if (rc != EOK)
     313        if (rc != sizeof(elf_section_header_t))
    315314                return EIO;
    316315
     
    331330static int chunk_load(int fd, off64_t start, size_t size, void **ptr)
    332331{
    333         int rc;
    334 
    335         rc = lseek(fd, start, SEEK_SET);
    336         if (rc == (off64_t) -1) {
     332        ssize_t rc;
     333        off64_t offs;
     334
     335        offs = lseek(fd, start, SEEK_SET);
     336        if (offs == (off64_t) -1) {
    337337                printf("failed seeking chunk\n");
    338338                *ptr = NULL;
     
    347347
    348348        rc = read_all(fd, *ptr, size);
    349         if (rc != EOK) {
     349        if (rc != (ssize_t) size) {
    350350                printf("failed reading chunk\n");
    351351                free(*ptr);
     
    357357}
    358358
    359 /** Read until the buffer is read in its entirety.
    360  *
    361  * This function fails if it cannot read exactly @a len bytes from the file.
    362  *
    363  * @param fd            The file to read from.
    364  * @param buf           Buffer for storing data, @a len bytes long.
    365  * @param len           Number of bytes to read.
    366  *
    367  * @return              EOK on error, EIO if file is short or return value
    368  *                      from read() if reading failed.
    369  */
    370 static int read_all(int fd, void *buf, size_t len)
    371 {
    372         int cnt = 0;
    373 
    374         do {
    375                 buf += cnt;
    376                 len -= cnt;
    377                 cnt = read(fd, buf, len);
    378         } while (cnt > 0 && (len - cnt) > 0);
    379 
    380         if (cnt < 0)
    381                 return cnt;
    382 
    383         if (len - cnt > 0)
    384                 return EIO;
    385 
    386         return EOK;
    387 }
    388 
    389359/** @}
    390360 */
  • uspace/lib/c/generic/vfs/vfs.c

    re16e2ba4 r8fd04ba  
    417417}
    418418
     419/** Read entire buffer.
     420 *
     421 * In face of short reads this function continues reading until either
     422 * the entire buffer is read or no more data is available (at end of file).
     423 *
     424 * @param fildes        File descriptor
     425 * @param buf           Buffer, @a nbytes bytes long
     426 * @param nbytes        Number of bytes to read
     427 *
     428 * @return              On success, positive number of bytes read.
     429 *                      On failure, negative error code from read().
     430 */
     431ssize_t read_all(int fildes, void *buf, size_t nbyte)
     432{
     433        ssize_t cnt = 0;
     434        size_t nread = 0;
     435        uint8_t *bp = (uint8_t *) buf;
     436
     437        do {
     438                bp += cnt;
     439                nread += cnt;
     440                cnt = read(fildes, bp, nbyte - nread);
     441        } while (cnt > 0 && (nbyte - nread - cnt) > 0);
     442
     443        if (cnt < 0)
     444                return cnt;
     445
     446        return nread + cnt;
     447}
     448
     449/** Write entire buffer.
     450 *
     451 * This function fails if it cannot write exactly @a len bytes to the file.
     452 *
     453 * @param fildes        File descriptor
     454 * @param buf           Data, @a nbytes bytes long
     455 * @param nbytes        Number of bytes to write
     456 *
     457 * @return              EOK on error, return value from write() if writing
     458 *                      failed.
     459 */
     460ssize_t write_all(int fildes, const void *buf, size_t nbyte)
     461{
     462        ssize_t cnt = 0;
     463        ssize_t nwritten = 0;
     464        const uint8_t *bp = (uint8_t *) buf;
     465
     466        do {
     467                bp += cnt;
     468                nwritten += cnt;
     469                cnt = write(fildes, bp, nbyte - nwritten);
     470        } while (cnt > 0 && ((ssize_t )nbyte - nwritten - cnt) > 0);
     471
     472        if (cnt < 0)
     473                return cnt;
     474
     475        if ((ssize_t)nbyte - nwritten - cnt > 0)
     476                return EIO;
     477
     478        return nbyte;
     479}
     480
    419481int fsync(int fildes)
    420482{
  • uspace/lib/c/include/unistd.h

    re16e2ba4 r8fd04ba  
    6363extern ssize_t read(int, void *, size_t);
    6464
     65extern ssize_t read_all(int, void *, size_t);
     66extern ssize_t write_all(int, const void *, size_t);
     67
    6568extern off64_t lseek(int, off64_t, int);
    6669extern int ftruncate(int, aoff64_t);
  • uspace/srv/devman/devman.c

    re16e2ba4 r8fd04ba  
    270270        }
    271271       
    272         ssize_t read_bytes = safe_read(fd, buf, len);
     272        ssize_t read_bytes = read_all(fd, buf, len);
    273273        if (read_bytes <= 0) {
    274                 log_msg(LVL_ERROR, "Unable to read file '%s'.", conf_path);
     274                log_msg(LVL_ERROR, "Unable to read file '%s' (%zd).", conf_path,
     275                    read_bytes);
    275276                goto cleanup;
    276277        }
  • uspace/srv/devman/util.c

    re16e2ba4 r8fd04ba  
    111111}
    112112
    113 ssize_t safe_read(int fd, void *buffer, size_t size)
    114 {
    115         if (size == 0) {
    116                 return 0;
    117         }
    118 
    119         uint8_t *buf_ptr = (uint8_t *) buffer;
    120 
    121         size_t total_read = 0;
    122         while (total_read < size) {
    123                 ssize_t bytes_read = read(fd, buf_ptr, size - total_read);
    124                 if (bytes_read < 0) {
    125                         /* Error. */
    126                         return bytes_read;
    127                 } else if (bytes_read == 0) {
    128                         /* Possibly end of file. */
    129                         break;
    130                 } else {
    131                         /* Read at least something. */
    132                         buf_ptr += bytes_read;
    133                         total_read += bytes_read;
    134                 }
    135         }
    136 
    137         return (ssize_t) total_read;
    138 }
    139 
    140113/** @}
    141114 */
  • uspace/srv/devman/util.h

    re16e2ba4 r8fd04ba  
    4747extern void replace_char(char *, char, char);
    4848
    49 extern ssize_t safe_read(int, void *, size_t);
    50 
    5149#endif
    5250
Note: See TracChangeset for help on using the changeset viewer.