1
0
Fork 0
forked from len0rd/rockbox

Added remove()

git-svn-id: svn://svn.rockbox.org/rockbox/trunk@2801 a1c6a512-1295-4272-9138-f99709370657
This commit is contained in:
Björn Stenberg 2002-11-01 15:26:06 +00:00
parent 54d5e2cb27
commit 307f5d846e
6 changed files with 57 additions and 18 deletions

View file

@ -72,6 +72,7 @@ typedef struct DIRtag
extern DIR* opendir(char* name); extern DIR* opendir(char* name);
extern int closedir(DIR* dir); extern int closedir(DIR* dir);
extern int mkdir(char* name);
extern struct dirent* readdir(DIR* dir); extern struct dirent* readdir(DIR* dir);

View file

@ -184,6 +184,20 @@ int close(int fd)
return rc; return rc;
} }
int remove(const char* name)
{
int rc;
int fd = open(name, O_WRONLY);
if ( fd < 0 )
return fd;
rc = fat_remove(&(openfiles[fd].fatfile));
close(fd);
return rc;
}
static int readwrite(int fd, void* buf, int count, bool write) static int readwrite(int fd, void* buf, int count, bool write)
{ {
int sectors; int sectors;

View file

@ -466,13 +466,11 @@ static int update_fat_entry(unsigned int entry, unsigned int val)
LDEBUGF("update_fat_entry(%x,%x)\n",entry,val); LDEBUGF("update_fat_entry(%x,%x)\n",entry,val);
#ifdef TEST_FAT
if (entry==val) if (entry==val)
panicf("Creating FAT loop: %x,%x\n",entry,val); panicf("Creating FAT loop: %x,%x\n",entry,val);
if ( entry < 2 ) if ( entry < 2 )
panicf("Updating reserved FAT entry %d.\n",entry); panicf("Updating reserved FAT entry %d.\n",entry);
#endif
sec = cache_fat_sector(sector); sec = cache_fat_sector(sector);
if (!sec) if (!sec)
@ -491,6 +489,8 @@ static int update_fat_entry(unsigned int entry, unsigned int val)
fat_bpb.fsinfo.freecount++; fat_bpb.fsinfo.freecount++;
} }
LDEBUGF("update_fat_entry: %d free clusters\n", fat_bpb.fsinfo.freecount);
/* don't change top 4 bits */ /* don't change top 4 bits */
sec[offset] &= SWAB32(0xf0000000); sec[offset] &= SWAB32(0xf0000000);
sec[offset] |= SWAB32(val & 0x0fffffff); sec[offset] |= SWAB32(val & 0x0fffffff);
@ -873,13 +873,14 @@ static void update_dir_entry( struct fat_file* file, int size )
unsigned short* clusptr; unsigned short* clusptr;
int err; int err;
LDEBUGF("update_dir_entry(cluster:%x entry:%d size:%d)\n", LDEBUGF("update_dir_entry(cluster:%x entry:%d sector:%x size:%d)\n",
file->firstcluster,file->direntry,size); file->firstcluster,file->direntry,sector,size);
if ( file->direntry >= (SECTOR_SIZE / DIR_ENTRY_SIZE) ) { if ( file->direntry >= (SECTOR_SIZE / DIR_ENTRY_SIZE) )
DEBUGF("update_dir_entry(): Illegal entry %d!\n",file->direntry); panicf("update_dir_entry(): Illegal entry %d!\n",file->direntry);
return;
} if ( file->direntry < 0 )
panicf("update_dir_entry(): Illegal entry %d!\n",file->direntry);
err = ata_read_sectors(sector, 1, buf); err = ata_read_sectors(sector, 1, buf);
if (err) if (err)
@ -949,7 +950,7 @@ int fat_open(unsigned int startcluster,
/* remember where the file's dir entry is located */ /* remember where the file's dir entry is located */
file->dirsector = dir->cached_sec; file->dirsector = dir->cached_sec;
file->direntry = (dir->entry % DIR_ENTRIES_PER_SECTOR) - 1; file->direntry = dir->entry - 1;
LDEBUGF("fat_open(%x), entry %d\n",startcluster,file->direntry); LDEBUGF("fat_open(%x), entry %d\n",startcluster,file->direntry);
return 0; return 0;
} }
@ -1013,7 +1014,8 @@ int fat_closewrite(struct fat_file *file, int size)
else else
update_fat_entry(endcluster, FAT_EOF_MARK); update_fat_entry(endcluster, FAT_EOF_MARK);
update_dir_entry(file, size); if (file->dirsector)
update_dir_entry(file, size);
flush_fat(); flush_fat();
#ifdef TEST_FAT #ifdef TEST_FAT
@ -1043,13 +1045,14 @@ int fat_remove(struct fat_file* file)
LDEBUGF("fat_remove(%x)\n",last); LDEBUGF("fat_remove(%x)\n",last);
while ( last != FAT_EOF_MARK ) { while ( last ) {
LDEBUGF("Freeing cluster %x\n",last);
next = get_next_cluster(last); next = get_next_cluster(last);
update_fat_entry(last,0); update_fat_entry(last,0);
last = next; last = next;
} }
update_dir_entry(file, -1); update_dir_entry(file, -1);
file->dirsector = 0;
file->firstcluster = 0;
return 0; return 0;
} }

