dfs_file.c 12.3 KB
Newer Older
1 2 3
/*
 * File      : dfs_file.c
 * This file is part of Device File System in RT-Thread RTOS
4
 * COPYRIGHT (C) 2004-2011, RT-Thread Development Team
5 6 7 8 9 10 11 12 13
 *
 * 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.
 */
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
#include <dfs.h>
#include <dfs_file.h>

/**
 * @addtogroup FileApi
 */
/*@{*/

/**
 * this function will open a file which specified by path with specified flags.
 *
 * @param fd the file descriptor pointer to return the corresponding result.
 * @param path the spaciefied file path.
 * @param flags the flags for open operator.
 *
 * @return 0 on successful, -1 on failed.
 */
32
int dfs_file_open(struct dfs_fd *fd, const char *path, int flags)
33
{
34
	struct dfs_filesystem *fs;
35 36 37 38
	char *fullpath;
	int result;

	/* parameter check */
39 40
	if (fd == RT_NULL)
		return -DFS_STATUS_EINVAL;
41 42 43 44 45 46 47 48 49 50 51 52

	/* make sure we have an absolute path */
	fullpath = dfs_normalize_path(RT_NULL, path);
	if (fullpath == RT_NULL)
	{
		return -1;
	}

	dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath));

	/* find filesystem */
	fs = dfs_filesystem_lookup(fullpath);
53
	if (fs == RT_NULL)
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
	{
		rt_free(fullpath); /* release path */
		return -DFS_STATUS_ENOENT;
	}

	dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name));
	fd->fs = fs;

	/* initilize the fd item */
	fd->type = FT_REGULAR;
	fd->flags = flags;
	fd->size = 0;
	fd->pos = 0;

	if (dfs_subdir(fs->path, fullpath) == RT_NULL)
		fd->path = rt_strdup("/");
	else
		fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
	rt_free(fullpath);
	dfs_log(DFS_DEBUG_INFO, ("actul file path: %s\n", fd->path));

	/* specific file system open routine */
	if (fs->ops->open == RT_NULL)
	{
		/* clear fd */
		rt_free(fd->path);
		rt_memset(fd, 0, sizeof(*fd));

		return -DFS_STATUS_ENOSYS;
	}

	if ((result = fs->ops->open(fd)) < 0)
	{
		/* clear fd */
		rt_free(fd->path);
		rt_memset(fd, 0, sizeof(*fd));

		dfs_log(DFS_DEBUG_INFO, ("open failed"));

		return result;
	}

	fd->flags |= DFS_F_OPEN;
97
	if (flags & DFS_O_DIRECTORY)
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
	{
		fd->type = FT_DIRECTORY;
		fd->flags |= DFS_F_DIRECTORY;
	}

	dfs_log(DFS_DEBUG_INFO, ("open successful"));
	return 0;
}

/**
 * this function will close a file descriptor.
 *
 * @param fd the file descriptor to be closed.
 *
 * @return 0 on successful, -1 on failed.
 */
114
int dfs_file_close(struct dfs_fd *fd)
115 116 117
{
	int result = 0;

118 119
	if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) 
		result = fd->fs->ops->close(fd);
120 121

	/* close fd error, return */
122 123
	if (result < 0) 
		return result;
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

	rt_free(fd->path);
	rt_memset(fd, 0, sizeof(struct dfs_fd));

	return result;
}

/**
 * this function will perform a io control on a file descriptor.
 *
 * @param fd the file descriptor.
 * @param cmd the command to send to file descriptor.
 * @param args the argument to send to file descriptor.
 *
 * @return 0 on successful, -1 on failed.
 */
140
int dfs_file_ioctl(struct dfs_fd *fd, int cmd, void *args)
141
{
142
	struct dfs_filesystem *fs;
143

144 145
	if (fd == RT_NULL || fd->type != FT_REGULAR)
		return -DFS_STATUS_EINVAL;
146 147

	fs = fd->fs;
148 149
	if (fs->ops->ioctl != RT_NULL) 
		return fs->ops->ioctl(fd, cmd, args);
150 151 152 153 154 155 156 157 158 159 160 161 162

	return -DFS_STATUS_ENOSYS;
}

