summaryrefslogtreecommitdiff
path: root/firmware/common
diff options
context:
space:
mode:
authorBjörn Stenberg <bjorn@haxx.se>2002-11-11 16:13:45 +0000
committerBjörn Stenberg <bjorn@haxx.se>2002-11-11 16:13:45 +0000
commit94fb95f00b3812cc5f439dbd51dcbb4f98a15269 (patch)
treeddf524f6f5354a0987026733f41fa1c01e7c4f8e /firmware/common
parent4059ea61d443b25cd18b6f194f0e5969463c460e (diff)
downloadrockbox-94fb95f00b3812cc5f439dbd51dcbb4f98a15269.tar.gz
rockbox-94fb95f00b3812cc5f439dbd51dcbb4f98a15269.zip
Cosmetic: Replaced dozens of openfiles[fd] with 'file' pointers.
git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2831 a1c6a512-1295-4272-9138-f99709370657
Diffstat (limited to 'firmware/common')
-rw-r--r--firmware/common/file.c178
1 files changed, 94 insertions, 84 deletions
diff --git a/firmware/common/file.c b/firmware/common/file.c
index 6d60595ab1..177635d0be 100644
--- a/firmware/common/file.c
+++ b/firmware/common/file.c
@@ -63,6 +63,7 @@ int open(const char* pathname, int flags)
63 struct dirent* entry; 63 struct dirent* entry;
64 int fd; 64 int fd;
65 char* name; 65 char* name;
66 struct filedesc* file = NULL;
66 67
67 LDEBUGF("open(\"%s\",%d)\n",pathname,flags); 68 LDEBUGF("open(\"%s\",%d)\n",pathname,flags);
68 69
@@ -83,15 +84,17 @@ int open(const char* pathname, int flags)
83 errno = EMFILE; 84 errno = EMFILE;
84 return -2; 85 return -2;
85 } 86 }
86 memset(&openfiles[fd], 0, sizeof (struct filedesc)); 87
88 file = &openfiles[fd];
89 memset(file, 0, sizeof(struct filedesc));
87 90
88 if (flags & (O_RDWR | O_WRONLY)) { 91 if (flags & (O_RDWR | O_WRONLY)) {
89 openfiles[fd].write = true; 92 file->write = true;
90 93
91 if (flags & O_TRUNC) 94 if (flags & O_TRUNC)
92 openfiles[fd].trunc = true; 95 file->trunc = true;
93 } 96 }
94 openfiles[fd].busy = true; 97 file->busy = true;
95 98
96 /* locate filename */ 99 /* locate filename */
97 name=strrchr(pathname+1,'/'); 100 name=strrchr(pathname+1,'/');
@@ -108,7 +111,7 @@ int open(const char* pathname, int flags)
108 if (!dir) { 111 if (!dir) {
109 DEBUGF("Failed opening dir\n"); 112 DEBUGF("Failed opening dir\n");
110 errno = EIO; 113 errno = EIO;
111 openfiles[fd].busy = false; 114 file->busy = false;
112 return -4; 115 return -4;
113 } 116 }
114 117
@@ -116,38 +119,38 @@ int open(const char* pathname, int flags)
116 while ((entry = readdir(dir))) { 119 while ((entry = readdir(dir))) {
117 if ( !strcasecmp(name, entry->d_name) ) { 120 if ( !strcasecmp(name, entry->d_name) ) {
118 fat_open(entry->startcluster, 121 fat_open(entry->startcluster,
119 &(openfiles[fd].fatfile), 122 &(file->fatfile),
120 &(dir->fatdir)); 123 &(dir->fatdir));
121 openfiles[fd].size = entry->size; 124 file->size = entry->size;
122 break; 125 break;
123 } 126 }
124 } 127 }
125 closedir(dir); 128 closedir(dir);
126 if ( !entry ) { 129 if ( !entry ) {
127 LDEBUGF("Didn't find file %s\n",name); 130 LDEBUGF("Didn't find file %s\n",name);
128 if ( openfiles[fd].write && (flags & O_CREAT) ) { 131 if ( file->write && (flags & O_CREAT) ) {
129 if (fat_create_file(name, 132 if (fat_create_file(name,
130 &(openfiles[fd].fatfile), 133 &(file->fatfile),
131 &(dir->fatdir)) < 0) { 134 &(dir->fatdir)) < 0) {
132 DEBUGF("Couldn't create %s in %s\n",name,pathname); 135 DEBUGF("Couldn't create %s in %s\n",name,pathname);
133 errno = EIO; 136 errno = EIO;
134 openfiles[fd].busy = false; 137 file->busy = false;
135 return -5; 138 return -5;
136 } 139 }
137 openfiles[fd].size = 0; 140 file->size = 0;
138 } 141 }
139 else { 142 else {
140 DEBUGF("Couldn't find %s in %s\n",name,pathname); 143 DEBUGF("Couldn't find %s in %s\n",name,pathname);
141 errno = ENOENT; 144 errno = ENOENT;
142 openfiles[fd].busy = false; 145 file->busy = false;
143 return -6; 146 return -6;
144 } 147 }
145 } 148 }
146 149
147 openfiles[fd].cacheoffset = -1; 150 file->cacheoffset = -1;
148 openfiles[fd].fileoffset = 0; 151 file->fileoffset = 0;
149 152
150 if (openfiles[fd].write && (flags & O_APPEND)) { 153 if (file->write && (flags & O_APPEND)) {
151 if (lseek(fd,0,SEEK_END) < 0 ) 154 if (lseek(fd,0,SEEK_END) < 0 )
152 return -7; 155 return -7;
153 } 156 }
@@ -157,6 +160,7 @@ int open(const char* pathname, int flags)
157 160
158int close(int fd) 161int close(int fd)
159{ 162{
163 struct filedesc* file = &openfiles[fd];
160 int rc = 0; 164 int rc = 0;
161 165
162 LDEBUGF("close(%d)\n",fd); 166 LDEBUGF("close(%d)\n",fd);
@@ -165,53 +169,55 @@ int close(int fd)
165 errno = EINVAL; 169 errno = EINVAL;
166 return -1; 170 return -1;
167 } 171 }
168 if (!openfiles[fd].busy) { 172 if (!file->busy) {
169 errno = EBADF; 173 errno = EBADF;
170 return -2; 174 return -2;
171 } 175 }
172 if (openfiles[fd].write) { 176 if (file->write) {
173 /* truncate? */ 177 /* truncate? */
174 if (openfiles[fd].trunc) { 178 if (file->trunc) {
175 if (ftruncate(fd, openfiles[fd].fileoffset) < 0) 179 if (ftruncate(fd, file->fileoffset) < 0)
176 return -1; 180 return -1;
177 } 181 }
178 182
179 /* flush sector cache */ 183 /* flush sector cache */
180 if ( openfiles[fd].dirty ) { 184 if ( file->dirty ) {
181 if (flush_cache(fd) < 0) 185 if (flush_cache(fd) < 0)
182 return -2; 186 return -2;
183 } 187 }
184 188
185 /* tie up all loose ends */ 189 /* tie up all loose ends */
186 if (fat_closewrite(&(openfiles[fd].fatfile), openfiles[fd].size) < 0) 190 if (fat_closewrite(&(file->fatfile), file->size) < 0)
187 return -3; 191 return -3;
188 } 192 }
189 openfiles[fd].busy = false; 193 file->busy = false;
190 return rc; 194 return rc;
191} 195}
192 196
193int remove(const char* name) 197int remove(const char* name)
194{ 198{
195 int rc; 199 int rc;
200 struct filedesc* file;
196 int fd = open(name, O_WRONLY); 201 int fd = open(name, O_WRONLY);
197 if ( fd < 0 ) 202 if ( fd < 0 )
198 return fd; 203 return fd;
199 204
200 rc = fat_truncate(&(openfiles[fd].fatfile)); 205 file = &openfiles[fd];
206 rc = fat_truncate(&(file->fatfile));
201 if ( rc < 0 ) { 207 if ( rc < 0 ) {
202 DEBUGF("Failed truncating file\n"); 208 DEBUGF("Failed truncating file\n");
203 errno = EIO; 209 errno = EIO;
204 return -1; 210 return -1;
205 } 211 }
206 212
207 rc = fat_remove(&(openfiles[fd].fatfile)); 213 rc = fat_remove(&(file->fatfile));
208 if ( rc < 0 ) { 214 if ( rc < 0 ) {
209 DEBUGF("Failed removing file\n"); 215 DEBUGF("Failed removing file\n");
210 errno = EIO; 216 errno = EIO;
211 return -2; 217 return -2;
212 } 218 }
213 219
214 openfiles[fd].size = 0; 220 file->size = 0;
215 221
216 close(fd); 222 close(fd);
217 223
@@ -221,24 +227,25 @@ int remove(const char* name)
221int ftruncate(int fd, unsigned int size) 227int ftruncate(int fd, unsigned int size)
222{ 228{
223 int rc, sector; 229 int rc, sector;
230 struct filedesc* file = &openfiles[fd];
224 231
225 sector = size / SECTOR_SIZE; 232 sector = size / SECTOR_SIZE;
226 if (size % SECTOR_SIZE) 233 if (size % SECTOR_SIZE)
227 sector++; 234 sector++;
228 235
229 rc = fat_seek(&(openfiles[fd].fatfile), sector); 236 rc = fat_seek(&(file->fatfile), sector);
230 if (rc < 0) { 237 if (rc < 0) {
231 errno = EIO; 238 errno = EIO;
232 return -1; 239 return -1;
233 } 240 }
234 241
235 rc = fat_truncate(&(openfiles[fd].fatfile)); 242 rc = fat_truncate(&(file->fatfile));
236 if (rc < 0) { 243 if (rc < 0) {
237 errno = EIO; 244 errno = EIO;
238 return -2; 245 return -2;
239 } 246 }
240 247
241 openfiles[fd].size = size; 248 file->size = size;
242 249
243 return 0; 250 return 0;
244} 251}
@@ -246,21 +253,22 @@ int ftruncate(int fd, unsigned int size)
246static int flush_cache(int fd) 253static int flush_cache(int fd)
247{ 254{
248 int rc; 255 int rc;
249 int sector = openfiles[fd].fileoffset / SECTOR_SIZE; 256 struct filedesc* file = &openfiles[fd];
257 int sector = file->fileoffset / SECTOR_SIZE;
250 258
251 DEBUGF("Flushing dirty sector cache %x\n", sector); 259 DEBUGF("Flushing dirty sector cache %x\n", sector);
252 260
253 /* seek back one sector to get file position right */ 261 /* seek back one sector to get file position right */
254 rc = fat_seek(&(openfiles[fd].fatfile), sector); 262 rc = fat_seek(&(file->fatfile), sector);
255 if ( rc < 0 ) 263 if ( rc < 0 )
256 return -1; 264 return -1;
257 265
258 rc = fat_readwrite(&(openfiles[fd].fatfile), 1, 266 rc = fat_readwrite(&(file->fatfile), 1,
259 openfiles[fd].cache, true ); 267 file->cache, true );
260 if ( rc < 0 ) 268 if ( rc < 0 )
261 return -2; 269 return -2;
262 270
263 openfiles[fd].dirty = false; 271 file->dirty = false;
264 272
265 return 0; 273 return 0;
266} 274}
@@ -269,8 +277,9 @@ static int readwrite(int fd, void* buf, int count, bool write)
269{ 277{
270 int sectors; 278 int sectors;
271 int nread=0; 279 int nread=0;
280 struct filedesc* file = &openfiles[fd];
272 281
273 if ( !openfiles[fd].busy ) { 282 if ( !file->busy ) {
274 errno = EBADF; 283 errno = EBADF;
275 return -1; 284 return -1;
276 } 285 }
@@ -279,41 +288,41 @@ static int readwrite(int fd, void* buf, int count, bool write)
279 fd,buf,count,write?"write":"read"); 288 fd,buf,count,write?"write":"read");
280 289
281 /* attempt to read past EOF? */ 290 /* attempt to read past EOF? */
282 if (!write && count > openfiles[fd].size - openfiles[fd].fileoffset) 291 if (!write && count > file->size - file->fileoffset)
283 count = openfiles[fd].size - openfiles[fd].fileoffset; 292 count = file->size - file->fileoffset;
284 293
285 /* any head bytes? */ 294 /* any head bytes? */
286 if ( openfiles[fd].cacheoffset != -1 ) { 295 if ( file->cacheoffset != -1 ) {
287 int headbytes; 296 int headbytes;
288 int offs = openfiles[fd].cacheoffset; 297 int offs = file->cacheoffset;
289 if ( count <= SECTOR_SIZE - openfiles[fd].cacheoffset ) { 298 if ( count <= SECTOR_SIZE - file->cacheoffset ) {
290 headbytes = count; 299 headbytes = count;
291 openfiles[fd].cacheoffset += count; 300 file->cacheoffset += count;
292 if ( openfiles[fd].cacheoffset >= SECTOR_SIZE ) 301 if ( file->cacheoffset >= SECTOR_SIZE )
293 openfiles[fd].cacheoffset = -1; 302 file->cacheoffset = -1;
294 } 303 }
295 else { 304 else {
296 headbytes = SECTOR_SIZE - openfiles[fd].cacheoffset; 305 headbytes = SECTOR_SIZE - file->cacheoffset;
297 openfiles[fd].cacheoffset = -1; 306 file->cacheoffset = -1;
298 } 307 }
299 308
300 if (write) { 309 if (write) {
301 memcpy( openfiles[fd].cache + offs, buf, headbytes ); 310 memcpy( file->cache + offs, buf, headbytes );
302 if (offs+headbytes == SECTOR_SIZE) { 311 if (offs+headbytes == SECTOR_SIZE) {
303 int rc = fat_readwrite(&(openfiles[fd].fatfile), 1, 312 int rc = fat_readwrite(&(file->fatfile), 1,
304 openfiles[fd].cache, true ); 313 file->cache, true );
305 if ( rc < 0 ) { 314 if ( rc < 0 ) {
306 errno = EIO; 315 errno = EIO;
307 return -2; 316 return -2;
308 } 317 }
309 openfiles[fd].dirty = false; 318 file->dirty = false;
310 openfiles[fd].cacheoffset = -1; 319 file->cacheoffset = -1;
311 } 320 }
312 else 321 else
313 openfiles[fd].dirty = true; 322 file->dirty = true;
314 } 323 }
315 else { 324 else {
316 memcpy( buf, openfiles[fd].cache + offs, headbytes ); 325 memcpy( buf, file->cache + offs, headbytes );
317 } 326 }
318 327
319 nread = headbytes; 328 nread = headbytes;
@@ -321,7 +330,7 @@ static int readwrite(int fd, void* buf, int count, bool write)
321 } 330 }
322 331
323 /* if buffer has been modified, write it back to disk */ 332 /* if buffer has been modified, write it back to disk */
324 if (count && openfiles[fd].dirty) { 333 if (count && file->dirty) {
325 if (flush_cache(fd) < 0) 334 if (flush_cache(fd) < 0)
326 return -3; 335 return -3;
327 } 336 }
@@ -329,7 +338,7 @@ static int readwrite(int fd, void* buf, int count, bool write)
329 /* read whole sectors right into the supplied buffer */ 338 /* read whole sectors right into the supplied buffer */
330 sectors = count / SECTOR_SIZE; 339 sectors = count / SECTOR_SIZE;
331 if ( sectors ) { 340 if ( sectors ) {
332 int rc = fat_readwrite(&(openfiles[fd].fatfile), sectors, 341 int rc = fat_readwrite(&(file->fatfile), sectors,
333 buf+nread, write ); 342 buf+nread, write );
334 if ( rc < 0 ) { 343 if ( rc < 0 ) {
335 DEBUGF("Failed read/writing %d sectors\n",sectors); 344 DEBUGF("Failed read/writing %d sectors\n",sectors);
@@ -350,27 +359,27 @@ static int readwrite(int fd, void* buf, int count, bool write)
350 count=0; 359 count=0;
351 } 360 }
352 361
353 openfiles[fd].cacheoffset = -1; 362 file->cacheoffset = -1;
354 } 363 }
355 } 364 }
356 365
357 /* any tail bytes? */ 366 /* any tail bytes? */
358 if ( count ) { 367 if ( count ) {
359 if (write) { 368 if (write) {
360 if ( openfiles[fd].fileoffset + nread < openfiles[fd].size ) { 369 if ( file->fileoffset + nread < file->size ) {
361 /* sector is only partially filled. copy-back from disk */ 370 /* sector is only partially filled. copy-back from disk */
362 int rc; 371 int rc;
363 LDEBUGF("Copy-back tail cache\n"); 372 LDEBUGF("Copy-back tail cache\n");
364 rc = fat_readwrite(&(openfiles[fd].fatfile), 1, 373 rc = fat_readwrite(&(file->fatfile), 1,
365 openfiles[fd].cache, false ); 374 file->cache, false );
366 if ( rc < 0 ) { 375 if ( rc < 0 ) {
367 DEBUGF("Failed reading\n"); 376 DEBUGF("Failed reading\n");
368 errno = EIO; 377 errno = EIO;
369 return -6; 378 return -6;
370 } 379 }
371 /* seek back one sector to put file position right */ 380 /* seek back one sector to put file position right */
372 rc = fat_seek(&(openfiles[fd].fatfile), 381 rc = fat_seek(&(file->fatfile),
373 (openfiles[fd].fileoffset + nread) / 382 (file->fileoffset + nread) /
374 SECTOR_SIZE); 383 SECTOR_SIZE);
375 if ( rc < 0 ) { 384 if ( rc < 0 ) {
376 DEBUGF("fat_seek() failed\n"); 385 DEBUGF("fat_seek() failed\n");
@@ -378,29 +387,29 @@ static int readwrite(int fd, void* buf, int count, bool write)
378 return -7; 387 return -7;
379 } 388 }
380 } 389 }
381 memcpy( openfiles[fd].cache, buf + nread, count ); 390 memcpy( file->cache, buf + nread, count );
382 openfiles[fd].dirty = true; 391 file->dirty = true;
383 } 392 }
384 else { 393 else {
385 if ( fat_readwrite(&(openfiles[fd].fatfile), 1, 394 if ( fat_readwrite(&(file->fatfile), 1,
386 &(openfiles[fd].cache),false) < 1 ) { 395 &(file->cache),false) < 1 ) {
387 DEBUGF("Failed caching sector\n"); 396 DEBUGF("Failed caching sector\n");
388 errno = EIO; 397 errno = EIO;
389 return -8; 398 return -8;
390 } 399 }
391 memcpy( buf + nread, openfiles[fd].cache, count ); 400 memcpy( buf + nread, file->cache, count );
392 } 401 }
393 402
394 nread += count; 403 nread += count;
395 openfiles[fd].cacheoffset = count; 404 file->cacheoffset = count;
396 } 405 }
397 406
398 openfiles[fd].fileoffset += nread; 407 file->fileoffset += nread;
399 LDEBUGF("fileoffset: %d\n", openfiles[fd].fileoffset); 408 LDEBUGF("fileoffset: %d\n", file->fileoffset);
400 409
401 /* adjust file size to length written */ 410 /* adjust file size to length written */
402 if ( write && openfiles[fd].fileoffset > openfiles[fd].size ) 411 if ( write && file->fileoffset > file->size )
403 openfiles[fd].size = openfiles[fd].fileoffset; 412 file->size = file->fileoffset;
404 413
405 return nread; 414 return nread;
406} 415}
@@ -427,10 +436,11 @@ int lseek(int fd, int offset, int whence)
427 int oldsector; 436 int oldsector;
428 int sectoroffset; 437 int sectoroffset;
429 int rc; 438 int rc;
439 struct filedesc* file = &openfiles[fd];
430 440
431 LDEBUGF("lseek(%d,%d,%d)\n",fd,offset,whence); 441 LDEBUGF("lseek(%d,%d,%d)\n",fd,offset,whence);
432 442
433 if ( !openfiles[fd].busy ) { 443 if ( !file->busy ) {
434 errno = EBADF; 444 errno = EBADF;
435 return -1; 445 return -1;
436 } 446 }
@@ -441,59 +451,59 @@ int lseek(int fd, int offset, int whence)
441 break; 451 break;
442 452
443 case SEEK_CUR: 453 case SEEK_CUR:
444 pos = openfiles[fd].fileoffset + offset; 454 pos = file->fileoffset + offset;
445 break; 455 break;
446 456
447 case SEEK_END: 457 case SEEK_END:
448 pos = openfiles[fd].size + offset; 458 pos = file->size + offset;
449 break; 459 break;
450 460
451 default: 461 default:
452 errno = EINVAL; 462 errno = EINVAL;
453 return -2; 463 return -2;
454 } 464 }
455 if ((pos < 0) || (pos > openfiles[fd].size)) { 465 if ((pos < 0) || (pos > file->size)) {
456 errno = EINVAL; 466 errno = EINVAL;
457 return -3; 467 return -3;
458 } 468 }
459 469
460 /* new sector? */ 470 /* new sector? */
461 newsector = pos / SECTOR_SIZE; 471 newsector = pos / SECTOR_SIZE;
462 oldsector = openfiles[fd].fileoffset / SECTOR_SIZE; 472 oldsector = file->fileoffset / SECTOR_SIZE;
463 sectoroffset = pos % SECTOR_SIZE; 473 sectoroffset = pos % SECTOR_SIZE;
464 474
465 if ( (newsector != oldsector) || 475 if ( (newsector != oldsector) ||
466 ((openfiles[fd].cacheoffset==-1) && sectoroffset) ) { 476 ((file->cacheoffset==-1) && sectoroffset) ) {
467 477
468 if ( newsector != oldsector ) { 478 if ( newsector != oldsector ) {
469 if (openfiles[fd].dirty) { 479 if (file->dirty) {
470 if (flush_cache(fd) < 0) 480 if (flush_cache(fd) < 0)
471 return -5; 481 return -5;
472 } 482 }
473 483
474 rc = fat_seek(&(openfiles[fd].fatfile), newsector); 484 rc = fat_seek(&(file->fatfile), newsector);
475 if ( rc < 0 ) { 485 if ( rc < 0 ) {
476 errno = EIO; 486 errno = EIO;
477 return -4; 487 return -4;
478 } 488 }
479 } 489 }
480 if ( sectoroffset ) { 490 if ( sectoroffset ) {
481 rc = fat_readwrite(&(openfiles[fd].fatfile), 1, 491 rc = fat_readwrite(&(file->fatfile), 1,
482 &(openfiles[fd].cache),false); 492 &(file->cache),false);
483 if ( rc < 0 ) { 493 if ( rc < 0 ) {
484 errno = EIO; 494 errno = EIO;
485 return -6; 495 return -6;
486 } 496 }
487 openfiles[fd].cacheoffset = sectoroffset; 497 file->cacheoffset = sectoroffset;
488 } 498 }
489 else 499 else
490 openfiles[fd].cacheoffset = -1; 500 file->cacheoffset = -1;
491 } 501 }
492 else 502 else
493 if ( openfiles[fd].cacheoffset != -1 ) 503 if ( file->cacheoffset != -1 )
494 openfiles[fd].cacheoffset = sectoroffset; 504 file->cacheoffset = sectoroffset;
495 505
496 openfiles[fd].fileoffset = pos; 506 file->fileoffset = pos;
497 507
498 return pos; 508 return pos;
499} 509}