Changeset 3b3e776 in mainline for uspace/srv/bd/file_bd/file_bd.c


Ignore:
Timestamp:
2010-02-05T10:57:50Z (14 years ago)
Author:
Lenka Trochtova <trochtova.lenka@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
0358da0
Parents:
3f085132 (diff), b4cbef1 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

merged with head

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/srv/bd/file_bd/file_bd.c

    r3f085132 r3b3e776  
    4545#include <async.h>
    4646#include <as.h>
    47 #include <fibril_sync.h>
     47#include <fibril_synch.h>
    4848#include <devmap.h>
    4949#include <sys/types.h>
     
    5656
    5757static const size_t block_size = 512;
     58static bn_t num_blocks;
    5859static FILE *img;
    5960
     
    99100{
    100101        int rc;
     102        long img_size;
    101103
    102104        rc = devmap_driver_register(NAME, file_bd_connection);
     
    109111        if (img == NULL)
    110112                return EINVAL;
     113
     114        if (fseek(img, 0, SEEK_END) != 0) {
     115                fclose(img);
     116                return EIO;
     117        }
     118
     119        img_size = ftell(img);
     120        if (img_size < 0) {
     121                fclose(img);
     122                return EIO;
     123        }
     124
     125        num_blocks = img_size / block_size;
    111126
    112127        fibril_mutex_initialize(&dev_lock);
     
    174189                        ipc_answer_1(callid, EOK, block_size);
    175190                        continue;
     191                case BD_GET_NUM_BLOCKS:
     192                        ipc_answer_2(callid, EOK, LOWER32(num_blocks),
     193                            UPPER32(num_blocks));
     194                        continue;
    176195                default:
    177196                        retval = EINVAL;
     
    186205{
    187206        size_t n_rd;
     207        int rc;
    188208
    189209        fibril_mutex_lock(&dev_lock);
    190210
    191         fseek(img, ba * block_size, SEEK_SET);
     211        clearerr(img);
     212        rc = fseek(img, ba * block_size, SEEK_SET);
     213        if (rc < 0) {
     214                fibril_mutex_unlock(&dev_lock);
     215                return EIO;
     216        }
     217
    192218        n_rd = fread(buf, block_size, cnt, img);
    193219
     
    209235{
    210236        size_t n_wr;
     237        int rc;
    211238
    212239        fibril_mutex_lock(&dev_lock);
    213240
    214         fseek(img, ba * block_size, SEEK_SET);
    215         n_wr = fread(buf, block_size, cnt, img);
     241        clearerr(img);
     242        rc = fseek(img, ba * block_size, SEEK_SET);
     243        if (rc < 0) {
     244                fibril_mutex_unlock(&dev_lock);
     245                return EIO;
     246        }
     247
     248        n_wr = fwrite(buf, block_size, cnt, img);
    216249
    217250        if (ferror(img) || n_wr < cnt) {
    218251                fibril_mutex_unlock(&dev_lock);
    219252                return EIO;     /* Write error */
     253        }
     254
     255        if (fflush(img) != 0) {
     256                fibril_mutex_unlock(&dev_lock);
     257                return EIO;
    220258        }
    221259
Note: See TracChangeset for help on using the changeset viewer.