/**
 * this function will read specified length data from a file descriptor to a buffer.
 *
 * @param fd the file descriptor.
 * @param buf the buffer to save the read data.
 * @param len the length of data buffer to be read.
 *
 * @return the actual read data bytes or 0 on end of file or failed.
 */
163
int dfs_file_read(struct dfs_fd *fd, void *buf, rt_size_t len)
164
{
165
	struct dfs_filesystem *fs;
166 167
	int result = 0;

168 169
	if (fd == RT_NULL) 
		return -DFS_STATUS_EINVAL;
170

171 172 173
	fs = (struct dfs_filesystem *)fd->fs;
	if (fs->ops->read == RT_NULL) 
		return -DFS_STATUS_ENOSYS;
174

175 176
	if ((result = fs->ops->read(fd, buf, len)) < 0)
		fd->flags |= DFS_F_EOF;
177 178 179 180 181 182 183 184 185 186 187 188 189

	return result;
}

/**
 * this function will fetch directory entries from a directory descriptor.
 *
 * @param fd the directory decriptor.
 * @param dirp the dirent buffer to save result.
 * @param nbytes the aviable room in the buffer.
 *
 * @return the read dirent, others on failed.
 */
190
int dfs_file_getdents(struct dfs_fd *fd, struct dirent *dirp, rt_size_t nbytes)
191
{
192
	struct dfs_filesystem *fs;
193 194

	/* parameter check */
195 196
	if (fd == RT_NULL || fd->type != FT_DIRECTORY) 
		return -DFS_STATUS_EINVAL;
197

198 199 200
	fs = (struct dfs_filesystem *)fd->fs;
	if (fs->ops->getdents != RT_NULL)
		return fs->ops->getdents(fd, dirp, nbytes);
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215

	return -DFS_STATUS_ENOSYS;
}

/**
 * this function will unlink (remove) a specified path file from file system.
 *
 * @param path the specified path file to be unlinked.
 *
 * @return 0 on successful, -1 on failed.
 */
int dfs_file_unlink(const char *path)
{
	int result;
	char *fullpath;
216
	struct dfs_filesystem *fs;
217 218 219 220 221

	result = DFS_STATUS_OK;

	/* Make sure we have an absolute path */
	fullpath = dfs_normalize_path(RT_NULL, path);
222
	if (fullpath == RT_NULL)
223 224 225 226 227
	{
		return -DFS_STATUS_EINVAL;
	}

	/* get filesystem */
228
	if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
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 261 262 263
	{
		result = -DFS_STATUS_ENOENT;
		goto __exit;
	}

	/* Check whether file is already open */
	if (fd_is_open(fullpath) == 0)
	{
		result = -DFS_STATUS_EBUSY;
		goto __exit;
	}

	if (fs->ops->unlink != RT_NULL)
	{
		if (dfs_subdir(fs->path, fullpath) == RT_NULL)
			result = fs->ops->unlink(fs, "/");
		else
			result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
	}
	else result = -DFS_STATUS_ENOSYS;

__exit:
	rt_free(fullpath);
	return result;
}

/**
 * this function will write some specified length data to file system.
 *
 * @param fd the file descriptor.
 * @param buf the data buffer to be written.
 * @param len the data buffer length
 *
 * @return the actual written data length.
 */
264
int dfs_file_write(struct dfs_fd *fd, const void *buf, rt_size_t len)
265
{
266
	struct dfs_filesystem *fs;
267

268 269
	if (fd == RT_NULL)
		return -DFS_STATUS_EINVAL;
270 271

	fs = fd->fs;
272 273
	if (fs->ops->write == RT_NULL)
		return -DFS_STATUS_ENOSYS;
274 275 276 277 278 279 280 281 282 283 284

	return fs->ops->write(fd, buf, len);
}

/**
 * this function will flush buffer on a file descriptor.
 *
 * @param fd the file descriptor.
 *
 * @return 0 on successful, -1 on failed.
 */
