Update of /cvsroot/rockbox/firmware/drivers
In directory sc8-pr-cvs1:/tmp/cvs-serv4722/drivers
Modified Files:
fat.c fat.h
Log Message:
Added rename()
Index: fat.c
===================================================================
RCS file: /cvsroot/rockbox/firmware/drivers/fat.c,v
retrieving revision 1.58
retrieving revision 1.59
diff -u -b -r1.58 -r1.59
--- fat.c 18 Nov 2002 14:55:05 -0000 1.58
+++ fat.c 19 Nov 2002 12:48:50 -0000 1.59
@@ -1175,29 +1175,17 @@
return 0;
}
-int fat_remove(struct fat_file* file)
+static int free_direntries(int dircluster, int startentry, int numentries)
{
- int next, last = file->firstcluster;
-
- LDEBUGF("fat_remove(%x)\n",last);
-
- while ( last ) {
- next = get_next_cluster(last);
- update_fat_entry(last,0);
- last = next;
- }
-
- /* free all dir entries */
- if ( file->dircluster ) {
unsigned char buf[SECTOR_SIZE];
struct fat_file dir;
- unsigned int entry = file->direntry - file->direntries + 1;
+ unsigned int entry = startentry - numentries + 1;
unsigned int sector = entry / DIR_ENTRIES_PER_SECTOR;
- unsigned int i;
+ int i;
int err;
/* create a temporary file handle for the dir holding this file */
- err = fat_open(file->dircluster, &dir, NULL);
+ err = fat_open(dircluster, &dir, NULL);
if (err<0)
return -1;
@@ -1209,9 +1197,9 @@
if (err<1)
return -3;
- for (i=0; i < file->direntries; i++) {
+ for (i=0; i < numentries; i++) {
LDEBUGF("Clearing dir entry %d (%d/%d)\n",
- entry, i+1, file->direntries);
+ entry, i+1, numentries);
buf[(entry % DIR_ENTRIES_PER_SECTOR) * DIR_ENTRY_SIZE] = 0xe5;
entry++;
@@ -1225,7 +1213,7 @@
if (err<1)
return -5;
- if ( i+1 < file->direntries ) {
+ if ( i+1 < numentries ) {
/* read next sector */
err = fat_readwrite(&dir, 1, buf, false);
if (err<1)
@@ -1245,10 +1233,75 @@
if (err<1)
return -8;
}
+
+ return 0;
+}
+
+int fat_remove(struct fat_file* file)
+{
+ int next, last = file->firstcluster;
+
+ LDEBUGF("fat_remove(%x)\n",last);
+
+ while ( last ) {
+ next = get_next_cluster(last);
+ update_fat_entry(last,0);
+ last = next;
}
+ if ( file->dircluster )
+ if ( free_direntries(file->dircluster,
+ file->direntry,
+ file->direntries) < 0 )
+ return -1;
+
file->firstcluster = 0;
file->dircluster = 0;
+
+ return 0;
+}
+
+int fat_rename(struct fat_file* file,
+ unsigned char* newname,
+ int size)
+{
+ int err;
+ struct fat_dir dir;
+ struct fat_file newfile = *file;
+
+ if ( !file->dircluster ) {
+ LDEBUGF("File has no dir cluster!\n");
+ return -1;
+ }
+
+ /* create a temporary file handle */
+ LDEBUGF("create a temporary file handle: fat_opendir(%x,%x)\n",
+ &dir, file->dircluster);
+ err = fat_opendir(&dir, file->dircluster);
+ if (err<0)
+ return -2;
+
+ /* create new name */
+ LDEBUGF("create new name\n");
+ err = add_dir_entry(&dir, &newfile, newname);
+ if (err<0)
+ return -3;
+
+ /* write size and cluster link */
+ LDEBUGF("write size and cluster link\n");
+ err = update_file_size(&newfile, size);
+ if (err<0)
+ return -4;
+
+ /* remove old name */
+ LDEBUGF("remove old name\n");
+ err = free_direntries(file->dircluster, file->direntry, file->direntries);
+ if (err<0)
+ return -5;
+
+ err = flush_fat();
+ if (err<0)
+ return -6;
return 0;
}
Index: fat.h
===================================================================
RCS file: /cvsroot/rockbox/firmware/drivers/fat.h,v
retrieving revision 1.20
retrieving revision 1.21
diff -u -b -r1.20 -r1.21
--- fat.h 18 Nov 2002 11:58:43 -0000 1.20
+++ fat.h 19 Nov 2002 12:48:50 -0000 1.21
@@ -84,6 +84,9 @@
extern int fat_seek(struct fat_file *ent, unsigned int sector );
extern int fat_remove(struct fat_file *ent);
extern int fat_truncate(struct fat_file *ent);
+extern int fat_rename(struct fat_file* file,
+ unsigned char* newname,
+ int size);
extern int fat_opendir(struct fat_dir *ent, unsigned int currdir);
extern int fat_getnext(struct fat_dir *ent, struct fat_direntry *entry);
|