dfs.c 7.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * File      : dfs.c
 * This file is part of Device File System in RT-Thread RTOS
 * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rt-thread.org/license/LICENSE.
 *
 * Change Logs:
 * Date           Author       Notes
 * 2005-02-22     Bernard      The first version.
 * 2010-07-16
 */

#include <dfs.h>
#include <dfs_fs.h>
#include <dfs_file.h>

B
bernard.xiong 已提交
20 21
#define NO_WORKING_DIR	"system does not support working dir\n"

22 23 24 25 26 27 28 29
/* Global variables */
const struct dfs_filesystem_operation* filesystem_operation_table[DFS_FILESYSTEM_TYPES_MAX];
struct dfs_filesystem filesystem_table[DFS_FILESYSTEMS_MAX];

/* device filesystem lock */
static struct rt_mutex fslock;

#ifdef DFS_USING_WORKDIR
30
char working_directory[DFS_PATH_MAX] = {"/"};
31 32 33 34 35 36 37 38
#endif

#ifdef DFS_USING_STDIO
struct dfs_fd fd_table[3 + DFS_FD_MAX];
#else
struct dfs_fd fd_table[DFS_FD_MAX];
#endif

B
bernard.xiong@gmail.com 已提交
39 40 41 42 43
/**
 * @addtogroup DFS
 */
/*@{*/

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
/**
 * this function will initialize device file system.
 */
void dfs_init()
{
	/* clear filesystem operations table */
	rt_memset(filesystem_operation_table, 0, sizeof(filesystem_operation_table));
	/* clear filesystem table */
	rt_memset(filesystem_table, 0, sizeof(filesystem_table));
	/* clean fd table */
	rt_memset(fd_table, 0, sizeof(fd_table));

	/* create device filesystem lock */
	rt_mutex_init(&fslock, "fslock", RT_IPC_FLAG_FIFO);

#ifdef DFS_USING_WORKDIR
	/* set current working directory */
	rt_memset(working_directory, 0, sizeof(working_directory));
	working_directory[0] = '/';
#endif
}

/**
 * this function will lock device file system.
 *
B
bernard.xiong@gmail.com 已提交
69
 * @note please don't invoke it on ISR.
70 71 72 73 74 75
 */
void dfs_lock()
{
	rt_err_t result;

	result = rt_mutex_take(&fslock, RT_WAITING_FOREVER);
76 77 78 79
	if (result != RT_EOK)
	{
		RT_ASSERT(0);
	}
80 81 82 83 84
}

/**
 * this function will lock device file system.
 *
B
bernard.xiong@gmail.com 已提交
85
 * @note please don't invoke it on ISR.
86 87 88 89 90 91 92
 */
void dfs_unlock()
{
	rt_mutex_release(&fslock);
}

/**
B
bernard.xiong@gmail.com 已提交
93 94
 * @ingroup Fd
 * This function will allocate a file descriptor.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
 *
 * @return -1 on failed or the allocated file descriptor.
 */
int fd_new(void)
{
	struct dfs_fd* d;
	int idx;

	/* lock filesystem */
	dfs_lock();

	/* find an empty fd entry */
#ifdef DFS_USING_STDIO
	for (idx = 3; idx < DFS_FD_MAX + 3 && fd_table[idx].ref_count > 0; idx++);
#else
	for (idx = 0; idx < DFS_FD_MAX && fd_table[idx].ref_count > 0; idx++);
#endif

	/* can't find an empty fd entry */
#ifdef DFS_USING_STDIO
	if (idx == DFS_FD_MAX + 3)
#else
	if (idx == DFS_FD_MAX)
#endif
	{
		idx = -1;
		goto __result;
	}

	d = &(fd_table[idx]);
	d->ref_count = 1;

__result:
	dfs_unlock();
	return idx;
}

/**
B
bernard.xiong@gmail.com 已提交
133 134 135
 * @ingroup Fd
 *
 * This function will return a file descriptor structure according to file
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
 * descriptor.
 *
 * @return NULL on on this file descriptor or the file descriptor structure
 * pointer.
 */
struct dfs_fd* fd_get(int fd)
{
	struct dfs_fd* d;

#ifdef DFS_USING_STDIO
	if ( fd < 3 || fd > DFS_FD_MAX + 3) return RT_NULL;
#else
	if ( fd < 0 || fd > DFS_FD_MAX ) return RT_NULL;
#endif

	dfs_lock();
	d = &fd_table[fd];

	/* increase the reference count */
	d->ref_count ++;
	dfs_unlock();

	return d;
}

/**
B
bernard.xiong@gmail.com 已提交
162 163 164
 * @ingroup Fd
 *
 * This function will put the file descriptor.
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
 */
void fd_put(struct dfs_fd* fd)
{
	dfs_lock();
	fd->ref_count --;

	/* clear this fd entry */
	if ( fd->ref_count == 0 )
	{
		rt_memset(fd, 0, sizeof(struct dfs_fd));
	}
	dfs_unlock();
};

