libdisk: error handling, sanity checks
- make sure disk file is an actual file and not a directory, log or empty file - sanity check: file has to be at least one sector to be a disk - simplify error handling using freedisk()
This commit is contained in:
parent
ce76af64fe
commit
3e124e1f13
1 changed files with 34 additions and 18 deletions
|
@ -208,15 +208,26 @@ findgeometry(Disk *disk)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static Disk*
|
||||||
|
freedisk(Disk *d)
|
||||||
|
{
|
||||||
|
if(d->fd >= 0)
|
||||||
|
close(d->fd);
|
||||||
|
if(d->wfd >= 0)
|
||||||
|
close(d->wfd);
|
||||||
|
if(d->ctlfd >= 0)
|
||||||
|
close(d->ctlfd);
|
||||||
|
free(d);
|
||||||
|
return nil;
|
||||||
|
}
|
||||||
|
|
||||||
static Disk*
|
static Disk*
|
||||||
openfile(Disk *disk)
|
openfile(Disk *disk)
|
||||||
{
|
{
|
||||||
Dir *d;
|
Dir *d;
|
||||||
|
|
||||||
if((d = dirfstat(disk->fd)) == nil){
|
if((d = dirfstat(disk->fd)) == nil)
|
||||||
free(disk);
|
return freedisk(disk);
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
|
|
||||||
disk->secsize = 512;
|
disk->secsize = 512;
|
||||||
disk->size = d->length;
|
disk->size = d->length;
|
||||||
|
@ -224,6 +235,11 @@ openfile(Disk *disk)
|
||||||
disk->offset = 0;
|
disk->offset = 0;
|
||||||
free(d);
|
free(d);
|
||||||
|
|
||||||
|
if(disk->secs == 0){
|
||||||
|
werrstr("file too small to be a disk");
|
||||||
|
return freedisk(disk);
|
||||||
|
}
|
||||||
|
|
||||||
findgeometry(disk);
|
findgeometry(disk);
|
||||||
return mkwidth(disk);
|
return mkwidth(disk);
|
||||||
}
|
}
|
||||||
|
@ -274,6 +290,7 @@ opendisk(char *disk, int rdonly, int noctl)
|
||||||
{
|
{
|
||||||
char *p, *q;
|
char *p, *q;
|
||||||
Disk *d;
|
Disk *d;
|
||||||
|
Dir *s;
|
||||||
|
|
||||||
d = mallocz(sizeof(*d), 1);
|
d = mallocz(sizeof(*d), 1);
|
||||||
if(d == nil)
|
if(d == nil)
|
||||||
|
@ -284,10 +301,17 @@ opendisk(char *disk, int rdonly, int noctl)
|
||||||
|
|
||||||
d->fd = open(disk, OREAD);
|
d->fd = open(disk, OREAD);
|
||||||
if(d->fd < 0) {
|
if(d->fd < 0) {
|
||||||
werrstr("cannot open disk file");
|
werrstr("cannot open disk file: %r");
|
||||||
free(d);
|
return freedisk(d);
|
||||||
return nil;
|
|
||||||
}
|
}
|
||||||
|
if((s = dirfstat(d->fd)) == nil)
|
||||||
|
return freedisk(d);
|
||||||
|
if((s->mode & (DMDIR|DMAPPEND)) != 0){
|
||||||
|
free(s);
|
||||||
|
werrstr("not a disk file: %s", disk);
|
||||||
|
return freedisk(d);
|
||||||
|
}
|
||||||
|
free(s);
|
||||||
|
|
||||||
if(rdonly == 0) {
|
if(rdonly == 0) {
|
||||||
d->wfd = open(disk, OWRITE);
|
d->wfd = open(disk, OWRITE);
|
||||||
|
@ -299,12 +323,8 @@ opendisk(char *disk, int rdonly, int noctl)
|
||||||
return openfile(d);
|
return openfile(d);
|
||||||
|
|
||||||
p = malloc(strlen(disk) + 4); /* 4: slop for "ctl\0" */
|
p = malloc(strlen(disk) + 4); /* 4: slop for "ctl\0" */
|
||||||
if(p == nil) {
|
if(p == nil)
|
||||||
close(d->wfd);
|
return freedisk(d);
|
||||||
close(d->fd);
|
|
||||||
free(d);
|
|
||||||
return nil;
|
|
||||||
}
|
|
||||||
strcpy(p, disk);
|
strcpy(p, disk);
|
||||||
|
|
||||||
/* check for floppy(3) disk */
|
/* check for floppy(3) disk */
|
||||||
|
@ -334,12 +354,8 @@ opendisk(char *disk, int rdonly, int noctl)
|
||||||
d->type = Tsd;
|
d->type = Tsd;
|
||||||
d->part = strdup(disk+(q-p));
|
d->part = strdup(disk+(q-p));
|
||||||
if(d->part == nil){
|
if(d->part == nil){
|
||||||
close(d->ctlfd);
|
|
||||||
close(d->wfd);
|
|
||||||
close(d->fd);
|
|
||||||
free(p);
|
free(p);
|
||||||
free(d);
|
return freedisk(d);
|
||||||
return nil;
|
|
||||||
}
|
}
|
||||||
return opensd(d);
|
return opensd(d);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue