dfs_file.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  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. int result;
  244. struct dfs_filesystem* fs = fd->fs;
  245. if (fd == RT_NULL) return -DFS_STATUS_EINVAL;
  246. if (fs->ops->lseek == RT_NULL) return -DFS_STATUS_ENOSYS;
  247. result = fs->ops->lseek(fd, offset);
  248. /* update current position */
  249. if (result >= 0)
  250. fd->pos = result;
  251. return result;
  252. }
  253. /**
  254. * this function will get file information.
  255. *
  256. * @param path the file path.
  257. * @param buf the data buffer to save stat description.
  258. *
  259. * @return 0 on successful, -1 on failed.
  260. */
  261. int dfs_file_stat(const char *path, struct stat *buf)
  262. {
  263. int result;
  264. char* fullpath;
  265. struct dfs_filesystem* fs;
  266. fullpath = dfs_normalize_path(RT_NULL, path);
  267. if ( fullpath == RT_NULL )
  268. {
  269. rt_kprintf(NO_WORKING_DIR);
  270. return -1;
  271. }
  272. if ((fs = dfs_filesystem_lookup(fullpath)) == RT_NULL)
  273. {
  274. dfs_log(DFS_DEBUG_ERROR, ("can't find mounted filesystem on this path:%s", fullpath));
  275. rt_free(fullpath);
  276. return -DFS_STATUS_ENOENT;
  277. }
  278. if (fullpath[0] == '/' && fullpath[1] == '\0')
  279. {
  280. /* it's the root directory */
  281. buf->st_dev = 0;
  282. buf->st_mode = DFS_S_IRUSR | DFS_S_IRGRP | DFS_S_IROTH |
  283. DFS_S_IWUSR | DFS_S_IWGRP | DFS_S_IWOTH;
  284. buf->st_mode |= DFS_S_IFDIR | DFS_S_IXUSR | DFS_S_IXGRP | DFS_S_IXOTH;
  285. buf->st_size = 0;
  286. buf->st_mtime = 0;
  287. buf->st_blksize = 512;
  288. /* release full path */
  289. rt_free(fullpath);
  290. return DFS_STATUS_OK;
  291. }
  292. /* get the real file path */
  293. if (fs->ops->stat == RT_NULL)
  294. {
  295. rt_free(fullpath);
  296. dfs_log(DFS_DEBUG_ERROR, ("the filesystem didn't implement this function"));
  297. return -DFS_STATUS_ENOSYS;
  298. }
  299. if (dfs_subdir(fs->path, fullpath) == RT_NULL)
  300. result = fs->ops->stat(fs, "/", buf);
  301. else
  302. result = fs->ops->stat(fs, dfs_subdir(fs->path, fullpath), buf);
  303. rt_free(fullpath);
  304. return result;
  305. }
  306. /**
  307. * this funciton will rename an old path name to a new path name.
  308. *
  309. * @param oldpath the old path name.
  310. * @param newpath the new path name.
  311. *
  312. * @return 0 on successful, -1 on failed.
  313. */
  314. int dfs_file_rename(const char* oldpath, const char* newpath)
  315. {
  316. int result;
  317. struct dfs_filesystem *oldfs, *newfs;
  318. char *oldfullpath, *newfullpath;
  319. result = DFS_STATUS_OK;
  320. oldfullpath = dfs_normalize_path(RT_NULL, oldpath);
  321. if ( oldfullpath == RT_NULL )
  322. {
  323. rt_kprintf(NO_WORKING_DIR);
  324. result = -DFS_STATUS_ENOENT;
  325. goto __exit;
  326. }
  327. newfullpath = dfs_normalize_path(RT_NULL, newpath);
  328. if ( newfullpath == RT_NULL )
  329. {
  330. rt_kprintf(NO_WORKING_DIR);
  331. result = -DFS_STATUS_ENOENT;
  332. goto __exit;
  333. }
  334. if ( (oldfs = dfs_filesystem_lookup(oldfullpath)) == RT_NULL )
  335. {
  336. result = -DFS_STATUS_ENOENT;
  337. goto __exit;
  338. }
  339. if ( (newfs = dfs_filesystem_lookup(newfullpath)) == RT_NULL )
  340. {
  341. result = -DFS_STATUS_ENOENT;
  342. goto __exit;
  343. }
  344. if ( oldfs == newfs )
  345. {
  346. if ( oldfs->ops->rename == RT_NULL )
  347. {
  348. result = -DFS_STATUS_ENOSYS;
  349. goto __exit;
  350. }
  351. result = oldfs->ops->rename(oldfs, oldfullpath, newfullpath);
  352. goto __exit;
  353. }
  354. result = -DFS_STATUS_EXDEV;
  355. __exit:
  356. rt_free(oldfullpath);
  357. rt_free(newfullpath);
  358. /* not at same file system, return EXDEV */
  359. return result;
  360. }
  361. #ifdef RT_USING_FINSH
  362. #include <finsh.h>
  363. static struct dfs_fd fd;
  364. static struct dirent dirent;
  365. void ls(const char* pathname)
  366. {
  367. struct stat stat;
  368. int length;
  369. char* fullpath;
  370. fullpath = rt_malloc(DFS_PATH_MAX + 1);
  371. if (fullpath == RT_NULL) return; /* out of memory */
  372. /* list directory */
  373. if ( dfs_file_open(&fd, pathname, DFS_O_DIRECTORY) == 0 )
  374. {
  375. rt_kprintf("Directory %s:\n", pathname);
  376. do
  377. {
  378. rt_memset(&dirent, 0, sizeof(struct dirent));
  379. length = dfs_file_getdents(&fd, &dirent, sizeof(struct dirent));
  380. if ( length > 0 )
  381. {
  382. rt_memset(&stat, 0, sizeof(struct stat));
  383. /* build full path for each file */
  384. if (pathname[strlen(pathname) - 1] != '/')
  385. rt_snprintf(fullpath, DFS_PATH_MAX + 1, "%s%c%s", pathname, '/', dirent.d_name);
  386. else
  387. rt_snprintf(fullpath, DFS_PATH_MAX + 1, "%s%s", pathname, dirent.d_name);
  388. if (dfs_file_stat(fullpath, &stat) == 0)
  389. {
  390. if ( DFS_S_ISDIR(stat.st_mode))
  391. {
  392. rt_kprintf("%s\t\t<DIR>\n", dirent.d_name);
  393. }
  394. else
  395. {
  396. rt_kprintf("%s\t\t%lu\n", dirent.d_name, stat.st_size);
  397. }
  398. }
  399. else
  400. rt_kprintf("BAD file: %s\n", dirent.d_name);
  401. }
  402. }while(length > 0);
  403. dfs_file_close(&fd);
  404. }
  405. else
  406. {
  407. rt_kprintf("No such directory\n");
  408. }
  409. rt_free(fullpath);
  410. }
  411. FINSH_FUNCTION_EXPORT(ls, list directory contents)
  412. void rm(const char* filename)
  413. {
  414. if (dfs_file_unlink(filename) < 0)
  415. {
  416. rt_kprintf("Delete %s failed\n", filename);
  417. }
  418. }
  419. FINSH_FUNCTION_EXPORT(rm, remove files or directories)
  420. void cat(const char* filename)
  421. {
  422. rt_uint32_t length;
  423. char buffer[81];
  424. if (dfs_file_open(&fd, filename, DFS_O_RDONLY) < 0)
  425. {
  426. rt_kprintf("Open %s failed\n", filename);
  427. return;
  428. }
  429. do
  430. {
  431. rt_memset(buffer, 0, sizeof(buffer));
  432. length = dfs_file_read(&fd, buffer, 81);
  433. if (length > 0)
  434. {
  435. rt_kprintf("%s", buffer);
  436. }
  437. }while (length > 0);
  438. dfs_file_close(&fd);
  439. }
  440. FINSH_FUNCTION_EXPORT(cat, print file)
  441. #endif