/** 
B
bernard.xiong@gmail.com 已提交
180 181 182
 * @ingroup Fd
 *
 * This function will return whether this file has been opend.
183 184 185
 * 
 * @param pathname the file path name.
 *
B
bernard.xiong@gmail.com 已提交
186
 * @return 0 on file has been open successfully, -1 on open failed.
187 188 189 190 191 192 193 194 195 196 197 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
 */
int fd_is_open(const char* pathname)
{
	char *fullpath;
	unsigned int index;
	struct dfs_filesystem* fs;
	struct dfs_fd* fd;

	fullpath = dfs_normalize_path(RT_NULL, pathname);
	if (fullpath != RT_NULL)
	{
		char *mountpath;
		fs = dfs_filesystem_lookup(fullpath);
		if (fs == RT_NULL)
		{
			/* can't find mounted file system */
			rt_free(fullpath);
			return -1;
		}

		/* get file path name under mounted file system */
		if (fs->path[0] == '/' && fs->path[1] == '\0')
			mountpath = fullpath;
		else mountpath = fullpath + strlen(fs->path);

		dfs_lock();
		for (index = 0; index < DFS_FD_MAX; index++)
		{
			fd = &(fd_table[index]);
			if (fd->fs == RT_NULL) continue;

			if (fd->fs == fs &&
				strcmp(fd->path, mountpath) == 0)
			{
				/* found file in file descriptor table */
				rt_free(fullpath);
				dfs_unlock();
				return 0;
			}
		}
		dfs_unlock();

		rt_free(fullpath);
	}

	return -1;
}

/**
 * this function will return a sub-path name under directory.
 *
 * @param directory the parent directory.
 * @param filename the filename.
 *
 * @return the subdir pointer in filename
 */
const char* dfs_subdir(const char* directory, const char* filename)
{
	const char* dir;

B
bernard.xiong@gmail.com 已提交
247 248 249
	if (strlen(directory) == strlen(filename)) /* it's a same path */
		return RT_NULL;

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279
	dir = filename + strlen(directory);
	if ((*dir != '/') && (dir != filename))
	{
		dir --;
	}
	return dir;
}

/** 
 * this function will normalize a path according to specified parent directory and file name.
 *
 * @param directory the parent path
 * @param filename the file name
 *
 * @return the built full file path (absoluted path)
 */
char* dfs_normalize_path(const char* directory, const char* filename)
{
	char *fullpath;
	char *dst0, *dst, *src;

	/* check parameters */
	RT_ASSERT(filename != RT_NULL);

#ifdef DFS_USING_WORKDIR
	if (directory == NULL) /* shall use working directory */
		directory = &working_directory[0];
#else
	if ((directory == NULL) && (filename[0] != '/'))
	{
B
bernard.xiong 已提交
280
		rt_kprintf(NO_WORKING_DIR);
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299
		return RT_NULL;
	}
#endif

	if (filename[0] != '/') /* it's a absolute path, use it directly */
	{
		fullpath = rt_malloc(strlen(directory) + strlen(filename) + 2);

		/* join path and file name */
		rt_snprintf(fullpath, strlen(directory) + strlen(filename) + 2, 
			"%s/%s", directory, filename);
	}
	else
	{
		fullpath = rt_strdup(filename); /* copy string */
	}

	src = fullpath;
	dst = fullpath;
B
bernard.xiong 已提交
300 301
	
	dst0 = dst;
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
	while (1)
	{
		char c = *src;

		 if (c == '.')
		 {
			 if (!src[1]) src ++; /* '.' and ends */
			 else if (src[1] == '/')
			 {
				 /* './' case */
				 src += 2;

				 while ((*src == '/') && (*src != '\0')) src ++;
				 continue;
			 }
			 else if (src[1] == '.')
			 {
				 if (!src[2])
				 {
					/* '..' and ends case */
					 src += 2;
					 goto up_one;
				 }
				 else if (src[2] == '/')
				 {
					/* '../' case */
					 src += 3;

					 while ((*src == '/') && (*src != '\0')) src ++;
					 goto up_one;
				 }
			 }
		 }

		 /* copy up the next '/' and erase all '/' */
		 while ((c = *src++) != '\0' && c != '/') *dst ++ = c;

		 if (c == '/')
		 {
			 *dst ++ = '/';
			 while (c == '/') c = *src++;

			 src --;
		 }
		 else if (!c) break;

		 continue;

up_one:
		dst --;
		if (dst < dst0) { rt_free(fullpath); return NULL;}
		while (dst0 < dst && dst[-1] != '/') dst --;
	}

	*dst = '\0';
357 358 359 360 361

	/* remove '/' in the end of path if exist */
	dst --;
	if ((dst != fullpath) && (*dst == '/')) *dst = '\0';

362 363
	return fullpath;
}
B
bernard.xiong@gmail.com 已提交
364 365
/*@}*/