dfs_file.c 59 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2023-05-05 Bernard Implement file APIs in dfs v2.0
  9. */
  10. #include "errno.h"
  11. #include "fcntl.h"
  12. #include <dfs.h>
  13. #include "dfs_file.h"
  14. #include "dfs_dentry.h"
  15. #include "dfs_fs.h"
  16. #include "dfs_mnt.h"
  17. #include "dfs_private.h"
  18. #ifdef RT_USING_PAGECACHE
  19. #include "dfs_pcache.h"
  20. #endif
  21. #define DBG_TAG "DFS.file"
  22. #define DBG_LVL DBG_WARNING
  23. #include <rtdbg.h>
  24. #define MAX_RW_COUNT 0xfffc0000
  25. rt_inline int _find_path_node(const char *path)
  26. {
  27. int i = 0;
  28. while (path[i] != '\0')
  29. {
  30. if ('/' == path[i++])
  31. {
  32. break;
  33. }
  34. }
  35. /* return path-note length */
  36. return i;
  37. }
  38. /*
  39. * rw_verify_area doesn't like huge counts. We limit
  40. * them to something that fits in "int" so that others
  41. * won't have to do range checks all the time.
  42. */
  43. ssize_t rw_verify_area(struct dfs_file *file, off_t *ppos, size_t count)
  44. {
  45. off_t pos;
  46. ssize_t retval = -EINVAL;
  47. if ((size_t)count < 0)
  48. return retval;
  49. pos = *ppos;
  50. if (pos < 0)
  51. {
  52. if (count >= -pos) /* both values are in 0..LLONG_MAX */
  53. return -EOVERFLOW;
  54. }
  55. return count > MAX_RW_COUNT ? MAX_RW_COUNT : count;
  56. }
  57. off_t dfs_file_get_fpos(struct dfs_file *file)
  58. {
  59. if (file)
  60. {
  61. if (file->vnode->type == FT_REGULAR)
  62. {
  63. rt_mutex_take(&file->pos_lock, RT_WAITING_FOREVER);
  64. }
  65. return file->fpos;
  66. }
  67. return 0;
  68. }
  69. void dfs_file_set_fpos(struct dfs_file *file, off_t fpos)
  70. {
  71. if (file)
  72. {
  73. if (file->vnode->type != FT_REGULAR)
  74. {
  75. rt_mutex_take(&file->pos_lock, RT_WAITING_FOREVER);
  76. }
  77. file->fpos = fpos;
  78. rt_mutex_release(&file->pos_lock);
  79. }
  80. }
  81. void dfs_file_init(struct dfs_file *file)
  82. {
  83. if (file)
  84. {
  85. rt_memset(file, 0x00, sizeof(struct dfs_file));
  86. file->magic = DFS_FD_MAGIC;
  87. rt_mutex_init(&file->pos_lock, "fpos", RT_IPC_FLAG_PRIO);
  88. rt_atomic_store(&(file->ref_count), 1);
  89. }
  90. }
  91. void dfs_file_deinit(struct dfs_file *file)
  92. {
  93. if (file)
  94. {
  95. rt_mutex_detach(&file->pos_lock);
  96. }
  97. }
  98. static void dfs_file_unref(struct dfs_file *file)
  99. {
  100. rt_err_t ret = RT_EOK;
  101. ret = dfs_file_lock();
  102. if (ret == RT_EOK)
  103. {
  104. if (rt_atomic_load(&(file->ref_count)) == 1)
  105. {
  106. /* should release this file */
  107. if (file->dentry)
  108. {
  109. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry(%s))", file->dentry->pathname);
  110. dfs_dentry_unref(file->dentry);
  111. file->dentry = RT_NULL;
  112. }
  113. else if (file->vnode)
  114. {
  115. if (file->vnode->ref_count > 1)
  116. {
  117. rt_atomic_sub(&(file->vnode->ref_count), 1);
  118. }
  119. else if (file->vnode->ref_count == 1)
  120. {
  121. rt_free(file->vnode);
  122. file->vnode = RT_NULL;
  123. }
  124. }
  125. LOG_I("release a file: %p", file);
  126. }
  127. dfs_file_unlock();
  128. }
  129. }
  130. struct dfs_dentry* dfs_file_follow_link(struct dfs_dentry *dentry)
  131. {
  132. int ret = 0;
  133. struct dfs_dentry *tmp = dfs_dentry_ref(dentry);
  134. if (dentry && dentry->vnode && dentry->vnode->type == FT_SYMLINK)
  135. {
  136. char *buf = NULL;
  137. buf = (char *)rt_malloc(DFS_PATH_MAX);
  138. if (buf)
  139. {
  140. do
  141. {
  142. if (dfs_is_mounted(tmp->mnt) == 0)
  143. {
  144. ret = tmp->mnt->fs_ops->readlink(tmp, buf, DFS_PATH_MAX);
  145. }
  146. if (ret > 0)
  147. {
  148. struct dfs_mnt *mnt = NULL;
  149. if (buf[0] != '/')
  150. {
  151. char *dir = dfs_dentry_pathname(tmp);
  152. /* is the relative directory */
  153. if (dir)
  154. {
  155. char *fullpath = dfs_normalize_path(dir, buf);
  156. if (fullpath)
  157. {
  158. strncpy(buf, fullpath, DFS_PATH_MAX);
  159. rt_free(fullpath);
  160. }
  161. rt_free(dir);
  162. }
  163. }
  164. mnt = dfs_mnt_lookup(buf);
  165. if (mnt)
  166. {
  167. struct dfs_dentry *de = dfs_dentry_lookup(mnt, buf, 0);
  168. /* release the old dentry */
  169. dfs_dentry_unref(tmp);
  170. tmp = de;
  171. }
  172. }
  173. else
  174. {
  175. break;
  176. }
  177. } while (tmp && tmp->vnode->type == FT_SYMLINK);
  178. }
  179. rt_free(buf);
  180. }
  181. return tmp;
  182. }
  183. /*
  184. * this function is creat a nolink path.
  185. *
  186. * @param mnt
  187. * @param fullpath
  188. * @param mode 0 middle path nolink; 1 all path nolink.
  189. *
  190. * @return new path.
  191. */
  192. char *dfs_nolink_path(struct dfs_mnt **mnt, char *fullpath, int mode)
  193. {
  194. int index = 0;
  195. char *path = RT_NULL;
  196. char *link_fn;
  197. struct dfs_dentry *dentry = RT_NULL;
  198. path = (char *)rt_malloc((DFS_PATH_MAX * 2) + 2); // path + \0 + link_fn + \0
  199. if (!path)
  200. {
  201. return path;
  202. }
  203. link_fn = path + DFS_PATH_MAX + 1;
  204. if (*mnt && fullpath)
  205. {
  206. int i = 0;
  207. char *fp = fullpath;
  208. while ((i = _find_path_node(fp)) > 0)
  209. {
  210. if (i + index > DFS_PATH_MAX)
  211. {
  212. goto _ERR_RET;
  213. }
  214. rt_memcpy(path + index, fp, i);
  215. path[index + i] = '\0';
  216. /* the last should by mode process. */
  217. if ((fp[i] == '\0') && (!mode))
  218. {
  219. break;
  220. }
  221. fp += i;
  222. dentry = dfs_dentry_lookup(*mnt, path, 0);
  223. if (dentry && dentry->vnode->type == FT_SYMLINK)
  224. {
  225. int ret = -1;
  226. if ((*mnt)->fs_ops->readlink)
  227. {
  228. if (dfs_is_mounted((*mnt)) == 0)
  229. {
  230. ret = (*mnt)->fs_ops->readlink(dentry, link_fn, DFS_PATH_MAX);
  231. }
  232. }
  233. if (ret > 0)
  234. {
  235. int len = rt_strlen(link_fn);
  236. if (link_fn[0] != '/')
  237. {
  238. path[index] = '/';
  239. }
  240. else
  241. {
  242. index = 0;
  243. }
  244. if (len + index + 1 >= DFS_PATH_MAX)
  245. {
  246. goto _ERR_RET;
  247. }
  248. rt_memcpy(path + index, link_fn, len);
  249. index += len;
  250. path[index] = '\0';
  251. *mnt = dfs_mnt_lookup(path);
  252. }
  253. else
  254. {
  255. goto _ERR_RET;
  256. }
  257. }
  258. else
  259. {
  260. index += i;
  261. }
  262. dfs_dentry_unref(dentry);
  263. }
  264. }
  265. else
  266. {
  267. _ERR_RET:
  268. rt_free(path);
  269. path = RT_NULL;
  270. }
  271. return path;
  272. }
  273. /**
  274. * this function will open a file which specified by path with specified oflags.
  275. *
  276. * @param fd the file descriptor pointer to return the corresponding result.
  277. * @param path the specified file path.
  278. * @param oflags the oflags for open operator.
  279. *
  280. * @return 0 on successful, -1 on failed.
  281. */
  282. int dfs_file_open(struct dfs_file *file, const char *path, int oflags, mode_t mode)
  283. {
  284. int ret = -RT_ERROR;
  285. char *fullpath = RT_NULL;
  286. struct dfs_dentry *dentry = RT_NULL;
  287. int fflags = dfs_fflags(oflags);
  288. if (mode == 0)
  289. {
  290. mode = (S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH); /* 0666 */
  291. }
  292. if (file && path)
  293. {
  294. fullpath = dfs_normalize_path(NULL, path);
  295. if (fullpath)
  296. {
  297. struct dfs_mnt *mnt = RT_NULL;
  298. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
  299. mnt = dfs_mnt_lookup(fullpath);
  300. if (mnt)
  301. {
  302. char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
  303. if (tmp)
  304. {
  305. rt_free(fullpath);
  306. fullpath = tmp;
  307. }
  308. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
  309. dentry = dfs_dentry_lookup(mnt, fullpath, oflags);
  310. if (dentry && dentry->vnode->type == FT_SYMLINK)
  311. {
  312. /* it's a symbol link but not follow */
  313. if (oflags & O_NOFOLLOW)
  314. {
  315. /* no follow symbol link */
  316. dfs_dentry_unref(dentry);
  317. dentry = RT_NULL;
  318. }
  319. else
  320. {
  321. struct dfs_dentry *target_dentry = RT_NULL;
  322. /* follow symbol link */
  323. target_dentry = dfs_file_follow_link(dentry);
  324. dfs_dentry_unref(dentry);
  325. dentry = target_dentry;
  326. }
  327. }
  328. if (dentry)
  329. {
  330. if (oflags & O_DIRECTORY)
  331. {
  332. if (dentry->vnode->type != FT_DIRECTORY)
  333. {
  334. dfs_dentry_unref(dentry);
  335. dentry = RT_NULL;
  336. }
  337. }
  338. else if (dentry->vnode->type == FT_DIRECTORY)
  339. {
  340. if (fflags & (DFS_F_FWRITE))
  341. {
  342. dfs_dentry_unref(dentry);
  343. dentry = RT_NULL;
  344. }
  345. else
  346. {
  347. oflags |= O_DIRECTORY;
  348. }
  349. }
  350. }
  351. if (oflags & O_CREAT)
  352. {
  353. if (dentry)
  354. {
  355. oflags &= ~O_CREAT;
  356. if (oflags & O_EXCL)
  357. {
  358. oflags &= ~O_EXCL;
  359. /* the dentry already exists */
  360. dfs_dentry_unref(dentry);
  361. ret = -EEXIST;
  362. goto _ERR_RET;
  363. }
  364. }
  365. else
  366. {
  367. /* create file/directory */
  368. if (mnt->fs_ops->create_vnode)
  369. {
  370. struct dfs_vnode *vnode = RT_NULL;
  371. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", fullpath);
  372. dfs_file_lock();
  373. dentry = dfs_dentry_create(mnt, fullpath);
  374. if (dentry)
  375. {
  376. DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->create_vnode");
  377. if (dfs_is_mounted(mnt) == 0)
  378. {
  379. vnode = mnt->fs_ops->create_vnode(dentry, oflags & O_DIRECTORY ? FT_DIRECTORY:FT_REGULAR, mode);
  380. }
  381. if (vnode)
  382. {
  383. /* set vnode */
  384. dentry->vnode = vnode; /* the refcount of created vnode is 1. no need to reference */
  385. dfs_dentry_insert(dentry);
  386. }
  387. else
  388. {
  389. DLOG(msg, mnt->fs_ops->name, "dfs_file", DLOG_MSG_RET, "create failed.");
  390. dfs_dentry_unref(dentry);
  391. dentry = RT_NULL;
  392. }
  393. }
  394. dfs_file_unlock();
  395. }
  396. }
  397. }
  398. if (dentry)
  399. {
  400. rt_bool_t permission = RT_TRUE;
  401. file->dentry = dentry;
  402. file->vnode = dentry->vnode;
  403. file->fops = dentry->mnt->fs_ops->default_fops;
  404. file->flags = oflags;
  405. /* check permission */
  406. if (!(oflags & O_CREAT))
  407. {
  408. if (fflags & DFS_F_FWRITE)
  409. {
  410. if (!(file->vnode->mode & S_IWUSR))
  411. {
  412. permission = RT_FALSE;
  413. }
  414. }
  415. if (fflags & DFS_F_FREAD)
  416. {
  417. if (!(file->vnode->mode & S_IRUSR))
  418. {
  419. permission = RT_FALSE;
  420. }
  421. }
  422. if (oflags & O_EXEC)
  423. {
  424. if (!(file->vnode->mode & S_IXUSR))
  425. {
  426. permission = RT_FALSE;
  427. }
  428. }
  429. }
  430. if (permission && file->fops->open)
  431. {
  432. DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fops->open(file)");
  433. if (dfs_is_mounted(file->vnode->mnt) == 0)
  434. {
  435. dfs_file_lock();
  436. ret = file->fops->open(file);
  437. dfs_file_unlock();
  438. }
  439. else
  440. {
  441. ret = -EINVAL;
  442. }
  443. if (ret < 0)
  444. {
  445. LOG_E("open %s failed in file system: %s", path, dentry->mnt->fs_ops->name);
  446. DLOG(msg, mnt->fs_ops->name, "dfs_file", DLOG_MSG_RET, "open failed.");
  447. dfs_file_unref(file);
  448. }
  449. else
  450. {
  451. /* for char/block device */
  452. if ((S_ISCHR(file->vnode->mode)) || (S_ISBLK(file->vnode->mode)))
  453. {
  454. file->fops = file->vnode->fops;
  455. }
  456. }
  457. }
  458. else
  459. {
  460. DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "no permission or fops->open");
  461. dfs_file_unref(file);
  462. ret = -EPERM;
  463. }
  464. }
  465. else
  466. {
  467. LOG_I("lookup file:%s failed in file system", path);
  468. ret = -ENOENT;
  469. }
  470. }
  471. }
  472. if (ret >= 0 && (oflags & O_TRUNC))
  473. {
  474. /* trunc file */
  475. if (!(fflags & DFS_F_FWRITE) || file->vnode->type == FT_DIRECTORY)
  476. {
  477. /* truncate on read a only file or a directory */
  478. DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file), trunc on RDOnly or directory");
  479. ret = -RT_ERROR;
  480. }
  481. else
  482. {
  483. if (file->fops->truncate)
  484. {
  485. DLOG(msg, "dfs_file", dentry->mnt->fs_ops->name, DLOG_MSG, "fops->truncate(file, 0)");
  486. if (dfs_is_mounted(file->vnode->mnt) == 0)
  487. {
  488. #ifdef RT_USING_PAGECACHE
  489. if (file->vnode->aspace)
  490. {
  491. dfs_aspace_clean(file->vnode->aspace);
  492. }
  493. #endif
  494. ret = file->fops->truncate(file, 0);
  495. }
  496. else
  497. {
  498. ret = -EINVAL;
  499. }
  500. }
  501. }
  502. if (ret < 0)
  503. {
  504. dfs_file_unref(file);
  505. }
  506. file->flags &= ~O_TRUNC;
  507. }
  508. }
  509. _ERR_RET:
  510. if (fullpath != NULL)
  511. {
  512. rt_free(fullpath);
  513. }
  514. return ret;
  515. }
  516. int dfs_file_close(struct dfs_file *file)
  517. {
  518. int ret = -RT_ERROR;
  519. if (file)
  520. {
  521. if (dfs_file_lock() == RT_EOK)
  522. {
  523. rt_atomic_t ref_count = rt_atomic_load(&(file->ref_count));
  524. if (ref_count == 1 && file->fops && file->fops->close)
  525. {
  526. DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG, "fops->close(file)");
  527. #ifdef RT_USING_PAGECACHE
  528. if (file->vnode->aspace)
  529. {
  530. dfs_aspace_flush(file->vnode->aspace);
  531. }
  532. #endif
  533. ret = file->fops->close(file);
  534. if (ret == 0) /* close file sucessfully */
  535. {
  536. DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file)");
  537. dfs_file_unref(file);
  538. }
  539. else
  540. {
  541. LOG_W("close file:%s failed on low level file system", file->dentry->pathname);
  542. }
  543. }
  544. else
  545. {
  546. DLOG(msg, "dfs_file", "dfs_file", DLOG_MSG, "dfs_file_unref(file)");
  547. dfs_file_unref(file);
  548. ret = 0;
  549. }
  550. dfs_file_unlock();
  551. }
  552. }
  553. return ret;
  554. }
  555. ssize_t dfs_file_read(struct dfs_file *file, void *buf, size_t len)
  556. {
  557. ssize_t ret = -EBADF;
  558. if (file)
  559. {
  560. /* check whether read */
  561. if (!(dfs_fflags(file->flags) & DFS_F_FREAD))
  562. {
  563. ret = -EPERM;
  564. }
  565. else if (!file->fops || !file->fops->read)
  566. {
  567. ret = -ENOSYS;
  568. }
  569. else if (file->vnode && file->vnode->type != FT_DIRECTORY)
  570. {
  571. /* fpos lock */
  572. off_t pos = dfs_file_get_fpos(file);
  573. ret = rw_verify_area(file, &pos, len);
  574. if (ret > 0)
  575. {
  576. len = ret;
  577. if (dfs_is_mounted(file->vnode->mnt) == 0)
  578. {
  579. #ifdef RT_USING_PAGECACHE
  580. if (file->vnode->aspace && !(file->flags & O_DIRECT))
  581. {
  582. ret = dfs_aspace_read(file, buf, len, &pos);
  583. }
  584. else
  585. #endif
  586. {
  587. ret = file->fops->read(file, buf, len, &pos);
  588. }
  589. }
  590. else
  591. {
  592. ret = -EINVAL;
  593. }
  594. }
  595. /* fpos unlock */
  596. dfs_file_set_fpos(file, pos);
  597. }
  598. }
  599. return ret;
  600. }
  601. ssize_t dfs_file_write(struct dfs_file *file, const void *buf, size_t len)
  602. {
  603. size_t ret = -EBADF;
  604. if (file)
  605. {
  606. if (!(dfs_fflags(file->flags) & DFS_F_FWRITE))
  607. {
  608. LOG_W("bad write flags.");
  609. ret = -EBADF;
  610. }
  611. else if (!file->fops || !file->fops->write)
  612. {
  613. LOG_W("no fops write.");
  614. ret = -ENOSYS;
  615. }
  616. else if (file->vnode && file->vnode->type != FT_DIRECTORY)
  617. {
  618. /* fpos lock */
  619. off_t pos = dfs_file_get_fpos(file);
  620. ret = rw_verify_area(file, &pos, len);
  621. if (ret > 0)
  622. {
  623. len = ret;
  624. DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG,
  625. "dfs_file_write(fd, buf, %d)", len);
  626. if (dfs_is_mounted(file->vnode->mnt) == 0)
  627. {
  628. #ifdef RT_USING_PAGECACHE
  629. if (file->vnode->aspace && !(file->flags & O_DIRECT))
  630. {
  631. ret = dfs_aspace_write(file, buf, len, &pos);
  632. }
  633. else
  634. #endif
  635. {
  636. ret = file->fops->write(file, buf, len, &pos);
  637. }
  638. if (file->flags & O_SYNC)
  639. {
  640. file->fops->flush(file);
  641. }
  642. }
  643. else
  644. {
  645. ret = -EINVAL;
  646. }
  647. }
  648. /* fpos unlock */
  649. dfs_file_set_fpos(file, pos);
  650. }
  651. }
  652. return ret;
  653. }
  654. off_t generic_dfs_lseek(struct dfs_file *file, off_t offset, int whence)
  655. {
  656. off_t foffset;
  657. if (whence == SEEK_SET)
  658. foffset = offset;
  659. else if (whence == SEEK_CUR)
  660. foffset = file->fpos + offset;
  661. else if (whence == SEEK_END)
  662. foffset = file->vnode->size + offset;
  663. else
  664. return -EINVAL;
  665. return foffset;
  666. }
  667. off_t dfs_file_lseek(struct dfs_file *file, off_t offset, int wherece)
  668. {
  669. off_t retval = -EINVAL;
  670. if (file && file->fops->lseek)
  671. {
  672. if (dfs_is_mounted(file->vnode->mnt) == 0)
  673. {
  674. /* fpos lock */
  675. off_t pos = dfs_file_get_fpos(file);
  676. retval = file->fops->lseek(file, offset, wherece);
  677. if (retval >= 0)
  678. {
  679. pos = retval;
  680. }
  681. /* fpos unlock */
  682. dfs_file_set_fpos(file, pos);
  683. }
  684. }
  685. return retval;
  686. }
  687. int dfs_file_stat(const char *path, struct stat *buf)
  688. {
  689. int ret = -ENOENT;
  690. char *fullpath = RT_NULL;
  691. struct dfs_mnt *mnt = RT_NULL;
  692. struct dfs_dentry *dentry = RT_NULL;
  693. fullpath = dfs_normalize_path(NULL, path);
  694. if (fullpath)
  695. {
  696. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
  697. mnt = dfs_mnt_lookup(fullpath);
  698. if (mnt)
  699. {
  700. char *tmp = dfs_nolink_path(&mnt, fullpath, 1);
  701. if (tmp)
  702. {
  703. rt_free(fullpath);
  704. fullpath = tmp;
  705. }
  706. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
  707. dentry = dfs_dentry_lookup(mnt, fullpath, 0);
  708. if (dentry)
  709. {
  710. DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
  711. if (mnt->fs_ops->stat)
  712. {
  713. DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
  714. if (dfs_is_mounted(mnt) == 0)
  715. {
  716. ret = mnt->fs_ops->stat(dentry, buf);
  717. }
  718. }
  719. /* unref dentry */
  720. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
  721. dfs_dentry_unref(dentry);
  722. dentry = RT_NULL;
  723. }
  724. }
  725. rt_free(fullpath);
  726. fullpath = RT_NULL;
  727. }
  728. else
  729. {
  730. ret = -ENOMEM;
  731. }
  732. return ret;
  733. }
  734. int dfs_file_lstat(const char *path, struct stat *buf)
  735. {
  736. int ret = -ENOENT;
  737. char *fullpath = RT_NULL;
  738. struct dfs_mnt *mnt = RT_NULL;
  739. struct dfs_dentry *dentry = RT_NULL;
  740. fullpath = dfs_normalize_path(NULL, path);
  741. if (fullpath)
  742. {
  743. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
  744. mnt = dfs_mnt_lookup(fullpath);
  745. if (mnt)
  746. {
  747. char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
  748. if (tmp)
  749. {
  750. rt_free(fullpath);
  751. fullpath = tmp;
  752. }
  753. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
  754. dentry = dfs_dentry_lookup(mnt, fullpath, 0);
  755. if (dentry)
  756. {
  757. DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
  758. if (mnt->fs_ops->stat)
  759. {
  760. DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
  761. if (dfs_is_mounted(mnt) == 0)
  762. {
  763. ret = mnt->fs_ops->stat(dentry, buf);
  764. }
  765. }
  766. /* unref dentry */
  767. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
  768. dfs_dentry_unref(dentry);
  769. dentry = RT_NULL;
  770. }
  771. }
  772. rt_free(fullpath);
  773. fullpath = RT_NULL;
  774. }
  775. else
  776. {
  777. ret = -ENOMEM;
  778. }
  779. rt_set_errno(-ret);
  780. return ret;
  781. }
  782. int dfs_file_fstat(struct dfs_file *file, struct stat *buf)
  783. {
  784. size_t ret = -EBADF;
  785. if (file)
  786. {
  787. if (file->fops && file->fops->ioctl)
  788. {
  789. // ret = file->fops->fstat(file, buf);
  790. }
  791. else
  792. {
  793. ret = -ENOSYS;
  794. }
  795. }
  796. else
  797. {
  798. ret = -EBADF;
  799. }
  800. return ret;
  801. }
  802. int dfs_file_setattr(const char *path, struct dfs_attr *attr)
  803. {
  804. int ret = -RT_ERROR;
  805. char *fullpath = RT_NULL;
  806. struct dfs_mnt *mnt = RT_NULL;
  807. struct dfs_dentry *dentry = RT_NULL;
  808. fullpath = dfs_normalize_path(NULL, path);
  809. if (fullpath)
  810. {
  811. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
  812. mnt = dfs_mnt_lookup(fullpath);
  813. if (mnt)
  814. {
  815. char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
  816. if (tmp)
  817. {
  818. rt_free(fullpath);
  819. fullpath = tmp;
  820. }
  821. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
  822. dentry = dfs_dentry_lookup(mnt, fullpath, 0);
  823. if (dentry)
  824. {
  825. DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
  826. if (mnt->fs_ops->setattr)
  827. {
  828. DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->setattr(dentry, attr)");
  829. if (dfs_is_mounted(mnt) == 0)
  830. {
  831. ret = mnt->fs_ops->setattr(dentry, attr);
  832. }
  833. }
  834. /* unref dentry */
  835. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
  836. dfs_dentry_unref(dentry);
  837. dentry = RT_NULL;
  838. }
  839. }
  840. rt_free(fullpath);
  841. fullpath = RT_NULL;
  842. }
  843. return ret;
  844. }
  845. int dfs_file_ioctl(struct dfs_file *file, int cmd, void *args)
  846. {
  847. size_t ret = 0;
  848. if (file)
  849. {
  850. if (file->fops && file->fops->ioctl)
  851. {
  852. if (dfs_is_mounted(file->vnode->mnt) == 0)
  853. {
  854. ret = file->fops->ioctl(file, cmd, args);
  855. }
  856. else
  857. {
  858. ret = -EINVAL;
  859. }
  860. }
  861. else
  862. {
  863. ret = -ENOSYS;
  864. }
  865. }
  866. else
  867. {
  868. ret = -EBADF;
  869. }
  870. return ret;
  871. }
  872. int dfs_file_fcntl(int fd, int cmd, unsigned long arg)
  873. {
  874. int ret = 0;
  875. struct dfs_file *file;
  876. file = fd_get(fd);
  877. if (file)
  878. {
  879. switch (cmd)
  880. {
  881. case F_DUPFD:
  882. ret = dfs_dup(fd, arg);
  883. break;
  884. case F_GETFD:
  885. ret = file->mode;
  886. break;
  887. case F_SETFD:
  888. file->mode = arg;
  889. break;
  890. case F_GETFL:
  891. ret = file->flags;
  892. break;
  893. case F_SETFL:
  894. {
  895. int flags = (int)(rt_base_t)arg;
  896. int mask =
  897. #ifdef O_ASYNC
  898. O_ASYNC |
  899. #endif
  900. #ifdef O_DIRECT
  901. O_DIRECT |
  902. #endif
  903. #ifdef O_NOATIME
  904. O_NOATIME |
  905. #endif
  906. O_APPEND | O_NONBLOCK;
  907. flags &= mask;
  908. file->flags &= ~mask;
  909. file->flags |= flags;
  910. break;
  911. }
  912. case F_GETLK:
  913. break;
  914. case F_SETLK:
  915. case F_SETLKW:
  916. break;
  917. #ifdef RT_USING_MUSLLIBC
  918. case F_DUPFD_CLOEXEC:
  919. ret = -EINVAL;
  920. break;
  921. #endif
  922. default:
  923. ret = -EPERM;
  924. break;
  925. }
  926. }
  927. else
  928. {
  929. ret = -EBADF;
  930. }
  931. return ret;
  932. }
  933. int dfs_file_fsync(struct dfs_file *file)
  934. {
  935. int ret = -EBADF;
  936. if (file)
  937. {
  938. if (file->fops->flush)
  939. {
  940. if (dfs_is_mounted(file->vnode->mnt) == 0)
  941. {
  942. #ifdef RT_USING_PAGECACHE
  943. if (file->vnode->aspace)
  944. {
  945. dfs_aspace_flush(file->vnode->aspace);
  946. }
  947. #endif
  948. ret = file->fops->flush(file);
  949. }
  950. else
  951. {
  952. ret = -EINVAL;
  953. }
  954. }
  955. }
  956. return ret;
  957. }
  958. int dfs_file_unlink(const char *path)
  959. {
  960. int ret = -RT_ERROR;
  961. char *fullpath = RT_NULL;
  962. struct dfs_mnt *mnt = RT_NULL;
  963. struct dfs_dentry *dentry = RT_NULL;
  964. fullpath = dfs_normalize_path(NULL, path);
  965. if (fullpath)
  966. {
  967. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
  968. mnt = dfs_mnt_lookup(fullpath);
  969. if (mnt)
  970. {
  971. char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
  972. if (tmp)
  973. {
  974. rt_free(fullpath);
  975. fullpath = tmp;
  976. }
  977. if (strcmp(mnt->fullpath, fullpath) != 0)
  978. {
  979. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
  980. dentry = dfs_dentry_lookup(mnt, fullpath, 0);
  981. if (dentry)
  982. {
  983. rt_bool_t has_child = RT_FALSE;
  984. has_child = dfs_mnt_has_child_mnt(mnt, fullpath);
  985. #ifdef RT_USING_PAGECACHE
  986. if (dentry->vnode->aspace)
  987. {
  988. dfs_aspace_clean(dentry->vnode->aspace);
  989. }
  990. #endif
  991. dfs_file_lock();
  992. if (has_child == RT_FALSE)
  993. {
  994. /* no child mnt point, unlink it */
  995. ret = -RT_ERROR;
  996. if (mnt->fs_ops->unlink)
  997. {
  998. if (dfs_is_mounted(mnt) == 0)
  999. {
  1000. ret = mnt->fs_ops->unlink(dentry);
  1001. }
  1002. }
  1003. }
  1004. else
  1005. {
  1006. ret = -EBUSY;
  1007. }
  1008. dfs_file_unlock();
  1009. /* release this dentry */
  1010. dfs_dentry_unref(dentry);
  1011. }
  1012. else
  1013. {
  1014. /* no this entry */
  1015. ret = -ENOENT;
  1016. }
  1017. }
  1018. else
  1019. {
  1020. /* it's a mount point, failed for busy */
  1021. ret = -EBUSY;
  1022. }
  1023. }
  1024. else
  1025. {
  1026. ret = -ENOENT;
  1027. }
  1028. /* release fullpath */
  1029. rt_free(fullpath);
  1030. }
  1031. else
  1032. {
  1033. ret = -ENOMEM;
  1034. }
  1035. return ret;
  1036. }
  1037. int dfs_file_link(const char *oldname, const char *newname)
  1038. {
  1039. int ret = -1;
  1040. struct stat stat;
  1041. struct dfs_mnt *mnt = RT_NULL;
  1042. char *old_fullpath, *new_fullpath;
  1043. if (dfs_file_isdir(oldname) == 0)
  1044. {
  1045. return ret;
  1046. }
  1047. if (dfs_file_lstat(newname, &stat) >= 0)
  1048. {
  1049. return ret;
  1050. }
  1051. old_fullpath = dfs_normalize_path(NULL, oldname);
  1052. if (old_fullpath)
  1053. {
  1054. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", old_fullpath);
  1055. mnt = dfs_mnt_lookup(old_fullpath);
  1056. if (mnt == RT_NULL)
  1057. {
  1058. rt_free(old_fullpath);
  1059. return -1;
  1060. }
  1061. char *tmp = dfs_nolink_path(&mnt, old_fullpath, 0);
  1062. if (tmp)
  1063. {
  1064. rt_free(old_fullpath);
  1065. old_fullpath = tmp;
  1066. }
  1067. }
  1068. new_fullpath = dfs_normalize_path(NULL, newname);
  1069. if (new_fullpath)
  1070. {
  1071. char *tmp = dfs_nolink_path(&mnt, new_fullpath, 0);
  1072. if (tmp)
  1073. {
  1074. rt_free(new_fullpath);
  1075. new_fullpath = tmp;
  1076. }
  1077. }
  1078. if (old_fullpath && new_fullpath)
  1079. {
  1080. struct dfs_dentry *old_dentry, *new_dentry;
  1081. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", old_fullpath);
  1082. old_dentry = dfs_dentry_lookup(mnt, old_fullpath, 0);
  1083. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", new_fullpath);
  1084. new_dentry = dfs_dentry_create(mnt, new_fullpath);
  1085. if (old_dentry && new_dentry)
  1086. {
  1087. if (mnt->fs_ops->link)
  1088. {
  1089. if (dfs_is_mounted(mnt) == 0)
  1090. {
  1091. ret = mnt->fs_ops->link(old_dentry, new_dentry);
  1092. }
  1093. }
  1094. }
  1095. dfs_dentry_unref(old_dentry);
  1096. dfs_dentry_unref(new_dentry);
  1097. }
  1098. if (old_fullpath)
  1099. {
  1100. rt_free(old_fullpath);
  1101. }
  1102. if (new_fullpath)
  1103. {
  1104. rt_free(new_fullpath);
  1105. }
  1106. return ret;
  1107. }
  1108. /* symlink creates a symbolic link named `linkpath` which contains the string `target`. */
  1109. int dfs_file_symlink(const char *target, const char *linkpath)
  1110. {
  1111. int ret = -RT_ERROR;
  1112. char *fullpath = RT_NULL, *parent = RT_NULL;
  1113. struct dfs_mnt *mnt = RT_NULL;
  1114. struct dfs_dentry *dentry = RT_NULL;
  1115. if (target && linkpath)
  1116. {
  1117. if (linkpath[0] != '/')
  1118. {
  1119. fullpath = dfs_normalize_path(NULL, linkpath);
  1120. }
  1121. else
  1122. {
  1123. fullpath = (char*)linkpath;
  1124. }
  1125. /* linkpath should be not exist */
  1126. if (dfs_file_access(fullpath, O_RDONLY) != 0)
  1127. {
  1128. char *index;
  1129. /* get parent path */
  1130. index = strrchr(fullpath, '/');
  1131. if (index)
  1132. {
  1133. int length = index - fullpath;
  1134. if (length > 0)
  1135. {
  1136. parent = (char*) rt_malloc (length + 1);
  1137. if (parent)
  1138. {
  1139. memcpy(parent, fullpath, length);
  1140. parent[length] = '\0';
  1141. }
  1142. }
  1143. else
  1144. {
  1145. parent = (char*) rt_malloc (1 + 1);
  1146. if (parent)
  1147. {
  1148. parent[0] = '/';
  1149. parent[1] = '\0';
  1150. }
  1151. }
  1152. }
  1153. if (parent)
  1154. {
  1155. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
  1156. mnt = dfs_mnt_lookup(parent);
  1157. if (mnt)
  1158. {
  1159. char *tmp = dfs_nolink_path(&mnt, parent, 0);
  1160. if (tmp)
  1161. {
  1162. rt_free(parent);
  1163. parent = tmp;
  1164. }
  1165. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
  1166. dentry = dfs_dentry_lookup(mnt, parent, 0);
  1167. if (dentry)
  1168. {
  1169. if (dentry->mnt->fs_ops->symlink)
  1170. {
  1171. char *path = dfs_normalize_path(parent, target);
  1172. if (path)
  1173. {
  1174. char *tmp = dfs_nolink_path(&mnt, path, 0);
  1175. if (tmp)
  1176. {
  1177. rt_free(path);
  1178. path = tmp;
  1179. }
  1180. else
  1181. {
  1182. tmp = path;
  1183. }
  1184. ret = rt_strncmp(parent, path, strlen(parent));
  1185. if (ret == 0)
  1186. {
  1187. tmp = path + strlen(parent);
  1188. if (*tmp == '/')
  1189. {
  1190. tmp ++;
  1191. }
  1192. }
  1193. if (dfs_is_mounted(mnt) == 0)
  1194. {
  1195. ret = mnt->fs_ops->symlink(dentry, tmp, index + 1);
  1196. }
  1197. rt_free(path);
  1198. }
  1199. }
  1200. else
  1201. {
  1202. ret = -ENOSYS;
  1203. }
  1204. dfs_dentry_unref(dentry);
  1205. }
  1206. else
  1207. {
  1208. ret = -ENOENT;
  1209. }
  1210. }
  1211. else
  1212. {
  1213. ret = -ENOENT;
  1214. }
  1215. rt_free(parent);
  1216. }
  1217. }
  1218. if (fullpath != linkpath)
  1219. rt_free(fullpath);
  1220. }
  1221. else
  1222. {
  1223. ret = -EINVAL;
  1224. }
  1225. return ret;
  1226. }
  1227. int dfs_file_readlink(const char *path, char *buf, int bufsize)
  1228. {
  1229. int ret = -RT_ERROR;
  1230. char *fullpath = RT_NULL;
  1231. struct dfs_mnt *mnt = RT_NULL;
  1232. struct dfs_dentry *dentry = RT_NULL;
  1233. fullpath = dfs_normalize_path(NULL, path);
  1234. if (fullpath)
  1235. {
  1236. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
  1237. mnt = dfs_mnt_lookup(fullpath);
  1238. if (mnt)
  1239. {
  1240. char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
  1241. if (tmp)
  1242. {
  1243. rt_free(fullpath);
  1244. fullpath = tmp;
  1245. }
  1246. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", fullpath);
  1247. dentry = dfs_dentry_lookup(mnt, fullpath, 0);
  1248. if (dentry)
  1249. {
  1250. if (mnt->fs_ops->readlink)
  1251. {
  1252. if (dfs_is_mounted(mnt) == 0)
  1253. {
  1254. ret = mnt->fs_ops->readlink(dentry, buf, bufsize);
  1255. }
  1256. }
  1257. else
  1258. {
  1259. ret = -ENOSYS;
  1260. }
  1261. /* release this dentry */
  1262. dfs_dentry_unref(dentry);
  1263. }
  1264. else
  1265. {
  1266. /* no this entry */
  1267. ret = -ENOENT;
  1268. }
  1269. }
  1270. else
  1271. {
  1272. ret = -ENOENT;
  1273. }
  1274. /* release fullpath */
  1275. rt_free(fullpath);
  1276. }
  1277. else
  1278. {
  1279. ret = -ENOMEM;
  1280. }
  1281. return ret;
  1282. }
  1283. int dfs_file_rename(const char *old_file, const char *new_file)
  1284. {
  1285. int ret = -1;
  1286. struct dfs_mnt *mnt = RT_NULL;
  1287. char *old_fullpath, *new_fullpath;
  1288. old_fullpath = dfs_normalize_path(NULL, old_file);
  1289. if (old_fullpath)
  1290. {
  1291. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", old_fullpath);
  1292. mnt = dfs_mnt_lookup(old_fullpath);
  1293. if (mnt == RT_NULL)
  1294. {
  1295. rt_free(old_fullpath);
  1296. return -1;
  1297. }
  1298. char *tmp = dfs_nolink_path(&mnt, old_fullpath, 0);
  1299. if (tmp)
  1300. {
  1301. rt_free(old_fullpath);
  1302. old_fullpath = tmp;
  1303. }
  1304. }
  1305. new_fullpath = dfs_normalize_path(NULL, new_file);
  1306. if (new_fullpath)
  1307. {
  1308. char *tmp = dfs_nolink_path(&mnt, new_fullpath, 0);
  1309. if (tmp)
  1310. {
  1311. rt_free(new_fullpath);
  1312. new_fullpath = tmp;
  1313. }
  1314. }
  1315. if (old_fullpath && new_fullpath)
  1316. {
  1317. struct dfs_dentry *old_dentry, *new_dentry;
  1318. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_lookup(mnt, %s)", old_fullpath);
  1319. old_dentry = dfs_dentry_lookup(mnt, old_fullpath, 0);
  1320. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_create(%s)", new_fullpath);
  1321. new_dentry = dfs_dentry_create(mnt, new_fullpath);
  1322. if (old_dentry && new_dentry)
  1323. {
  1324. if (mnt->fs_ops->rename)
  1325. {
  1326. if (dfs_is_mounted(mnt) == 0)
  1327. {
  1328. #ifdef RT_USING_PAGECACHE
  1329. if (old_dentry->vnode->aspace)
  1330. {
  1331. dfs_aspace_clean(old_dentry->vnode->aspace);
  1332. }
  1333. #endif
  1334. ret = mnt->fs_ops->rename(old_dentry, new_dentry);
  1335. }
  1336. }
  1337. }
  1338. dfs_dentry_unref(old_dentry);
  1339. dfs_dentry_unref(new_dentry);
  1340. }
  1341. if (old_fullpath)
  1342. {
  1343. rt_free(old_fullpath);
  1344. }
  1345. if (new_fullpath)
  1346. {
  1347. rt_free(new_fullpath);
  1348. }
  1349. return ret;
  1350. }
  1351. int dfs_file_ftruncate(struct dfs_file *file, off_t length)
  1352. {
  1353. int ret = 0;
  1354. if (file)
  1355. {
  1356. if (file->fops->truncate)
  1357. {
  1358. if (dfs_is_mounted(file->vnode->mnt) == 0)
  1359. {
  1360. #ifdef RT_USING_PAGECACHE
  1361. if (file->vnode->aspace)
  1362. {
  1363. dfs_aspace_clean(file->vnode->aspace);
  1364. }
  1365. #endif
  1366. ret = file->fops->truncate(file, length);
  1367. }
  1368. else
  1369. {
  1370. ret = -EINVAL;
  1371. }
  1372. }
  1373. else
  1374. {
  1375. ret = -ENOSYS;
  1376. }
  1377. }
  1378. else
  1379. {
  1380. ret = -EBADF;
  1381. }
  1382. return ret;
  1383. }
  1384. int dfs_file_flush(struct dfs_file *file)
  1385. {
  1386. int ret = 0;
  1387. if (file)
  1388. {
  1389. if (file->fops->flush)
  1390. {
  1391. if (dfs_is_mounted(file->vnode->mnt) == 0)
  1392. {
  1393. #ifdef RT_USING_PAGECACHE
  1394. if (file->vnode->aspace)
  1395. {
  1396. dfs_aspace_flush(file->vnode->aspace);
  1397. }
  1398. #endif
  1399. ret = file->fops->flush(file);
  1400. }
  1401. else
  1402. {
  1403. ret = -EINVAL;
  1404. }
  1405. }
  1406. else
  1407. {
  1408. ret = -ENOSYS;
  1409. }
  1410. }
  1411. else
  1412. {
  1413. ret = -EBADF;
  1414. }
  1415. return ret;
  1416. }
  1417. int dfs_file_getdents(struct dfs_file *file, struct dirent *dirp, size_t nbytes)
  1418. {
  1419. int ret = -RT_ERROR;
  1420. if (file)
  1421. {
  1422. if (file->vnode && S_ISDIR(file->vnode->mode))
  1423. {
  1424. if (file->fops && file->fops->getdents)
  1425. {
  1426. DLOG(msg, "dfs_file", file->dentry->mnt->fs_ops->name, DLOG_MSG, "fops->getdents()");
  1427. if (dfs_is_mounted(file->vnode->mnt) == 0)
  1428. {
  1429. ret = file->fops->getdents(file, dirp, nbytes);
  1430. }
  1431. else
  1432. {
  1433. ret = -EINVAL;
  1434. }
  1435. }
  1436. }
  1437. }
  1438. else
  1439. {
  1440. ret = -EBADF;
  1441. }
  1442. return ret;
  1443. }
  1444. /**
  1445. * this function will check the path is it a directory.
  1446. *
  1447. * @param path the file path.
  1448. *
  1449. * @return 0 on is dir, -1 on not dir.
  1450. */
  1451. int dfs_file_isdir(const char *path)
  1452. {
  1453. int ret = -RT_ERROR;
  1454. char *fullpath = RT_NULL;
  1455. struct dfs_mnt *mnt = RT_NULL;
  1456. struct dfs_dentry *dentry = RT_NULL;
  1457. fullpath = dfs_normalize_path(NULL, path);
  1458. if (fullpath)
  1459. {
  1460. DLOG(msg, "dfs_file", "mnt", DLOG_MSG, "dfs_mnt_lookup(%s)", fullpath);
  1461. mnt = dfs_mnt_lookup(fullpath);
  1462. if (mnt)
  1463. {
  1464. char *tmp = dfs_nolink_path(&mnt, fullpath, 1);
  1465. if (tmp)
  1466. {
  1467. rt_free(fullpath);
  1468. fullpath = tmp;
  1469. }
  1470. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dentry = dfs_dentry_lookup(mnt, %s)", fullpath);
  1471. dentry = dfs_dentry_lookup(mnt, fullpath, 0);
  1472. if (dentry)
  1473. {
  1474. DLOG(msg, "dentry", "dfs_file", DLOG_MSG_RET, "return dentry");
  1475. if (mnt->fs_ops->stat)
  1476. {
  1477. struct stat stat = {0};
  1478. DLOG(msg, "dfs_file", mnt->fs_ops->name, DLOG_MSG, "fs_ops->stat(dentry, buf)");
  1479. if (dfs_is_mounted(mnt) == 0)
  1480. {
  1481. ret = mnt->fs_ops->stat(dentry, &stat);
  1482. }
  1483. if (ret == RT_EOK && S_ISDIR(stat.st_mode))
  1484. {
  1485. ret = RT_EOK;
  1486. }
  1487. else
  1488. {
  1489. ret = -RT_ERROR;
  1490. }
  1491. }
  1492. /* unref dentry */
  1493. DLOG(msg, "dfs_file", "dentry", DLOG_MSG, "dfs_dentry_unref(dentry)");
  1494. dfs_dentry_unref(dentry);
  1495. dentry = RT_NULL;
  1496. }
  1497. }
  1498. rt_free(fullpath);
  1499. fullpath = RT_NULL;
  1500. }
  1501. return ret;
  1502. }
  1503. int dfs_file_access(const char *path, mode_t mode)
  1504. {
  1505. int ret;
  1506. struct dfs_file file;
  1507. dfs_file_init(&file);
  1508. if (dfs_file_open(&file, path, O_RDONLY, mode) >= 0)
  1509. {
  1510. ret = 0;
  1511. dfs_file_close(&file);
  1512. }
  1513. else
  1514. {
  1515. ret = -1;
  1516. }
  1517. dfs_file_deinit(&file);
  1518. return ret;
  1519. }
  1520. #ifdef RT_USING_SMART
  1521. int dfs_file_mmap2(struct dfs_file *file, struct dfs_mmap2_args *mmap2)
  1522. {
  1523. int ret = RT_EOK;
  1524. if (file && mmap2)
  1525. {
  1526. if (file->vnode->type == FT_REGULAR)
  1527. {
  1528. ret = dfs_file_mmap(file, mmap2);
  1529. if (ret != 0)
  1530. {
  1531. ret = ret > 0 ? ret : -ret;
  1532. rt_set_errno(ret);
  1533. }
  1534. }
  1535. else if (file->vnode->type != FT_DEVICE || !file->vnode->fops->ioctl)
  1536. {
  1537. rt_set_errno(EINVAL);
  1538. }
  1539. else if (file->vnode->type == FT_DEVICE && file->vnode->fops->ioctl)
  1540. {
  1541. if (dfs_is_mounted(file->vnode->mnt) == 0)
  1542. {
  1543. ret = file->vnode->fops->ioctl(file, RT_FIOMMAP2, mmap2);
  1544. }
  1545. else
  1546. {
  1547. ret = EINVAL;
  1548. }
  1549. if (ret != 0)
  1550. {
  1551. ret = ret > 0 ? ret : -ret;
  1552. rt_set_errno(ret);
  1553. }
  1554. }
  1555. }
  1556. return ret;
  1557. }
  1558. #endif
  1559. #ifdef RT_USING_FINSH
  1560. #define _COLOR_RED "\033[31m"
  1561. #define _COLOR_GREEN "\033[32m"
  1562. #define _COLOR_YELLOW "\033[33m"
  1563. #define _COLOR_BLUE "\033[34m"
  1564. #define _COLOR_CYAN "\033[36m"
  1565. #define _COLOR_WHITE "\033[37m"
  1566. #define _COLOR_NORMAL "\033[0m"
  1567. void ls(const char *pathname)
  1568. {
  1569. struct dirent dirent;
  1570. struct stat stat;
  1571. int length;
  1572. char *fullpath, *path;
  1573. struct dfs_file file;
  1574. if (pathname == NULL)
  1575. {
  1576. #ifdef DFS_USING_WORKDIR
  1577. /* open current working directory */
  1578. path = rt_strdup(working_directory);
  1579. #else
  1580. path = rt_strdup("/");
  1581. #endif
  1582. if (path == NULL)
  1583. {
  1584. return; /* out of memory */
  1585. }
  1586. }
  1587. else
  1588. {
  1589. path = dfs_normalize_path(NULL, (char *)pathname);
  1590. if (path == NULL)
  1591. {
  1592. return; /* out of memory */
  1593. }
  1594. }
  1595. dfs_file_init(&file);
  1596. /* list directory */
  1597. DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_open(%s, O_DIRECTORY, 0)", path);
  1598. if (dfs_file_open(&file, path, O_DIRECTORY, 0) >= 0)
  1599. {
  1600. char *link_fn = (char *)rt_malloc(DFS_PATH_MAX);
  1601. if (link_fn)
  1602. {
  1603. rt_kprintf("Directory %s:\n", path);
  1604. do
  1605. {
  1606. memset(&dirent, 0, sizeof(struct dirent));
  1607. DLOG(group, "foreach_item");
  1608. DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_getdents(&dirent)");
  1609. length = dfs_file_getdents(&file, &dirent, sizeof(struct dirent));
  1610. if (length > 0)
  1611. {
  1612. DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "dirent.d_name=%s", dirent.d_name);
  1613. memset(&stat, 0, sizeof(struct stat));
  1614. /* build full path for each file */
  1615. fullpath = dfs_normalize_path(path, dirent.d_name);
  1616. if (fullpath == NULL)
  1617. break;
  1618. DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_lstat(%s, &stat)", fullpath);
  1619. if (dfs_file_lstat(fullpath, &stat) == 0)
  1620. {
  1621. if (S_ISDIR(stat.st_mode))
  1622. {
  1623. rt_kprintf(_COLOR_BLUE "%-20s" _COLOR_NORMAL, dirent.d_name);
  1624. rt_kprintf("%-25s\n", "<DIR>");
  1625. }
  1626. else if (S_ISLNK(stat.st_mode))
  1627. {
  1628. int ret = 0;
  1629. rt_kprintf(_COLOR_CYAN "%-20s" _COLOR_NORMAL, dirent.d_name);
  1630. ret = dfs_file_readlink(fullpath, link_fn, DFS_PATH_MAX);
  1631. if (ret > 0)
  1632. {
  1633. char *link_path = link_fn;
  1634. struct dfs_mnt *mnt = RT_NULL;
  1635. mnt = dfs_mnt_lookup(fullpath);
  1636. if (mnt)
  1637. {
  1638. char *tmp = dfs_nolink_path(&mnt, fullpath, 0);
  1639. if (tmp)
  1640. {
  1641. char *index;
  1642. index = strrchr(fullpath, '/');
  1643. if (index)
  1644. {
  1645. int length = index - fullpath;
  1646. char *parent = (char*) rt_malloc (length + 1);
  1647. if (parent)
  1648. {
  1649. rt_memcpy(parent, fullpath, length);
  1650. parent[length] = '\0';
  1651. ret = rt_strncmp(parent, link_fn, length);
  1652. if (ret == 0)
  1653. {
  1654. link_path = link_fn + length;
  1655. if (*link_path == '/')
  1656. {
  1657. link_path ++;
  1658. }
  1659. }
  1660. rt_free(parent);
  1661. }
  1662. }
  1663. rt_free(tmp);
  1664. }
  1665. }
  1666. rt_kprintf("-> %s\n", link_path);
  1667. }
  1668. else
  1669. {
  1670. rt_kprintf(_COLOR_RED "-> link_error\n" _COLOR_NORMAL);
  1671. }
  1672. }
  1673. else if (stat.st_mode & (S_IXUSR | S_IXGRP | S_IXOTH))
  1674. {
  1675. rt_kprintf(_COLOR_GREEN "%-20s" _COLOR_NORMAL, dirent.d_name);
  1676. rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
  1677. }
  1678. else if (S_ISCHR(stat.st_mode))
  1679. {
  1680. rt_kprintf(_COLOR_YELLOW "%-20s" _COLOR_NORMAL, dirent.d_name);
  1681. rt_kprintf("%-25s\n", "<CHR>");
  1682. }
  1683. else
  1684. {
  1685. rt_kprintf("%-20s", dirent.d_name);
  1686. rt_kprintf("%-25lu\n", (unsigned long)stat.st_size);
  1687. }
  1688. }
  1689. else
  1690. {
  1691. rt_kprintf(_COLOR_RED "%-20s\n" _COLOR_NORMAL, dirent.d_name);
  1692. }
  1693. rt_free(fullpath);
  1694. }
  1695. else
  1696. {
  1697. DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "return NULL");
  1698. }
  1699. DLOG(group_end);
  1700. } while (length > 0);
  1701. rt_free(link_fn);
  1702. }
  1703. DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_close()");
  1704. dfs_file_close(&file);
  1705. }
  1706. else
  1707. {
  1708. rt_kprintf("No such directory\n");
  1709. }
  1710. dfs_file_deinit(&file);
  1711. DLOG(msg, "dfs_file", "dfs", DLOG_MSG_RET, "return");
  1712. rt_free(path);
  1713. }
  1714. void cat(const char *filename)
  1715. {
  1716. int length = 0;
  1717. char buffer[81];
  1718. struct dfs_file file;
  1719. if (filename && dfs_file_isdir(filename) == 0)
  1720. {
  1721. rt_kprintf("cat: %s Is a directory\n", filename);
  1722. return;
  1723. }
  1724. dfs_file_init(&file);
  1725. DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_open(%s, O_RDONLY, 0)", filename);
  1726. if (dfs_file_open(&file, filename, O_RDONLY, 0) < 0)
  1727. {
  1728. rt_kprintf("Open %s failed\n", filename);
  1729. dfs_file_deinit(&file);
  1730. return;
  1731. }
  1732. do
  1733. {
  1734. rt_memset(buffer, 0x0, sizeof(buffer));
  1735. DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_read(fd, buffer, %d)", sizeof(buffer) - 1);
  1736. length = dfs_file_read(&file, (void *)buffer, sizeof(buffer) - 1);
  1737. if (length > 0)
  1738. {
  1739. buffer[length] = '\0';
  1740. rt_kprintf("%s", buffer);
  1741. }
  1742. } while (length > 0);
  1743. rt_kprintf("\n");
  1744. DLOG(msg, "dfs", "dfs_file", DLOG_MSG, "dfs_file_close()");
  1745. dfs_file_close(&file);
  1746. dfs_file_deinit(&file);
  1747. }
  1748. #define BUF_SZ 4096
  1749. static void copyfile(const char *src, const char *dst)
  1750. {
  1751. int ret;
  1752. struct dfs_file src_file, dst_file;
  1753. rt_uint8_t *block_ptr;
  1754. rt_int32_t read_bytes;
  1755. block_ptr = (rt_uint8_t *)rt_malloc(BUF_SZ);
  1756. if (block_ptr == NULL)
  1757. {
  1758. rt_kprintf("out of memory\n");
  1759. return;
  1760. }
  1761. dfs_file_init(&src_file);
  1762. ret = dfs_file_open(&src_file, src, O_RDONLY, 0);
  1763. if (ret < 0)
  1764. {
  1765. dfs_file_deinit(&src_file);
  1766. rt_free(block_ptr);
  1767. rt_kprintf("Read %s failed\n", src);
  1768. return;
  1769. }
  1770. dfs_file_init(&dst_file);
  1771. ret = dfs_file_open(&dst_file, dst, O_WRONLY | O_CREAT | O_TRUNC, 0);
  1772. if (ret < 0)
  1773. {
  1774. dfs_file_deinit(&dst_file);
  1775. dfs_file_close(&src_file);
  1776. dfs_file_deinit(&src_file);
  1777. rt_free(block_ptr);
  1778. rt_kprintf("Write %s failed\n", dst);
  1779. return;
  1780. }
  1781. do
  1782. {
  1783. read_bytes = dfs_file_read(&src_file, block_ptr, BUF_SZ);
  1784. if (read_bytes > 0)
  1785. {
  1786. int length;
  1787. length = dfs_file_write(&dst_file, block_ptr, read_bytes);
  1788. if (length != read_bytes)
  1789. {
  1790. /* write failed. */
  1791. rt_kprintf("Write file data failed, errno=%d\n", length);
  1792. break;
  1793. }
  1794. }
  1795. } while (read_bytes > 0);
  1796. dfs_file_close(&dst_file);
  1797. dfs_file_deinit(&dst_file);
  1798. dfs_file_close(&src_file);
  1799. dfs_file_deinit(&src_file);
  1800. rt_free(block_ptr);
  1801. }
  1802. extern int mkdir(const char *path, mode_t mode);
  1803. static void copydir(const char *src, const char *dst)
  1804. {
  1805. struct dirent dirent;
  1806. struct stat stat;
  1807. int length;
  1808. struct dfs_file file;
  1809. dfs_file_init(&file);
  1810. if (dfs_file_open(&file, src, O_DIRECTORY, 0) < 0)
  1811. {
  1812. rt_kprintf("open %s failed\n", src);
  1813. dfs_file_deinit(&file);
  1814. return ;
  1815. }
  1816. do
  1817. {
  1818. rt_memset(&dirent, 0, sizeof(struct dirent));
  1819. length = dfs_file_getdents(&file, &dirent, sizeof(struct dirent));
  1820. if (length > 0)
  1821. {
  1822. char *src_entry_full = NULL;
  1823. char *dst_entry_full = NULL;
  1824. if (strcmp(dirent.d_name, "..") == 0 || strcmp(dirent.d_name, ".") == 0)
  1825. continue;
  1826. /* build full path for each file */
  1827. if ((src_entry_full = dfs_normalize_path(src, dirent.d_name)) == NULL)
  1828. {
  1829. rt_kprintf("out of memory!\n");
  1830. break;
  1831. }
  1832. if ((dst_entry_full = dfs_normalize_path(dst, dirent.d_name)) == NULL)
  1833. {
  1834. rt_kprintf("out of memory!\n");
  1835. rt_free(src_entry_full);
  1836. break;
  1837. }
  1838. rt_memset(&stat, 0, sizeof(struct stat));
  1839. if (dfs_file_lstat(src_entry_full, &stat) != 0)
  1840. {
  1841. rt_kprintf("open file: %s failed\n", dirent.d_name);
  1842. continue;
  1843. }
  1844. if (S_ISDIR(stat.st_mode))
  1845. {
  1846. mkdir(dst_entry_full, 0);
  1847. copydir(src_entry_full, dst_entry_full);
  1848. }
  1849. else
  1850. {
  1851. copyfile(src_entry_full, dst_entry_full);
  1852. }
  1853. rt_free(src_entry_full);
  1854. rt_free(dst_entry_full);
  1855. }
  1856. }
  1857. while (length > 0);
  1858. dfs_file_close(&file);
  1859. dfs_file_deinit(&file);
  1860. }
  1861. static const char *_get_path_lastname(const char *path)
  1862. {
  1863. char *ptr;
  1864. if ((ptr = (char *)strrchr(path, '/')) == NULL)
  1865. return path;
  1866. /* skip the '/' then return */
  1867. return ++ptr;
  1868. }
  1869. void copy(const char *src, const char *dst)
  1870. {
  1871. #define FLAG_SRC_TYPE 0x03
  1872. #define FLAG_SRC_IS_DIR 0x01
  1873. #define FLAG_SRC_IS_FILE 0x02
  1874. #define FLAG_SRC_NON_EXSIT 0x00
  1875. #define FLAG_DST_TYPE 0x0C
  1876. #define FLAG_DST_IS_DIR 0x04
  1877. #define FLAG_DST_IS_FILE 0x08
  1878. #define FLAG_DST_NON_EXSIT 0x00
  1879. struct stat stat;
  1880. uint32_t flag = 0;
  1881. /* check the staus of src and dst */
  1882. if (dfs_file_lstat(src, &stat) < 0)
  1883. {
  1884. rt_kprintf("copy failed, bad %s\n", src);
  1885. return;
  1886. }
  1887. if (S_ISDIR(stat.st_mode))
  1888. flag |= FLAG_SRC_IS_DIR;
  1889. else
  1890. flag |= FLAG_SRC_IS_FILE;
  1891. if (dfs_file_stat(dst, &stat) < 0)
  1892. {
  1893. flag |= FLAG_DST_NON_EXSIT;
  1894. }
  1895. else
  1896. {
  1897. if (S_ISDIR(stat.st_mode))
  1898. flag |= FLAG_DST_IS_DIR;
  1899. else
  1900. flag |= FLAG_DST_IS_FILE;
  1901. }
  1902. //2. check status
  1903. if ((flag & FLAG_SRC_IS_DIR) && (flag & FLAG_DST_IS_FILE))
  1904. {
  1905. rt_kprintf("cp faild, cp dir to file is not permitted!\n");
  1906. return ;
  1907. }
  1908. //3. do copy
  1909. if (flag & FLAG_SRC_IS_FILE)
  1910. {
  1911. if (flag & FLAG_DST_IS_DIR)
  1912. {
  1913. char *fdst;
  1914. fdst = dfs_normalize_path(dst, _get_path_lastname(src));
  1915. if (fdst == NULL)
  1916. {
  1917. rt_kprintf("out of memory\n");
  1918. return;
  1919. }
  1920. copyfile(src, fdst);
  1921. rt_free(fdst);
  1922. }
  1923. else
  1924. {
  1925. copyfile(src, dst);
  1926. }
  1927. }
  1928. else //flag & FLAG_SRC_IS_DIR
  1929. {
  1930. if (flag & FLAG_DST_IS_DIR)
  1931. {
  1932. char *fdst;
  1933. fdst = dfs_normalize_path(dst, _get_path_lastname(src));
  1934. if (fdst == NULL)
  1935. {
  1936. rt_kprintf("out of memory\n");
  1937. return;
  1938. }
  1939. mkdir(fdst, 0);
  1940. copydir(src, fdst);
  1941. rt_free(fdst);
  1942. }
  1943. else if ((flag & FLAG_DST_TYPE) == FLAG_DST_NON_EXSIT)
  1944. {
  1945. mkdir(dst, 0);
  1946. copydir(src, dst);
  1947. }
  1948. else
  1949. {
  1950. copydir(src, dst);
  1951. }
  1952. }
  1953. }
  1954. FINSH_FUNCTION_EXPORT(copy, copy file or dir)
  1955. #endif