dfs_romfs.c 7.2 KB
Newer Older
1 2 3 4 5
/*
 * File      : dfs_romfs.c
 * This file is part of Device File System in RT-Thread RTOS
 * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
 *
Y
yiyue.fang 已提交
6 7 8 9 10 11 12 13 14 15 16 17 18
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License along
 *  with this program; if not, write to the Free Software Foundation, Inc.,
 *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
 *
 * Change Logs:
 * Date           Author       Notes
 */

#include <rtthread.h>
#include <dfs.h>
#include <dfs_fs.h>
#include "dfs_romfs.h"

int dfs_romfs_mount(struct dfs_filesystem *fs, unsigned long rwflag, const void *data)
{
	struct romfs_dirent *root_dirent;

	if (data == RT_NULL)
		return -DFS_STATUS_EIO;

	root_dirent = (struct romfs_dirent *)data;
	fs->data = root_dirent;

	return DFS_STATUS_OK;
}

int dfs_romfs_unmount(struct dfs_filesystem *fs)
{
	return DFS_STATUS_OK;
}

int dfs_romfs_ioctl(struct dfs_fd *file, int cmd, void *args)
{
	return -DFS_STATUS_EIO;
}

G
Grissiom 已提交
52 53
rt_inline int check_dirent(struct romfs_dirent *dirent)
{
54 55
    if ((dirent->type != ROMFS_DIRENT_FILE && dirent->type != ROMFS_DIRENT_DIR)
        || dirent->size == ~0)
G
Grissiom 已提交
56 57 58 59
        return -1;
    return 0;
}

60 61 62 63 64 65 66
struct romfs_dirent *dfs_romfs_lookup(struct romfs_dirent *root_dirent, const char *path, rt_size_t *size)
{
	rt_size_t index, found;
	const char *subpath, *subpath_end;
	struct romfs_dirent *dirent;
	rt_size_t dirent_size;

G
Grissiom 已提交
67 68 69 70
    /* Check the root_dirent. */
    if (check_dirent(root_dirent) != 0)
        return RT_NULL;

G
Grissiom 已提交
71
	if (path[0] == '/' && path[1] == '\0')
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
	{
		*size = root_dirent->size;
		return root_dirent;
	}

	/* goto root directy entries */
	dirent = (struct romfs_dirent *)root_dirent->data;
	dirent_size = root_dirent->size;

	/* get the end position of this subpath */
	subpath_end = path;
	/* skip /// */
	while (*subpath_end && *subpath_end == '/')
		subpath_end ++;
	subpath = subpath_end;
	while ((*subpath_end != '/') && *subpath_end)
		subpath_end ++;

	while (dirent != RT_NULL)
	{
		found = 0;
G
Grissiom 已提交
93

94 95 96
		/* search in folder */
		for (index = 0; index < dirent_size; index ++)
		{
G
Grissiom 已提交
97 98
            if (check_dirent(&dirent[index]) != 0)
                return RT_NULL;
99 100
			if (rt_strlen(dirent[index].name) == (subpath_end - subpath) &&
					rt_strncmp(dirent[index].name, subpath, (subpath_end - subpath)) == 0)
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
			{
				dirent_size = dirent[index].size;

				/* skip /// */
				while (*subpath_end && *subpath_end == '/')
					subpath_end ++;
				subpath = subpath_end;
				while ((*subpath_end != '/') && *subpath_end)
					subpath_end ++;

				if (!(*subpath))
				{
					*size = dirent_size;
					return &dirent[index];
				}

				if (dirent[index].type == ROMFS_DIRENT_DIR)
				{
					/* enter directory */
					dirent = (struct romfs_dirent*)dirent[index].data;
					found = 1;
					break;
				}
G
Grissiom 已提交
124
				else
125 126 127 128
				{
					/* return file dirent */
					if (subpath != RT_NULL)
						break; /* not the end of path */
G
Grissiom 已提交
129

130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
					return &dirent[index];
				}
			}
		}

		if (!found)
			break; /* not found */
	}

	/* not found */
	return RT_NULL;
}

int dfs_romfs_read(struct dfs_fd *file, void *buf, rt_size_t count)
{
	rt_size_t length;
	struct romfs_dirent *dirent;

	dirent = (struct romfs_dirent *)file->data;
	RT_ASSERT(dirent != RT_NULL);

G
Grissiom 已提交
151 152 153 154 155
    if (check_dirent(dirent) != 0)
    {
        return -DFS_STATUS_EIO;
    }

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
	if (count < file->size - file->pos)
		length = count;
	else
		length = file->size - file->pos;

	if (length > 0)
		memcpy(buf, &(dirent->data[file->pos]), length);

	/* update file current position */
	file->pos += length;

	return length;
}

