int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) { FILINFO file_info; FRESULT result; #if _VOLUMES > 1 int vol; char *drivers_fn; extern int elm_get_vol(FATFS *fat); /* add path for ELM FatFS driver support */ vol = elm_get_vol((FATFS *)fs->data); if (vol < 0) return -DFS_STATUS_ENOENT; drivers_fn = rt_malloc(256); if (drivers_fn == RT_NULL) return -DFS_STATUS_ENOMEM; rt_snprintf(drivers_fn, 256, "%d:%s", vol, path); #else const char *drivers_fn; drivers_fn = path; #endif #if _USE_LFN /* allocate long file name */ file_info.lfname = rt_malloc(256); file_info.lfsize = 256; #endif result = f_stat(drivers_fn, &file_info); #if _VOLUMES > 1 rt_free(drivers_fn); #endif if (result == FR_OK) { /* convert to dfs stat structure */ st->st_dev = 0; st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; if (file_info.fattrib & AM_DIR) { st->st_mode &= ~DFS_S_IFREG; st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; } if (file_info.fattrib & AM_RDO) st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH); st->st_size = file_info.fsize; st->st_mtime = file_info.ftime; st->st_blksize = 512; } #if _USE_LFN rt_free(file_info.lfname); #endif return elm_result_to_dfs(result); }
int dfs_elm_rename(struct dfs_filesystem *fs, const char *oldpath, const char *newpath) { FRESULT result; #if _VOLUMES > 1 char *drivers_oldfn; const char *drivers_newfn; int vol; extern int elm_get_vol(FATFS *fat); /* add path for ELM FatFS driver support */ vol = elm_get_vol((FATFS *)fs->data); if (vol < 0) return -DFS_STATUS_ENOENT; drivers_oldfn = rt_malloc(256); if (drivers_oldfn == RT_NULL) return -DFS_STATUS_ENOMEM; drivers_newfn = newpath; rt_snprintf(drivers_oldfn, 256, "%d:%s", vol, oldpath); #else const char *drivers_oldfn, *drivers_newfn; drivers_oldfn = oldpath; drivers_newfn = newpath; #endif result = f_rename(drivers_oldfn, drivers_newfn); #if _VOLUMES > 1 rt_free(drivers_oldfn); #endif return elm_result_to_dfs(result); }
int dfs_elm_open(struct dfs_fd *file) { FIL *fd; BYTE mode; FRESULT result; char *drivers_fn; #if (_VOLUMES > 1) int vol; extern int elm_get_vol(FATFS *fat); /* add path for ELM FatFS driver support */ vol = elm_get_vol((FATFS *)file->fs->data); if (vol < 0) return -DFS_STATUS_ENOENT; drivers_fn = rt_malloc(256); if (drivers_fn == RT_NULL) return -DFS_STATUS_ENOMEM; rt_snprintf(drivers_fn, 256, "%d:%s", vol, file->path); #else drivers_fn = file->path; #endif if (file->flags & DFS_O_DIRECTORY) { DIR *dir; if (file->flags & DFS_O_CREAT) { result = f_mkdir(drivers_fn); if (result != FR_OK) { #if _VOLUMES > 1 rt_free(drivers_fn); #endif return elm_result_to_dfs(result); } } /* open directory */ dir = (DIR *)rt_malloc(sizeof(DIR)); if (dir == RT_NULL) { #if _VOLUMES > 1 rt_free(drivers_fn); #endif return -DFS_STATUS_ENOMEM; } result = f_opendir(dir, drivers_fn); #if _VOLUMES > 1 rt_free(drivers_fn); #endif if (result != FR_OK) { rt_free(dir); return elm_result_to_dfs(result); } file->data = dir; return DFS_STATUS_OK; } else { mode = FA_READ; if (file->flags & DFS_O_WRONLY) mode |= FA_WRITE; if ((file->flags & DFS_O_ACCMODE) & DFS_O_RDWR) mode |= FA_WRITE; /* Opens the file, if it is existing. If not, a new file is created. */ if (file->flags & DFS_O_CREAT) mode |= FA_OPEN_ALWAYS; /* Creates a new file. If the file is existing, it is truncated and overwritten. */ if (file->flags & DFS_O_TRUNC) mode |= FA_CREATE_ALWAYS; /* Creates a new file. The function fails if the file is already existing. */ if (file->flags & DFS_O_EXCL) mode |= FA_CREATE_NEW; /* allocate a fd */ fd = (FIL *)rt_malloc(sizeof(FIL)); if (fd == RT_NULL) { return -DFS_STATUS_ENOMEM; } result = f_open(fd, drivers_fn, mode); #if _VOLUMES > 1 rt_free(drivers_fn); #endif if (result == FR_OK) { file->pos = fd->fptr; file->size = fd->fsize; file->data = fd; if (file->flags & DFS_O_APPEND) { file->pos = f_lseek(fd, fd->fsize); } } else { /* open failed, return */ rt_free(fd); return elm_result_to_dfs(result); } } return DFS_STATUS_OK; }
int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st) { FILINFO file_info; FRESULT result; #if _VOLUMES > 1 int vol; char *drivers_fn; extern int elm_get_vol(FATFS * fat); /* add path for ELM FatFS driver support */ vol = elm_get_vol((FATFS *)fs->data); if (vol < 0) return -DFS_STATUS_ENOENT; drivers_fn = rt_malloc(256); if (drivers_fn == RT_NULL) return -DFS_STATUS_ENOMEM; rt_snprintf(drivers_fn, 256, "%d:%s", vol, path); #else const char *drivers_fn; drivers_fn = path; #endif result = f_stat(drivers_fn, &file_info); #if _VOLUMES > 1 rt_free(drivers_fn); #endif if (result == FR_OK) { /* convert to dfs stat structure */ st->st_dev = 0; st->st_mode = DFS_S_IFREG | DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH | DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH; if (file_info.fattrib & AM_DIR) { st->st_mode &= ~DFS_S_IFREG; st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH; } if (file_info.fattrib & AM_RDO) st->st_mode &= ~(DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH); st->st_size = file_info.fsize; /* get st_mtime. */ { struct tm tm_file; int year, mon, day, hour, min, sec; WORD tmp; tmp = file_info.fdate; day = tmp & 0x1F; /* bit[4:0] Day(1..31) */ tmp >>= 5; mon = tmp & 0x0F; /* bit[8:5] Month(1..12) */ tmp >>= 4; year = (tmp & 0x7F) + 1980; /* bit[15:9] Year origin from 1980(0..127) */ tmp = file_info.ftime; sec = (tmp & 0x1F) * 2; /* bit[4:0] Second/2(0..29) */ tmp >>= 5; min = tmp & 0x3F; /* bit[10:5] Minute(0..59) */ tmp >>= 6; hour = tmp & 0x1F; /* bit[15:11] Hour(0..23) */ memset(&tm_file, 0, sizeof(tm_file)); tm_file.tm_year = year - 1900; /* Years since 1900 */ tm_file.tm_mon = mon - 1; /* Months *since* january: 0-11 */ tm_file.tm_mday = day; /* Day of the month: 1-31 */ tm_file.tm_hour = hour; /* Hours since midnight: 0-23 */ tm_file.tm_min = min; /* Minutes: 0-59 */ tm_file.tm_sec = sec; /* Seconds: 0-59 */ st->st_mtime = mktime(&tm_file); } /* get st_mtime. */ }