285
int dfs_file_flush(struct dfs_fd *fd)
286
{
287
	struct dfs_filesystem *fs;
288

289 290
	if (fd == RT_NULL)
		return -DFS_STATUS_EINVAL;
291 292

	fs = fd->fs;
293 294
	if (fs->ops->flush == RT_NULL)
		return -DFS_STATUS_ENOSYS;
295 296 297 298 299 300 301 302 303 304 305 306

	return fs->ops->flush(fd);
}

/**
 * this function will seek the offset for specified file descriptor.
 *
 * @param fd the file descriptor.
 * @param offset the offset to be seeked.
 *
 * @return the current position after seek.
 */
307
int dfs_file_lseek(struct dfs_fd *fd, rt_off_t offset)
308 309
{
	int result;
310
	struct dfs_filesystem *fs = fd->fs;
311

312 313 314 315
	if (fd == RT_NULL)
		return -DFS_STATUS_EINVAL;
	if (fs->ops->lseek == RT_NULL)
		return -DFS_STATUS_ENOSYS;
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336

	result = fs->ops->lseek(fd, offset);

	/* update current position */
	if (result >= 0)
		fd->pos = result;

	return result;
}

/**
 * this function will get file information.
 *
 * @param path the file path.
 * @param buf the data buffer to save stat description.
 *
 * @return 0 on successful, -1 on failed.
 */
int dfs_file_stat(const char *path, struct stat *buf)
{
	int result;
337 338
	char *fullpath;
	struct dfs_filesystem *fs;
339 340

	fullpath = dfs_normalize_path(RT_NULL, path);
341
	if (fullpath == RT_NULL)
342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	{
		return -1;
	}

	if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
	{
		dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath));
		rt_free(fullpath);
		return -DFS_STATUS_ENOENT;
	}

	if ((fullpath[0] == '/' && fullpath[1] == '\0') ||
		(dfs_subdir(fs->path, fullpath) == RT_NULL))
	{
		/* it's the root directory */
		buf->st_dev   = 0;

359
		buf->st_mode  = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397
			DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
		buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;

		buf->st_size  = 0;
		buf->st_mtime = 0;
		buf->st_blksize = 512;

		/* release full path */
		rt_free(fullpath);

		return DFS_STATUS_OK;
	}
	else
	{
		if (fs->ops->stat == RT_NULL)
		{
			rt_free(fullpath);
			dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function"));
			return -DFS_STATUS_ENOSYS;
		}

		/* get the real file path and get file stat */
		result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
	}

	rt_free(fullpath);

	return result;
}

/**
 * this funciton will rename an old path name to a new path name.
 *
 * @param oldpath the old path name.
 * @param newpath the new path name.
 *
 * @return 0 on successful, -1 on failed.
 */
398
int dfs_file_rename(const char *oldpath, const char *newpath)
399 400 401 402 403 404 405 406 407
{
	int result;
	struct dfs_filesystem *oldfs, *newfs;
	char *oldfullpath, *newfullpath;

	result = DFS_STATUS_OK;
	newfullpath = RT_NULL;

	oldfullpath = dfs_normalize_path(RT_NULL, oldpath);
408
	if (oldfullpath == RT_NULL)
409 410 411 412 413 414
	{
		result = -DFS_STATUS_ENOENT;
		goto __exit;
	}

	newfullpath = dfs_normalize_path(RT_NULL, newpath);
415
	if (newfullpath == RT_NULL)
416 417 418 419 420
	{
		result = -DFS_STATUS_ENOENT;
		goto __exit;
	}

421
	if ((oldfs = dfs_filesystem_lookup(oldfullpath)) == RT_NULL)
422 423 424 425 426
	{
		result = -DFS_STATUS_ENOENT;
		goto __exit;
	}

427
	if ((newfs = dfs_filesystem_lookup(newfullpath)) == RT_NULL)
428 429 430 431 432
	{
		result = -DFS_STATUS_ENOENT;
		goto __exit;
	}

433
	if (oldfs == newfs)
434
	{
435
		if (oldfs->ops->rename == RT_NULL)
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459
		{
			result = -DFS_STATUS_ENOSYS;
			goto __exit;
		}

		result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
		goto __exit;
	}

	result = -DFS_STATUS_EXDEV;

__exit:
	rt_free(oldfullpath);
	rt_free(newfullpath);

	/* not at same file system, return EXDEV */
	return result;
}