View file

@ -14,7 +14,7 @@ int ata_read_sectors(unsigned long start, unsigned char count, void* buf)
DEBUGF("[Reading %d blocks: 0x%lx to 0x%lx]\n", DEBUGF("[Reading %d blocks: 0x%lx to 0x%lx]\n",
count, start, start+count-1); count, start, start+count-1);
else else
DEBUGF("[Reading block 0x%lx, %d]\n", start, count); DEBUGF("[Reading block 0x%lx]\n", start);
if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) { if(fseek(file,start*BLOCK_SIZE,SEEK_SET)) {
perror("fseek"); perror("fseek");

View file

@ -267,6 +267,11 @@ void dbg_head(char* name)
close(fd); close(fd);
} }
int dbg_del(char* name)
{
return remove(name);
}
char current_directory[256] = "\\"; char current_directory[256] = "\\";
int last_secnum = 0; int last_secnum = 0;
@ -300,6 +305,7 @@ int dbg_cmd(int argc, char *argv[])
" tail <file>\n" " tail <file>\n"
" mkfile <file> <size (KB)>\n" " mkfile <file> <size (KB)>\n"
" chkfile <file>\n" " chkfile <file>\n"
" del <file>\n"
); );
return -1; return -1;
} }
@ -358,6 +364,12 @@ int dbg_cmd(int argc, char *argv[])
} }
} }
if (!strcasecmp(cmd, "del"))
{
if (arg1)
return dbg_del(arg1);
}
return 0; return 0;
} }

View file

@ -15,6 +15,7 @@ check() {
} }
try() { try() {
echo COMMAND: fat $1 $2 $3 >> $RESULT
./fat $1 $2 $3 2>> $RESULT ./fat $1 $2 $3 2>> $RESULT
RETVAL=$? RETVAL=$?
[ $RETVAL -ne 0 ] && fail [ $RETVAL -ne 0 ] && fail
@ -25,6 +26,7 @@ buildimage() {
mount -o loop $IMAGE $MOUNT mount -o loop $IMAGE $MOUNT
echo "Filling it with /etc files" echo "Filling it with /etc files"
find /etc -type f -maxdepth 1 -exec cp {} $MOUNT \; find /etc -type f -maxdepth 1 -exec cp {} $MOUNT \;
mkdir $MOUNT/dir
umount $MOUNT umount $MOUNT
} }
@ -33,8 +35,10 @@ runtests() {
echo ---Test: create a 10K file echo ---Test: create a 10K file
try mkfile /apa.txt 10 try mkfile /apa.txt 10
try mkfile /dir/apa.txt 10
check check
try chkfile /apa.txt 10 try chkfile /apa.txt 10
try chkfile /dir/apa.txt 8
echo ---Test: create a 1K file echo ---Test: create a 1K file
try mkfile /bpa.txt 1 try mkfile /bpa.txt 1
@ -64,7 +68,7 @@ runtests() {
try chkfile /bpa.txt try chkfile /bpa.txt
LOOP=50 LOOP=50
SIZE=50 SIZE=70
echo ---Test: create $LOOP $SIZE k files echo ---Test: create $LOOP $SIZE k files
for i in `seq 1 $LOOP`; for i in `seq 1 $LOOP`;
@ -73,10 +77,19 @@ runtests() {
try mkfile /rockbox.$i $SIZE try mkfile /rockbox.$i $SIZE
check check
try chkfile /rockbox.$i $SIZE try chkfile /rockbox.$i $SIZE
check
try del /rockbox.$i
check
try mkfile /rockbox.$i $SIZE
check
done done
} }
echo "Building test image (4 sector/cluster)"
buildimage 4
runtests
echo "Building test image (128 sectors/cluster)" echo "Building test image (128 sectors/cluster)"
buildimage 128 buildimage 128
runtests runtests
@ -89,10 +102,6 @@ echo "Building test image (8 sectors/cluster)"
buildimage 8 buildimage 8
runtests runtests
echo "Building test image (4 sector/cluster)"
buildimage 4
runtests
echo "Building test image (1 sector/cluster)" echo "Building test image (1 sector/cluster)"
buildimage 1 buildimage 1
runtests runtests