Example #1
0
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);
}
Example #2
0
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);
}
Example #3
0
int dfs_elm_mkfs(const char *device_name)
{
	BYTE drv;
	rt_device_t dev;
	FRESULT result;

	/* find device name */
	for (drv = 0; drv < _VOLUMES; drv ++)
	{
		dev = disk[drv];
		if (rt_strncmp(dev->parent.name, device_name, RT_NAME_MAX) == 0)
		{
			/* 1: no partition table */
			/* 0: auto selection of cluster size */
			result = f_mkfs(drv, 1, 0);
			if (result != FR_OK)
			{
				rt_kprintf("format error\n");
				return elm_result_to_dfs(result);
			}

			return DFS_STATUS_OK;
		}
	}

	/* can't find device driver */
	rt_kprintf("can not find device driver: %s\n", device_name);
	return -DFS_STATUS_EIO;
}
Example #4
0
int dfs_elm_close(struct dfs_fd *file)
{
	FRESULT result;

	result = FR_OK;
	if (file->type == FT_DIRECTORY)
	{
		DIR *dir;

		dir = (DIR *)(file->data);
		RT_ASSERT(dir != RT_NULL);

		/* release memory */
		rt_free(dir);
	}
	else if (file->type == FT_REGULAR)
	{
		FIL *fd;
		fd = (FIL *)(file->data);
		RT_ASSERT(fd != RT_NULL);

		result = f_close(fd);
		if (result == FR_OK)
		{
			/* release memory */
			rt_free(fd);
		}
	}

	return elm_result_to_dfs(result);
}
Example #5
0
int dfs_elm_statfs(struct dfs_filesystem *fs, struct statfs *buf)
{
	FATFS *f;
	FRESULT res;
	char driver[4];
	DWORD fre_clust, fre_sect, tot_sect;

	RT_ASSERT(fs != RT_NULL);
	RT_ASSERT(buf != RT_NULL);

	f = (FATFS *)fs->data;

	rt_snprintf(driver, sizeof(driver), "%d:", f->drv);
	res = f_getfree(driver, &fre_clust, &f);
	if (res) 
		return elm_result_to_dfs(res);

	/* Get total sectors and free sectors */
	tot_sect = (f->n_fatent - 2) * f->csize;
	fre_sect = fre_clust * f->csize;

	buf->f_bfree = fre_sect;
	buf->f_blocks = tot_sect;
#if _MAX_SS != 512
	buf->f_bsize = f->ssize;
#else
    buf->f_bsize = 512;
#endif

	return 0;
}
Example #6
0
int dfs_elm_unmount(struct dfs_filesystem *fs)
{
    FATFS *fat;
    FRESULT result;
    int  index;

    fat = (FATFS *)fs->data;

    RT_ASSERT(fat != RT_NULL);

    /* find the device index and then umount it */
    index = get_disk(fs->dev_id);
    if (index == -1) /* not found */
        return -DFS_STATUS_ENOENT;

    result = f_mount(RT_NULL, "", (BYTE)index);
    if (result != FR_OK)
        return elm_result_to_dfs(result);

    fs->data = RT_NULL;
    disk[index] = RT_NULL;
    rt_free(fat);

    return DFS_STATUS_OK;
}
Example #7
0
int dfs_elm_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
{
    DIR *dir;
    FILINFO fno;
    FRESULT result;
    rt_uint32_t index;
    struct dirent *d;

    dir = (DIR *)(file->data);
    RT_ASSERT(dir != RT_NULL);

    /* make integer count */
    count = (count / sizeof(struct dirent)) * sizeof(struct dirent);
    if (count == 0)
        return -DFS_STATUS_EINVAL;

    index = 0;
    while (1)
    {
        char *fn;

        d = dirp + index;

        result = f_readdir(dir, &fno);
        if (result != FR_OK || fno.fname[0] == 0)
            break;

#if _USE_LFN
        fn = *fno.fname ? fno.fname : fno.altname;
#else
        fn = fno.fname;
#endif

        d->d_type = DFS_DT_UNKNOWN;
        if (fno.fattrib & AM_DIR)
            d->d_type = DFS_DT_DIR;
        else
            d->d_type = DFS_DT_REG;

        d->d_namlen = (rt_uint8_t)rt_strlen(fn);
        d->d_reclen = (rt_uint16_t)sizeof(struct dirent);
        rt_strncpy(d->d_name, fn, rt_strlen(fn) + 1);

        index ++;
        if (index * sizeof(struct dirent) >= count)
            break;
    }

    if (index == 0)
        return elm_result_to_dfs(result);

    file->pos += index * sizeof(struct dirent);

    return index * sizeof(struct dirent);
}
Example #8
0
int dfs_elm_flush(struct dfs_fd *file)
{
	FIL *fd;
	FRESULT result;

	fd = (FIL *)(file->data);
	RT_ASSERT(fd != RT_NULL);

	result = f_sync(fd);
	return elm_result_to_dfs(result);
}
Example #9
0
int dfs_elm_read(struct dfs_fd *file, void *buf, rt_size_t len)
{
	FIL *fd;
	FRESULT result;
	UINT byte_read;

	if (file->type == FT_DIRECTORY)
	{
		return -DFS_STATUS_EISDIR;
	}

	fd = (FIL *)(file->data);
	RT_ASSERT(fd != RT_NULL);

	result = f_read(fd, buf, len, &byte_read);
	/* update position */
	file->pos  = fd->fptr;
	if (result == FR_OK)
		return byte_read;

	return elm_result_to_dfs(result);
}
Example #10
0
int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
	FATFS *fat;
	FRESULT result;
	rt_uint32_t index;

	/* handle RT-Thread device routine */
	for (index = 0; index < _VOLUMES; index ++)
	{
		if (disk[index] == RT_NULL)
		{
			break;
		}
	}
	if (index == _VOLUMES)
		return -DFS_STATUS_ENOSPC;

	/* get device */
	disk[index] = fs->dev_id;

	fat = (FATFS *)rt_malloc(sizeof(FATFS));
	if (fat == RT_NULL)
	{
		return -1;
	}

	/* mount fatfs, always 0 logic driver */
	result = f_mount(index, fat);
	if (result == FR_OK)
		fs->data = fat;
	else
	{
		rt_free(fat);
		return elm_result_to_dfs(result);
	}

	return 0;
}
Example #11
0
int dfs_elm_lseek(struct dfs_fd *file, rt_off_t offset)
{
    FRESULT result = FR_OK;
    if (file->type == FT_REGULAR)
    {
        FIL *fd;

        /* regular file type */
        fd = (FIL *)(file->data);
        RT_ASSERT(fd != RT_NULL);

        result = f_lseek(fd, offset);
        if (result == FR_OK)
        {
            /* return current position */
            file->pos = fd->fptr;
            return fd->fptr;
        }
    }
    else if (file->type == FT_DIRECTORY)
    {
        /* which is a directory */
        DIR *dir;

        dir = (DIR *)(file->data);
        RT_ASSERT(dir != RT_NULL);

        result = f_seekdir(dir, offset / sizeof(struct dirent));
        if (result == FR_OK)
        {
            /* update file position */
            file->pos = offset;
            return file->pos;
        }
    }

    return elm_result_to_dfs(result);
}
Example #12
0
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;
}
Example #13
0
int dfs_elm_mkfs(rt_device_t dev_id)
{
#define FSM_STATUS_INIT            0
#define FSM_STATUS_USE_TEMP_DRIVER 1
    FATFS *fat = RT_NULL;
    BYTE *work;
    int flag;
    FRESULT result;
    int index;

    work = rt_malloc(_MAX_SS);
    if(RT_NULL == work) {
        return -DFS_STATUS_ENOMEM;
    }

    if (dev_id == RT_NULL)
        return -DFS_STATUS_EINVAL;

    /* if the device is already mounted, then just do mkfs to the drv,
     * while if it is not mounted yet, then find an empty drive to do mkfs
     */

    flag = FSM_STATUS_INIT;
    index = get_disk(dev_id);
    if (index == -1)
    {
        /* not found the device id */
        index = get_disk(RT_NULL);
        if (index == -1)
        {
            /* no space to store an temp driver */
            rt_kprintf("sorry, there is no space to do mkfs! \n");
            return -DFS_STATUS_ENOSPC;
        }
        else
        {
            fat = rt_malloc(sizeof(FATFS));
            if (fat == RT_NULL)
                return -DFS_STATUS_ENOMEM;

            flag = FSM_STATUS_USE_TEMP_DRIVER;

            disk[index] = dev_id;
            /* try to open device */
            rt_device_open(dev_id, RT_DEVICE_OFLAG_RDWR);

            /* just fill the FatFs[vol] in ff.c, or mkfs will failded!
             * consider this condition: you just umount the elm fat,
             * then the space in FatFs[index] is released, and now do mkfs
             * on the disk, you will get a failure. so we need f_mount here,
             * just fill the FatFS[index] in elm fatfs to make mkfs work.
             */
            f_mount(fat, "", (BYTE)index);
        }
    }

    /* [IN] Logical drive number */
    /* [IN] Format options */
    /* [IN] Size of the allocation unit */
    /* [-]  Working buffer */
    /* [IN] Size of working buffer */
    result = f_mkfs("", FM_ANY, 0, work, _MAX_SS);
    rt_free(work);

    /* check flag status, we need clear the temp driver stored in disk[] */
    if (flag == FSM_STATUS_USE_TEMP_DRIVER)
    {
        rt_free(fat);
        f_mount(RT_NULL, "",(BYTE)index);
        disk[index] = RT_NULL;
        /* close device */
        rt_device_close(dev_id);
    }

    if (result != FR_OK)
    {
        rt_kprintf("format error\n");
        return elm_result_to_dfs(result);
    }

    return DFS_STATUS_OK;
}
Example #14
0
int dfs_elm_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
    FATFS *fat;
    FRESULT result;
    int index;
    struct rt_device_blk_geometry geometry;

    /* get an empty position */
    index = get_disk(RT_NULL);
    if (index == -1)
        return -DFS_STATUS_ENOENT;

    /* save device */
    disk[index] = fs->dev_id;
    /* check sector size */
    if (rt_device_control(fs->dev_id, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry) == RT_EOK)
    {
        if (geometry.bytes_per_sector > _MAX_SS)
        {
            rt_kprintf("The sector size of device is greater than the sector size of FAT.\n");
            return -DFS_STATUS_EINVAL;
        }
    }

    fat = (FATFS *)rt_malloc(sizeof(FATFS));
    if (fat == RT_NULL)
    {
        disk[index] = RT_NULL;
        return -DFS_STATUS_ENOMEM;
    }

    /* mount fatfs, always 0 logic driver */
    result = f_mount(fat,"", (BYTE)index);
    if (result == FR_OK)
    {
        char drive[8];
        DIR *dir;

        rt_snprintf(drive, sizeof(drive), "%d:/", index);
        dir = (DIR *)rt_malloc(sizeof(DIR));
        if (dir == RT_NULL)
        {
            f_mount(RT_NULL,"",(BYTE)index);
            disk[index] = RT_NULL;
            rt_free(fat);
            return -DFS_STATUS_ENOMEM;
        }

        /* open the root directory to test whether the fatfs is valid */
        result = f_opendir(dir, drive);
        if (result != FR_OK)
            goto __err;

        /* mount succeed! */
        fs->data = fat;
        rt_free(dir);
        return 0;
    }

__err:
    f_mount(RT_NULL, "", (BYTE)index);
    disk[index] = RT_NULL;
    rt_free(fat);
    return elm_result_to_dfs(result);
}