int dfs_romfs_lseek(struct dfs_fd *file, rt_off_t offset)
{
	if (offset <= file->size)
	{
		file->pos = offset;
		return file->pos;
	}

	return -DFS_STATUS_EIO;
}

int dfs_romfs_close(struct dfs_fd *file)
{
	file->data = RT_NULL;
	return DFS_STATUS_OK;
}

int dfs_romfs_open(struct dfs_fd *file)
{
	rt_size_t size;
	struct romfs_dirent *dirent;
	struct romfs_dirent *root_dirent;

	root_dirent = (struct romfs_dirent *)file->fs->data;

195
    if (check_dirent(root_dirent) != 0)
G
Grissiom 已提交
196 197
        return -DFS_STATUS_EIO;

198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
	if (file->flags & (DFS_O_CREAT | DFS_O_WRONLY | DFS_O_APPEND | DFS_O_TRUNC | DFS_O_RDWR))
		return -DFS_STATUS_EINVAL;

	dirent = dfs_romfs_lookup(root_dirent, file->path, &size);
	if (dirent == RT_NULL)
		return -DFS_STATUS_ENOENT;

	/* entry is a directory file type */
	if (dirent->type == ROMFS_DIRENT_DIR)
	{
		if (!(file->flags & DFS_O_DIRECTORY))
			return -DFS_STATUS_ENOENT;
	}
	else
	{
		/* entry is a file, but open it as a directory */
		if (file->flags & DFS_O_DIRECTORY)
			return -DFS_STATUS_ENOENT;
	}

	file->data = dirent;
	file->size = size;
	file->pos = 0;

	return DFS_STATUS_OK;
}

int dfs_romfs_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
{
	rt_size_t size;
	struct romfs_dirent *dirent;
	struct romfs_dirent *root_dirent;

	root_dirent = (struct romfs_dirent *)fs->data;
	dirent = dfs_romfs_lookup(root_dirent, path, &size);

	if (dirent == RT_NULL)
		return -DFS_STATUS_ENOENT;

	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 (dirent->type == ROMFS_DIRENT_DIR)
	{
		st->st_mode &= ~DFS_S_IFREG;
		st->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
	}

	st->st_size = dirent->size;
	st->st_mtime = 0;

	return DFS_STATUS_OK;
}

int dfs_romfs_getdents(struct dfs_fd *file, struct dirent *dirp, rt_uint32_t count)
{
	rt_size_t index;
	const char *name;
	struct dirent *d;
	struct romfs_dirent *dirent, *sub_dirent;

	dirent = (struct romfs_dirent *)file->data;
G
Grissiom 已提交
261 262
    if (check_dirent(dirent) != 0)
        return -DFS_STATUS_EIO;
263 264 265 266
	RT_ASSERT(dirent->type == ROMFS_DIRENT_DIR);

	/* enter directory */
	dirent = (struct romfs_dirent *)dirent->data;
G
Grissiom 已提交
267

268 269 270 271
	/* make integer count */
	count = (count / sizeof(struct dirent));
	if (count == 0)
		return -DFS_STATUS_EINVAL;
G
Grissiom 已提交
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
	index = 0;
	for (index = 0; index < count && file->pos < file->size; index ++)
	{
		d = dirp + index;

		sub_dirent = &dirent[file->pos];
		name = sub_dirent->name;

		/* fill dirent */
		if (sub_dirent->type == ROMFS_DIRENT_DIR)
			d->d_type = DFS_DT_DIR;
		else
			d->d_type = DFS_DT_REG;

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

		/* move to next position */
G
Grissiom 已提交
292
		++ file->pos;
293 294 295 296 297
	}

	return index * sizeof(struct dirent);
}

G
Grissiom 已提交
298
static const struct dfs_filesystem_operation _romfs =
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
{
	"rom",
	DFS_FS_FLAG_DEFAULT,
	dfs_romfs_mount,
	dfs_romfs_unmount,
	RT_NULL,
	RT_NULL,

	dfs_romfs_open,
	dfs_romfs_close,
	dfs_romfs_ioctl,
	dfs_romfs_read,
	RT_NULL,
	RT_NULL,
	dfs_romfs_lseek,
	dfs_romfs_getdents,
	RT_NULL,
	dfs_romfs_stat,
	RT_NULL,
};

int dfs_romfs_init(void)
{
    /* register rom file system */
    dfs_register(&_romfs);
	return 0;
}
B
Bernard Xiong 已提交
326
INIT_FS_EXPORT(dfs_romfs_init);
327