#ifdef RT_USING_FINSH
#include <finsh.h>

static struct dfs_fd fd;
static struct dirent dirent;
460
void ls(const char *pathname)
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478
{
	struct stat stat;
	int length;
	char *fullpath, *path;

	fullpath = RT_NULL;
	if (pathname == RT_NULL)
	{
#ifdef DFS_USING_WORKDIR
		/* open current working directory */
		path = rt_strdup(working_directory);
#else
		path = rt_strdup("/");
#endif
		if (path == RT_NULL) return ; /* out of memory */
	}
	else
	{
479
		path = (char *)pathname;
480 481 482
	}

	/* list directory */
483
	if (dfs_file_open(&fd, path, DFS_O_DIRECTORY) == 0)
484 485 486 487 488 489
	{
		rt_kprintf("Directory %s:\n", path);
		do
		{
			rt_memset(&dirent, 0, sizeof(struct dirent));
			length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
490
			if (length > 0)
491 492 493 494 495
			{
				rt_memset(&stat, 0, sizeof(struct stat));

				/* build full path for each file */
				fullpath = dfs_normalize_path(path, dirent.d_name);
496 497
				if (fullpath == RT_NULL) 
					break;
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522

				if (dfs_file_stat(fullpath, &stat) == 0)
				{
					rt_kprintf("%-20s", dirent.d_name);
					if ( DFS_S_ISDIR(stat.st_mode))
					{
						rt_kprintf("%-25s\n", "<DIR>");
					}
					else
					{
						rt_kprintf("%-25lu\n", stat.st_size);
					}
				}
				else
					rt_kprintf("BAD file: %s\n", dirent.d_name);
				rt_free(fullpath);
			}
		}while(length > 0);

		dfs_file_close(&fd);
	}
	else
	{
		rt_kprintf("No such directory\n");
	}
523 524
	if (pathname == RT_NULL) 
		rt_free(path);
525 526 527
}
FINSH_FUNCTION_EXPORT(ls, list directory contents)

528
void rm(const char *filename)
529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562
{
	if (dfs_file_unlink(filename) < 0)
	{
		rt_kprintf("Delete %s failed\n", filename);
	}
}
FINSH_FUNCTION_EXPORT(rm, remove files or directories)

void cat(const char* filename)
{
	rt_uint32_t length;
	char buffer[81];

	if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0)
	{
		rt_kprintf("Open %s failed\n", filename);
		return;
	}

	do
	{
		rt_memset(buffer, 0, sizeof(buffer));
		length = dfs_file_read(&fd, buffer, sizeof(buffer)-1 );
		if (length > 0)
		{
			rt_kprintf("%s", buffer);
		}
	}while (length > 0);

	dfs_file_close(&fd);
}
FINSH_FUNCTION_EXPORT(cat, print file)

#define BUF_SZ	4096
563
void copy(const char *src, const char *dst)
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608
{
	struct dfs_fd src_fd;
	rt_uint8_t *block_ptr;
	rt_uint32_t read_bytes;

	block_ptr = rt_malloc(BUF_SZ);
	if (block_ptr == RT_NULL)
	{
		rt_kprintf("out of memory\n");
		return;
	}

	if (dfs_file_open(&src_fd, src, DFS_O_RDONLY) < 0)
	{
		rt_free(block_ptr);
		rt_kprintf("Read %s failed\n", src);
		return;
	}
	if (dfs_file_open(&fd, dst, DFS_O_WRONLY | DFS_O_CREAT) < 0)
	{
		rt_free(block_ptr);
		dfs_file_close(&src_fd);

		rt_kprintf("Write %s failed\n", dst);
		return;
	}

	do
	{
		read_bytes = dfs_file_read(&src_fd, block_ptr, BUF_SZ);
		if (read_bytes > 0)
		{
			dfs_file_write(&fd, block_ptr, read_bytes);
		}
	} while (read_bytes > 0);

	dfs_file_close(&src_fd);
	dfs_file_close(&fd);
	rt_free(block_ptr);
}
FINSH_FUNCTION_EXPORT(copy, copy source file to destination file)

#endif
/* @} */