rewrite fatfs error translation function

This commit is contained in:
sunphsjtu 2021-04-29 10:12:43 +08:00
parent 34383bdcde
commit bfb176db32
1 changed files with 36 additions and 55 deletions

View File

@ -27,43 +27,24 @@
struct ff_disk ff_disks[FF_VOLUMES]; struct ff_disk ff_disks[FF_VOLUMES];
static int TranslateError(FRESULT error) static int GetErrno(FRESULT error)
{ {
int status = EOK; if (error == FR_OK)
return EOK;
switch (error) { else if (error == FR_NO_FILE || error == FR_NO_PATH || error == FR_NO_FILESYSTEM)
case FR_OK: return -ENOENT;
break; else if (error == FR_INVALID_NAME)
case FR_NO_FILE: return -EINVAL;
case FR_NO_PATH: else if (error == FR_EXIST || error == FR_INVALID_OBJECT)
case FR_NO_FILESYSTEM: return -EEXIST;
status = -ENOENT; else if (error == FR_DISK_ERR || error == FR_NOT_READY || error == FR_INT_ERR)
break; return -EIO;
case FR_INVALID_NAME: else if (error == FR_WRITE_PROTECTED || error == FR_DENIED)
status = -EINVAL; return -EROFS;
break; else if (error == FR_MKFS_ABORTED)
case FR_EXIST: return -EINVAL;
case FR_INVALID_OBJECT: else
status = -EEXIST; return -1;
break;
case FR_DISK_ERR:
case FR_NOT_READY:
case FR_INT_ERR:
status = -EIO;
break;
case FR_WRITE_PROTECTED:
case FR_DENIED:
status = -EROFS;
break;
case FR_MKFS_ABORTED:
status = -EINVAL;
break;
default:
status = -1;
break;
}
return status;
} }
static int GetDisk(HardwareDevType dev) static int GetDisk(HardwareDevType dev)
@ -104,7 +85,7 @@ static int FatfsOpen(struct FileDescriptor *fdp, const char *path)
res = f_open(fdp->data, ff_path, fs_mode); res = f_open(fdp->data, ff_path, fs_mode);
free(ff_path); free(ff_path);
return TranslateError(res); return GetErrno(res);
} }
static int FatfsClose(struct FileDescriptor *fdp) static int FatfsClose(struct FileDescriptor *fdp)
@ -119,7 +100,7 @@ static int FatfsClose(struct FileDescriptor *fdp)
fdp->data = NULL; fdp->data = NULL;
} }
return TranslateError(res); return GetErrno(res);
} }
static ssize_t FatfsRead(struct FileDescriptor *fdp, void *dst, size_t len) static ssize_t FatfsRead(struct FileDescriptor *fdp, void *dst, size_t len)
@ -129,7 +110,7 @@ static ssize_t FatfsRead(struct FileDescriptor *fdp, void *dst, size_t len)
res = f_read(fdp->data, dst, len, &br); res = f_read(fdp->data, dst, len, &br);
if (res != FR_OK) if (res != FR_OK)
return TranslateError(res); return GetErrno(res);
return br; return br;
} }
@ -141,7 +122,7 @@ static ssize_t FatfsWrite(struct FileDescriptor *fdp, const void *src, size_t le
res = f_write(fdp->data, src, len, &bw); res = f_write(fdp->data, src, len, &bw);
if (res != FR_OK) if (res != FR_OK)
return TranslateError(res); return GetErrno(res);
return bw; return bw;
} }
@ -172,7 +153,7 @@ static int FatfsSeek(struct FileDescriptor *fdp, off_t offset, int whence,
res = f_lseek(fdp->data, pos); res = f_lseek(fdp->data, pos);
*new_offset = (off_t)f_tell((FIL *)fdp->data); *new_offset = (off_t)f_tell((FIL *)fdp->data);
return TranslateError(res); return GetErrno(res);
} }
static off_t FatfsTell(struct FileDescriptor *fdp) static off_t FatfsTell(struct FileDescriptor *fdp)
@ -188,7 +169,7 @@ static int FatfsTruncate(struct FileDescriptor *fdp, off_t length)
/* expand file if new position is larger than file size */ /* expand file if new position is larger than file size */
res = f_lseek(fdp->data, length); res = f_lseek(fdp->data, length);
if (res != FR_OK) if (res != FR_OK)
return TranslateError(res); return GetErrno(res);
if (length < curr_len) if (length < curr_len)
res = f_truncate(fdp->data); res = f_truncate(fdp->data);
@ -198,7 +179,7 @@ static int FatfsTruncate(struct FileDescriptor *fdp, off_t length)
res = f_lseek(fdp->data, curr_len); res = f_lseek(fdp->data, curr_len);
if (res != FR_OK) if (res != FR_OK)
return TranslateError(res); return GetErrno(res);
unsigned int bw; unsigned int bw;
uint8_t c = 0; uint8_t c = 0;
@ -210,7 +191,7 @@ static int FatfsTruncate(struct FileDescriptor *fdp, off_t length)
} }
} }
return TranslateError(res); return GetErrno(res);
} }
static int FatfsSync(struct FileDescriptor *fdp) static int FatfsSync(struct FileDescriptor *fdp)
@ -219,7 +200,7 @@ static int FatfsSync(struct FileDescriptor *fdp)
res = f_sync(fdp->data); res = f_sync(fdp->data);
return TranslateError(res); return GetErrno(res);
} }
static int FatfsOpendir(struct FileDescriptor *fdp, const char *path) static int FatfsOpendir(struct FileDescriptor *fdp, const char *path)
@ -238,7 +219,7 @@ static int FatfsOpendir(struct FileDescriptor *fdp, const char *path)
res = f_opendir((DIR *)fdp->data, ff_path); res = f_opendir((DIR *)fdp->data, ff_path);
free(ff_path); free(ff_path);
return TranslateError(res); return GetErrno(res);
} }
static int FatfsClosedir(struct FileDescriptor *fdp) static int FatfsClosedir(struct FileDescriptor *fdp)
@ -252,7 +233,7 @@ static int FatfsClosedir(struct FileDescriptor *fdp)
fdp->data = NULL; fdp->data = NULL;
} }
return TranslateError(res); return GetErrno(res);
} }
static int FatfsReaddir(struct FileDescriptor *fdp, struct dirent *dirent) static int FatfsReaddir(struct FileDescriptor *fdp, struct dirent *dirent)
@ -266,7 +247,7 @@ static int FatfsReaddir(struct FileDescriptor *fdp, struct dirent *dirent)
strcpy(dirent->d_name, fno.fname); strcpy(dirent->d_name, fno.fname);
} }
return TranslateError(res); return GetErrno(res);
} }
static int FatfsMount(struct MountPoint *mp) static int FatfsMount(struct MountPoint *mp)
@ -298,7 +279,7 @@ static int FatfsMount(struct MountPoint *mp)
CHECK(res == FR_OK); CHECK(res == FR_OK);
return TranslateError(res); return GetErrno(res);
} }
static int FatfsUnmount(struct MountPoint *mp) static int FatfsUnmount(struct MountPoint *mp)
@ -316,7 +297,7 @@ static int FatfsUnmount(struct MountPoint *mp)
ff_disks[i].dev = NULL; ff_disks[i].dev = NULL;
} }
return TranslateError(res); return GetErrno(res);
} }
static int FatfsUnlink(struct MountPoint *mp, const char *path) static int FatfsUnlink(struct MountPoint *mp, const char *path)
@ -328,7 +309,7 @@ static int FatfsUnlink(struct MountPoint *mp, const char *path)
res = f_unlink(ff_path); res = f_unlink(ff_path);
free(ff_path); free(ff_path);
return TranslateError(res); return GetErrno(res);
} }
static int FatfsRename(struct MountPoint *mp, const char *from, static int FatfsRename(struct MountPoint *mp, const char *from,
@ -355,7 +336,7 @@ err:
free(ff_path_from); free(ff_path_from);
free(ff_path_to); free(ff_path_to);
return TranslateError(res); return GetErrno(res);
} }
static int FatfsMkdir(struct MountPoint *mp, const char *path) static int FatfsMkdir(struct MountPoint *mp, const char *path)
@ -367,7 +348,7 @@ static int FatfsMkdir(struct MountPoint *mp, const char *path)
res = f_mkdir(ff_path); res = f_mkdir(ff_path);
free(ff_path); free(ff_path);
return TranslateError(res); return GetErrno(res);
} }
static int FatfsStat(struct MountPoint *mp, const char *path, static int FatfsStat(struct MountPoint *mp, const char *path,
@ -417,7 +398,7 @@ static int FatfsStat(struct MountPoint *mp, const char *path,
err: err:
free(ff_path); free(ff_path);
return TranslateError(res); return GetErrno(res);
} }
static int FatfsStatvfs(struct MountPoint *mp, const char *path, static int FatfsStatvfs(struct MountPoint *mp, const char *path,
@ -447,7 +428,7 @@ static int FatfsStatvfs(struct MountPoint *mp, const char *path,
buf->f_bsize = 512; buf->f_bsize = 512;
#endif #endif
return TranslateError(res); return GetErrno(res);
} }
/* fatfs filesystem interface */ /* fatfs filesystem interface */