Changes in uspace/srv/bd/file_bd/file_bd.c [ed903174:0da4e41] in mainline
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/bd/file_bd/file_bd.c
red903174 r0da4e41 45 45 #include <async.h> 46 46 #include <as.h> 47 #include <fibril_sync h.h>47 #include <fibril_sync.h> 48 48 #include <devmap.h> 49 49 #include <sys/types.h> 50 #include <sys/typefmt.h>51 50 #include <errno.h> 52 51 #include <bool.h> … … 57 56 58 57 static const size_t block_size = 512; 59 static aoff64_t num_blocks;60 58 static FILE *img; 61 59 … … 101 99 { 102 100 int rc; 103 long img_size;104 101 105 102 rc = devmap_driver_register(NAME, file_bd_connection); … … 112 109 if (img == NULL) 113 110 return EINVAL; 114 115 if (fseek(img, 0, SEEK_END) != 0) {116 fclose(img);117 return EIO;118 }119 120 img_size = ftell(img);121 if (img_size < 0) {122 fclose(img);123 return EIO;124 }125 126 num_blocks = img_size / block_size;127 111 128 112 fibril_mutex_initialize(&dev_lock); … … 190 174 ipc_answer_1(callid, EOK, block_size); 191 175 continue; 192 case BD_GET_NUM_BLOCKS:193 ipc_answer_2(callid, EOK, LOWER32(num_blocks),194 UPPER32(num_blocks));195 continue;196 176 default: 197 177 retval = EINVAL; … … 206 186 { 207 187 size_t n_rd; 208 int rc;209 210 /* Check whether access is within device address bounds. */211 if (ba + cnt > num_blocks) {212 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "213 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,214 num_blocks - 1);215 return ELIMIT;216 }217 188 218 189 fibril_mutex_lock(&dev_lock); 219 190 220 clearerr(img); 221 rc = fseek(img, ba * block_size, SEEK_SET); 222 if (rc < 0) { 223 fibril_mutex_unlock(&dev_lock); 224 return EIO; 225 } 226 191 fseek(img, ba * block_size, SEEK_SET); 227 192 n_rd = fread(buf, block_size, cnt, img); 228 193 … … 244 209 { 245 210 size_t n_wr; 246 int rc;247 248 /* Check whether access is within device address bounds. */249 if (ba + cnt > num_blocks) {250 printf(NAME ": Accessed blocks %" PRIuOFF64 "-%" PRIuOFF64 ", while "251 "max block number is %" PRIuOFF64 ".\n", ba, ba + cnt - 1,252 num_blocks - 1);253 return ELIMIT;254 }255 211 256 212 fibril_mutex_lock(&dev_lock); 257 213 258 clearerr(img); 259 rc = fseek(img, ba * block_size, SEEK_SET); 260 if (rc < 0) { 261 fibril_mutex_unlock(&dev_lock); 262 return EIO; 263 } 264 265 n_wr = fwrite(buf, block_size, cnt, img); 214 fseek(img, ba * block_size, SEEK_SET); 215 n_wr = fread(buf, block_size, cnt, img); 266 216 267 217 if (ferror(img) || n_wr < cnt) { … … 270 220 } 271 221 272 if (fflush(img) != 0) {273 fibril_mutex_unlock(&dev_lock);274 return EIO;275 }276 277 222 fibril_mutex_unlock(&dev_lock); 278 223
Note:
See TracChangeset
for help on using the changeset viewer.