dfs_file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525
  1. /*
  2. * File : dfs_file.c
  3. * This file is part of Device File System in RT-Thread RTOS
  4. * COPYRIGHT (C) 2004-2010, RT-Thread Development Team
  5. *
  6. * The license and distribution terms for this file may be
  7. * found in the file LICENSE in this distribution or at
  8. * http://www.rt-thread.org/license/LICENSE.
  9. *
  10. * Change Logs:
  11. * Date Author Notes
  12. * 2005-02-22 Bernard The first version.
  13. */
  14. #include <dfs.h>
  15. #include <dfs_file.h>
  16. #define NO_WORKING_DIR "system does not support working dir\n"
  17. /**
  18. * this function will open a file which specified by path with specified flags.
  19. *
  20. * @param fd the file descriptor pointer to return the corresponding result.
  21. * @param path the spaciefied file path.
  22. * @param flags the flags for open operator.
  23. *
  24. * @return 0 on successful, -1 on failed.
  25. */
  26. int dfs_file_open(struct dfs_fd* fd, const char *path, int flags)
  27. {
  28. struct dfs_filesystem* fs;
  29. char *fullpath;
  30. int result;
  31. /* parameter check */
  32. if ( fd == RT_NULL ) return -DFS_STATUS_EINVAL;
  33. /* make sure we have an absolute path */
  34. fullpath = dfs_normalize_path(RT_NULL, path);
  35. if (fullpath == RT_NULL)
  36. {
  37. rt_kprintf(NO_WORKING_DIR);
  38. return -1;
  39. }
  40. dfs_log(DFS_DEBUG_INFO, ("open file:%s", fullpath));
  41. /* find filesystem */
  42. fs = dfs_filesystem_lookup(fullpath);
  43. if ( fs == RT_NULL )
  44. {
  45. rt_free(fullpath); /* release path */
  46. return -DFS_STATUS_ENOENT;
  47. }
  48. dfs_log(DFS_DEBUG_INFO, ("open in filesystem:%s", fs->ops->name));
  49. fd->fs = fs;
  50. /* initilize the fd item */
  51. fd->type = FT_REGULAR;
  52. fd->flags = flags;
  53. fd->size = 0;
  54. fd->pos = 0;
  55. if (dfs_subdir(fs->path, fullpath) == RT_NULL)
  56. fd->path = rt_strdup("/");
  57. else
  58. fd->path = rt_strdup(dfs_subdir(fs->path, fullpath));
  59. rt_free(fullpath);
  60. dfs_log(DFS_DEBUG_INFO, ("actul file path: %s\n", fd->path));
  61. /* specific file system open routine */
  62. if (fs->ops->open == RT_NULL)
  63. {
  64. /* clear fd */
  65. rt_free(fd->path);
  66. rt_memset(fd, 0, sizeof(*fd));
  67. return -DFS_STATUS_ENOSYS;
  68. }
  69. if ((result = fs->ops->open(fd)) < 0)
  70. {
  71. /* clear fd */
  72. rt_free(fd->path);
  73. rt_memset(fd, 0, sizeof(*fd));
  74. dfs_log(DFS_DEBUG_INFO, ("open failed"));
  75. return result;
  76. }
  77. fd->flags |= DFS_F_OPEN;
  78. if ( flags & DFS_O_DIRECTORY )
  79. {
  80. fd->type = FT_DIRECTORY;
  81. fd->flags |= DFS_F_DIRECTORY;
  82. }
  83. dfs_log(DFS_DEBUG_INFO, ("open successful"));
  84. return 0;
  85. }
  86. /**
  87. * this function will close a file descriptor.
  88. *
  89. * @param fd the file descriptor to be closed.
  90. *
  91. * @return 0 on successful, -1 on failed.
  92. */
  93. int dfs_file_close(struct dfs_fd* fd)
  94. {
  95. int result = 0;
  96. if (fd != RT_NULL && fd->fs->ops->close != RT_NULL) result = fd->fs->ops->close(fd);
  97. /* close fd error, return */
  98. if ( result < 0 ) return result;
  99. rt_free(fd->path);
  100. rt_memset(fd, 0, sizeof(struct dfs_fd));
  101. return result;
  102. }
  103. /**
  104. * this function will perform a io control on a file descriptor.
  105. *
  106. * @param fd the file descriptor.
  107. * @param cmd the command to send to file descriptor.
  108. * @param args the argument to send to file descriptor.
  109. *
  110. * @return 0 on successful, -1 on failed.
  111. */
  112. int dfs_file_ioctl(struct dfs_fd* fd, int cmd, void *args)
  113. {
  114. struct dfs_filesystem* fs;
  115. if (fd == RT_NULL || fd->type != FT_REGULAR) return -DFS_STATUS_EINVAL;
  116. fs = fd->fs;
  117. if (fs->ops->ioctl != RT_NULL) return fs->ops->ioctl(fd, cmd, args);
  118. return -DFS_STATUS_ENOSYS;
  119. }
  120. /**
  121. * this function will read specified length data from a file descriptor to a buffer.
  122. *
  123. * @param fd the file descriptor.
  124. * @param buf the buffer to save the read data.
  125. * @param len the length of data buffer to be read.
  126. *
  127. * @return the actual read data bytes or 0 on end of file or failed.
  128. */
  129. int dfs_file_read(struct dfs_fd* fd, void *buf, rt_size_t len)
  130. {
  131. struct dfs_filesystem* fs;
  132. int result = 0;
  133. if (fd == RT_NULL) return -DFS_STATUS_EINVAL;
  134. fs = (struct dfs_filesystem*) fd->fs;
  135. if (fs->ops->read == RT_NULL) return -DFS_STATUS_ENOSYS;
  136. if ( (result = fs->ops->read(fd, buf, len)) < 0 ) fd->flags |= DFS_F_EOF;
  137. return result;
  138. }
  139. /**
  140. * this function will fetch directory entries from a directory descriptor.
  141. *
  142. * @param fd the directory decriptor.
  143. * @param dirp the dirent buffer to save result.
  144. * @param nbytes the aviable room in the buffer.
  145. *
  146. * @return the read dirent, others on failed.
  147. */
  148. int dfs_file_getdents(struct dfs_fd* fd, struct dirent* dirp, rt_size_t nbytes)
  149. {
  150. struct dfs_filesystem* fs;
  151. /* parameter check */
  152. if (fd == RT_NULL || fd->type != FT_DIRECTORY) return -DFS_STATUS_EINVAL;
  153. fs = (struct dfs_filesystem*) fd->fs;
  154. if (fs->ops->getdents != RT_NULL) return fs->ops->getdents(fd, dirp, nbytes);
  155. return -DFS_STATUS_ENOSYS;
  156. }
  157. /**
  158. * this function will unlink (remove) a specified path file from file system.
  159. *
  160. * @param path the specified path file to be unlinked.
  161. *
  162. * @return 0 on successful, -1 on failed.
  163. */
  164. int dfs_file_unlink(const char *path)
  165. {
  166. int result;
  167. char *fullpath;
  168. struct dfs_filesystem* fs;
  169. result = DFS_STATUS_OK;
  170. /* Make sure we have an absolute path */
  171. fullpath = dfs_normalize_path(RT_NULL, path);
  172. if ( fullpath == RT_NULL)
  173. {
  174. rt_kprintf(NO_WORKING_DIR);
  175. return -DFS_STATUS_EINVAL;
  176. }
  177. /* get filesystem */
  178. if ( (fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
  179. {
  180. result = -DFS_STATUS_ENOENT;
  181. goto __exit;
  182. }
  183. /* Check whether file is already open */
  184. if (fd_is_open(fullpath) == 0)
  185. {
  186. result = -DFS_STATUS_EBUSY;
  187. goto __exit;
  188. }
  189. if (fs->ops->unlink != RT_NULL)
  190. {
  191. if (dfs_subdir(fs->path, fullpath) == RT_NULL)
  192. result = fs->ops->unlink(fs, "/");
  193. else
  194. result = fs->ops->unlink(fs, dfs_subdir(fs->path, fullpath));
  195. }
  196. else result = -DFS_STATUS_ENOSYS;
  197. __exit:
  198. rt_free(fullpath);
  199. return result;
  200. }
  201. /**
  202. * this function will write some specified length data to file system.
  203. *
  204. * @param fd the file descriptor.
  205. * @param buf the data buffer to be written.
  206. * @param len the data buffer length
  207. *
  208. * @return the actual written data length.
  209. */
  210. int dfs_file_write(struct dfs_fd* fd, const void *buf, rt_size_t len)
  211. {
  212. struct dfs_filesystem* fs;
  213. if (fd == RT_NULL) return -DFS_STATUS_EINVAL;
  214. fs = fd->fs;
  215. if (fs->ops->write == RT_NULL) return -DFS_STATUS_ENOSYS;
  216. return fs->ops->write(fd, buf, len);
  217. }
  218. /**
  219. * this function will flush buffer on a file descriptor.
  220. *
  221. * @param fd the file descriptor.
  222. *
  223. * @return 0 on successful, -1 on failed.
  224. */
  225. int dfs_file_flush(struct dfs_fd* fd)
  226. {
  227. struct dfs_filesystem* fs;
  228. if (fd == RT_NULL) return -DFS_STATUS_EINVAL;
  229. fs = fd->fs;
  230. if (fs->ops->flush == RT_NULL) return -DFS_STATUS_ENOSYS;
  231. return fs->ops->flush(fd);
  232. }
  233. /**
  234. * this function will seek the offset for specified file descriptor.
  235. *
  236. * @param fd the file descriptor.
  237. * @param offset the offset to be seeked.
  238. *
  239. * @return the current position after seek.
  240. */
  241. int dfs_file_lseek(struct dfs_fd* fd, rt_off_t offset)
  242. {
  243. struct dfs_filesystem* fs = fd->fs;
  244. if (fd == RT_NULL) return -DFS_STATUS_EINVAL;
  245. if (fs->ops->lseek == RT_NULL) return -DFS_STATUS_ENOSYS;
  246. return fs->ops->lseek(fd, offset);
  247. }
  248. /**
  249. * this function will get file information.
  250. *
  251. * @param path the file path.
  252. * @param buf the data buffer to save stat description.
  253. *
  254. * @return 0 on successful, -1 on failed.
  255. */
  256. int dfs_file_stat(const char *path, struct stat *buf)
  257. {
  258. int result;
  259. char* fullpath;
  260. struct dfs_filesystem* fs;
  261. fullpath = dfs_normalize_path(RT_NULL, path);
  262. if ( fullpath == RT_NULL )
  263. {
  264. rt_kprintf(NO_WORKING_DIR);
  265. return -1;
  266. }
  267. if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
  268. {
  269. dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath));
  270. rt_free(fullpath);
  271. return -DFS_STATUS_ENOENT;
  272. }
  273. if (fullpath[0] == '/' && fullpath[1] == '\0')
  274. {
  275. /* it's the root directory */
  276. buf->st_dev = 0;
  277. buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  278. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  279. buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  280. buf->st_size = 0;
  281. buf->st_mtime = 0;
  282. buf->st_blksize = 512;
  283. /* release full path */
  284. rt_free(fullpath);
  285. return DFS_STATUS_OK;
  286. }
  287. /* get the real file path */
  288. if (fs->ops->stat == RT_NULL)
  289. {
  290. rt_free(fullpath);
  291. dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function"));
  292. return -DFS_STATUS_ENOSYS;
  293. }
  294. if (dfs_subdir(fs->path, fullpath) == RT_NULL)
  295. result = fs->ops->stat(fs, "/", buf);
  296. else
  297. result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
  298. rt_free(fullpath);
  299. return result;
  300. }
  301. /**
  302. * this funciton will rename an old path name to a new path name.
  303. *
  304. * @param oldpath the old path name.
  305. * @param newpath the new path name.
  306. *
  307. * @return 0 on successful, -1 on failed.
  308. */
  309. int dfs_file_rename(const char* oldpath, const char* newpath)
  310. {
  311. int result;
  312. struct dfs_filesystem *oldfs, *newfs;
  313. char *oldfullpath, *newfullpath;
  314. result = DFS_STATUS_OK;
  315. oldfullpath = dfs_normalize_path(RT_NULL, oldpath);
  316. if ( oldfullpath == RT_NULL )
  317. {
  318. rt_kprintf(NO_WORKING_DIR);
  319. result = -DFS_STATUS_ENOENT;
  320. goto __exit;
  321. }
  322. newfullpath = dfs_normalize_path(RT_NULL, newpath);
  323. if ( newfullpath == RT_NULL )
  324. {
  325. rt_kprintf(NO_WORKING_DIR);
  326. result = -DFS_STATUS_ENOENT;
  327. goto __exit;
  328. }
  329. if ( (oldfs = dfs_filesystem_lookup(oldfullpath)) == RT_NULL )
  330. {
  331. result = -DFS_STATUS_ENOENT;
  332. goto __exit;
  333. }
  334. if ( (newfs = dfs_filesystem_lookup(newfullpath)) == RT_NULL )
  335. {
  336. result = -DFS_STATUS_ENOENT;
  337. goto __exit;
  338. }
  339. if ( oldfs == newfs )
  340. {
  341. if ( oldfs->ops->rename == RT_NULL )
  342. {
  343. result = -DFS_STATUS_ENOSYS;
  344. goto __exit;
  345. }
  346. result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
  347. goto __exit;
  348. }
  349. result = -DFS_STATUS_EXDEV;
  350. __exit:
  351. rt_free(oldfullpath);
  352. rt_free(newfullpath);
  353. /* not at same file system, return EXDEV */
  354. return result;
  355. }
  356. #ifdef RT_USING_FINSH
  357. #include <finsh.h>
  358. static struct dfs_fd fd;
  359. static struct dirent dirent;
  360. void ls(const char* pathname)
  361. {
  362. struct stat stat;
  363. int length;
  364. char* fullpath;
  365. fullpath = rt_malloc(DFS_PATH_MAX + 1);
  366. if (fullpath == RT_NULL) return; /* out of memory */
  367. /* list directory */
  368. if ( dfs_file_open(&fd, pathname, DFS_O_DIRECTORY) == 0 )
  369. {
  370. rt_kprintf("Directory %s:\n", pathname);
  371. do
  372. {
  373. rt_memset(&dirent, 0, sizeof(struct dirent));
  374. length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
  375. if ( length > 0 )
  376. {
  377. rt_memset(&stat, 0, sizeof(struct stat));
  378. /* build full path for each file */
  379. if (pathname[strlen(pathname) - 1] != '/')
  380. rt_snprintf(fullpath, DFS_PATH_MAX + 1, "%s%c%s", pathname, '/', dirent.d_name);
  381. else
  382. rt_snprintf(fullpath, DFS_PATH_MAX + 1, "%s%s", pathname, dirent.d_name);
  383. if (dfs_file_stat(fullpath, &stat) == 0)
  384. {
  385. if ( stat.st_mode & DFS_S_IFDIR )
  386. {
  387. rt_kprintf("%s\t\t<DIR>\n", dirent.d_name);
  388. }
  389. else
  390. {
  391. rt_kprintf("%s\t\t%lu\n", dirent.d_name, stat.st_size);
  392. }
  393. }
  394. else
  395. rt_kprintf("BAD file: %s\n", dirent.d_name);
  396. }
  397. }while(length > 0);
  398. dfs_file_close(&fd);
  399. }
  400. else
  401. {
  402. rt_kprintf("No such directory\n");
  403. }
  404. rt_free(fullpath);
  405. }
  406. FINSH_FUNCTION_EXPORT(ls, list directory contents)
  407. void rm(const char* filename)
  408. {
  409. if (dfs_file_unlink(filename) < 0)
  410. {
  411. rt_kprintf("Delete %s failed\n", filename);
  412. }
  413. }
  414. FINSH_FUNCTION_EXPORT(rm, remove files or directories)
  415. void cat(const char* filename)
  416. {
  417. rt_uint32_t length;
  418. char buffer[81];
  419. if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0)
  420. {
  421. rt_kprintf("Open %s failed\n", filename);
  422. return;
  423. }
  424. do
  425. {
  426. rt_memset(buffer, 0, sizeof(buffer));
  427. length = dfs_file_read(&fd, buffer, 81);
  428. if (length > 0)
  429. {
  430. rt_kprintf("%s", buffer);
  431. }
  432. }while (length > 0);
  433. dfs_file_close(&fd);
  434. }
  435. FINSH_FUNCTION_EXPORT(cat, print file)
  436. #endif