libcm is a C development framework with an emphasis on audio signal processing applications.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

cmFileSys.c 30KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297
  1. #include "cmPrefix.h"
  2. #include "cmGlobal.h"
  3. #include "cmRpt.h"
  4. #include "cmErr.h"
  5. #include "cmCtx.h"
  6. #include "cmMem.h"
  7. #include "cmMallocDebug.h"
  8. #include "cmLinkedHeap.h"
  9. #include "cmFileSys.h"
  10. #include <sys/stat.h>
  11. #include <errno.h>
  12. #include <libgen.h> // basename(), dirname()
  13. #include <dirent.h> // opendir()/readdir()
  14. #include <limits.h> // PATH_MAX
  15. #include <sys/types.h> // mkdir
  16. #ifdef OS_OSX
  17. #include "osx/cmFileSysOsx.h"
  18. #endif
  19. #ifdef OS_LINUX
  20. #include "linux/cmFileSysLinux.h"
  21. #endif
  22. cmFileSysH_t cmFileSysNullHandle = {NULL};
  23. typedef struct
  24. {
  25. cmErr_t err;
  26. cmLHeapH_t heapH;
  27. const cmChar_t* appNameStr;
  28. #ifdef OS_OSX
  29. _cmFsOsx_t* p;
  30. #endif
  31. #ifdef OS_LINUX
  32. _cmFsLinux_t* p;
  33. #endif
  34. } cmFs_t;
  35. cmFsRC_t _cmFileSysError( cmFs_t* p, cmFsRC_t rc, int sysErr, const cmChar_t* fmt, ... )
  36. {
  37. va_list vl;
  38. va_start(vl,fmt);
  39. if( sysErr == 0 )
  40. rc = cmErrVMsg(&p->err,rc,fmt,vl);
  41. else
  42. {
  43. const unsigned bufCharCnt = 511;
  44. cmChar_t buf[bufCharCnt+1];
  45. vsnprintf(buf,bufCharCnt,fmt,vl);
  46. rc = cmErrMsg(&p->err,rc,"%s\nSystem Msg:%s",buf,strerror(sysErr));
  47. }
  48. va_end(vl);
  49. return rc;
  50. }
  51. cmFs_t* _cmFileSysHandleToPtr( cmFileSysH_t h )
  52. {
  53. cmFs_t* p = (cmFs_t*)h.h;
  54. assert( p != NULL);
  55. return p;
  56. }
  57. cmFsRC_t _cmFileSysFinalize( cmFs_t* p )
  58. {
  59. cmFsRC_t rc = kOkFsRC;
  60. if( cmLHeapIsValid(p->heapH) )
  61. cmLHeapDestroy(&p->heapH);
  62. #ifdef OS_OSX
  63. if( p->p != NULL )
  64. if((rc = _cmOsxFileSysFinalize(p->p) ) != kOkFsRC )
  65. {
  66. _cmFileSysError(p,kOsxFailFsRC,0,"The OSX file system finalization failed.");
  67. return rc;
  68. }
  69. #endif
  70. #ifdef OS_LINUX
  71. if( p->p != NULL )
  72. if((rc = _cmLinuxFileSysFinalize(p->p) ) != kOkFsRC )
  73. {
  74. _cmFileSysError(p,kLinuxFailFsRC,0,"The Linux file system finalization failed.");
  75. return rc;
  76. }
  77. #endif
  78. cmMemPtrFree(&p);
  79. return rc;
  80. }
  81. cmFsRC_t cmFileSysInitialize( cmFileSysH_t* hp, cmCtx_t* ctx, const cmChar_t* appNameStr )
  82. {
  83. cmFs_t* p;
  84. cmFsRC_t rc;
  85. cmErr_t err;
  86. if((rc = cmFileSysFinalize(hp)) != kOkFsRC )
  87. return rc;
  88. cmErrSetup(&err,&ctx->rpt,"File System");
  89. if((p = cmMemAllocZ( cmFs_t, 1 )) == NULL )
  90. return cmErrMsg(&err,kMemAllocErrFsRC,"Unable to allocate the file system object.");
  91. cmErrClone(&p->err,&err);
  92. if(cmLHeapIsValid( p->heapH = cmLHeapCreate(1024,ctx)) == false )
  93. {
  94. rc = _cmFileSysError(p,kLHeapAllocErrFsRC,0,"Unable to allocate the linked heap.");
  95. goto errLabel;
  96. }
  97. #ifdef OS_OSX
  98. if( (rc = _cmOsxFileSysInit(&p->p, p->heapH, &p->err)) != kOkFsRC )
  99. {
  100. rc = _cmFileSysError(p,kOsxFailFsRC,0,"OSX file system initialization failed.");
  101. goto errLabel;
  102. }
  103. #endif
  104. #ifdef OS_LINUX
  105. if( (rc = _cmLinuxFileSysInit(&p->p, p->heapH, &p->err)) != kOkFsRC )
  106. {
  107. rc = _cmFileSysError(p,kLinuxFailFsRC,0,"Linux file system initialization failed.");
  108. goto errLabel;
  109. }
  110. else
  111. {
  112. #endif
  113. p->appNameStr = cmLhAllocStr(p->heapH,appNameStr);
  114. hp->h = p;
  115. #ifdef OS_LINUX
  116. cmChar_t hidAppNameStr[ strlen(appNameStr) + 2 ];
  117. strcpy(hidAppNameStr,".");
  118. strcat(hidAppNameStr,appNameStr);
  119. p->p->prefDir = cmFileSysMakeFn( *hp, p->p->prefDir, hidAppNameStr, NULL, NULL );
  120. // the resource directory must exist before the program can start
  121. p->p->rsrcDir = cmFileSysMakeFn( *hp, p->p->rsrcDir, appNameStr, NULL, NULL );
  122. }
  123. #endif
  124. errLabel:
  125. if( rc != kOkFsRC )
  126. return _cmFileSysFinalize(p);
  127. return kOkFsRC;
  128. }
  129. cmFsRC_t cmFileSysFinalize( cmFileSysH_t* hp )
  130. {
  131. cmFsRC_t rc;
  132. if( hp==NULL || cmFileSysIsValid(*hp) == false )
  133. return kOkFsRC;
  134. cmFs_t* p = _cmFileSysHandleToPtr(*hp);
  135. if((rc = _cmFileSysFinalize(p)) != kOkFsRC )
  136. return rc;
  137. hp->h = NULL;
  138. return rc;
  139. }
  140. const cmChar_t* cmFileSysAppName( cmFileSysH_t h )
  141. {
  142. cmFs_t* p = _cmFileSysHandleToPtr(h);
  143. return p->appNameStr;
  144. }
  145. const cmChar_t* cmFileSysPrefsDir( cmFileSysH_t h )
  146. {
  147. cmFs_t* p = _cmFileSysHandleToPtr(h);
  148. #if defined OS_OSX || defined OS_LINUX
  149. return p->p->prefDir;
  150. #else
  151. return NULL;
  152. #endif
  153. }
  154. const cmChar_t* cmFileSysRsrcDir( cmFileSysH_t h )
  155. {
  156. cmFs_t* p = _cmFileSysHandleToPtr(h);
  157. #if defined OS_OSX || defined OS_LINUX
  158. return p->p->rsrcDir;
  159. #else
  160. return NULL;
  161. #endif
  162. }
  163. const cmChar_t* cmFileSysUserDir( cmFileSysH_t h )
  164. {
  165. cmFs_t* p = _cmFileSysHandleToPtr(h);
  166. #if defined OS_OSX || defined OS_LINUX
  167. return p->p->userDir;
  168. #else
  169. return NULL;
  170. #endif
  171. }
  172. bool cmFileSysIsValid( cmFileSysH_t h )
  173. { return h.h != NULL; }
  174. bool _cmFileSysIsDir( cmFs_t* p, const cmChar_t* dirStr )
  175. {
  176. struct stat s;
  177. errno = 0;
  178. if( stat(dirStr,&s) != 0 )
  179. {
  180. // if the dir does not exist
  181. if( errno == ENOENT )
  182. return false;
  183. _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'",dirStr);
  184. return false;
  185. }
  186. return S_ISDIR(s.st_mode);
  187. }
  188. bool cmFileSysIsDir( cmFileSysH_t h, const cmChar_t* dirStr )
  189. {
  190. cmFs_t* p = _cmFileSysHandleToPtr(h);
  191. return _cmFileSysIsDir(p,dirStr);
  192. }
  193. bool _cmFileSysIsFile( cmFs_t* p, const cmChar_t* fnStr )
  194. {
  195. struct stat s;
  196. errno = 0;
  197. if( stat(fnStr,&s) != 0 )
  198. {
  199. // if the file does not exist
  200. if( errno == ENOENT )
  201. return false;
  202. _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'.",fnStr);
  203. return false;
  204. }
  205. return S_ISREG(s.st_mode);
  206. }
  207. bool cmFileSysIsFile( cmFileSysH_t h, const cmChar_t* fnStr )
  208. {
  209. cmFs_t* p = _cmFileSysHandleToPtr(h);
  210. return _cmFileSysIsFile(p,fnStr);
  211. }
  212. bool _cmFileSysIsLink( cmFs_t* p, const cmChar_t* fnStr )
  213. {
  214. struct stat s;
  215. errno = 0;
  216. if( stat(fnStr,&s) != 0 )
  217. {
  218. // if the file does not exist
  219. if( errno == ENOENT )
  220. return false;
  221. _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'.",fnStr);
  222. return false;
  223. }
  224. return S_ISLNK(s.st_mode);
  225. }
  226. bool cmFileSysIsLink( cmFileSysH_t h, const cmChar_t* fnStr )
  227. {
  228. cmFs_t* p = _cmFileSysHandleToPtr(h);
  229. return _cmFileSysIsLink(p,fnStr);
  230. }
  231. bool _cmFileSysConcat( cmChar_t* rp, unsigned rn, char sepChar, const cmChar_t* suffixStr )
  232. {
  233. unsigned m = strlen(rp);
  234. // m==0 if no sep needs to be inserted or removed
  235. //if( m == 0 )
  236. // return false;
  237. if( m != 0 )
  238. {
  239. // if a sep char needs to be inserted
  240. if( rp[m-1] != sepChar && suffixStr[0] != sepChar )
  241. {
  242. assert((m+1)<rn);
  243. if((m+1)>=rn)
  244. return false;
  245. rp[m] = sepChar;
  246. rp[m+1]= 0;
  247. ++m;
  248. }
  249. else
  250. // if a sep char needs to be removed
  251. if( rp[m-1] == sepChar && suffixStr[0] == sepChar )
  252. {
  253. rp[m-1] = 0;
  254. --m;
  255. }
  256. }
  257. assert( rn>=m && strlen(rp)+strlen(suffixStr) <= rn );
  258. strncat(rp,suffixStr,rn-m);
  259. return true;
  260. }
  261. const cmChar_t* cmFileSysVMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
  262. {
  263. cmFsRC_t rc = kOkFsRC;
  264. cmChar_t* rp = NULL;
  265. const cmChar_t* dp = NULL;
  266. unsigned n = 0;
  267. cmFs_t* p = _cmFileSysHandleToPtr(h);
  268. char pathSep = cmPathSeparatorChar[0];
  269. char extSep = '.';
  270. va_list vl_t;
  271. va_copy(vl_t,vl);
  272. assert( fn != NULL );
  273. // get prefix directory length
  274. if( dir != NULL )
  275. n += strlen(dir) + 1; // add 1 for ending sep
  276. // get file name length
  277. n += strlen(fn);
  278. // get extension length
  279. if( ext != NULL )
  280. n += strlen(ext) + 1; // add 1 for period
  281. // get length of all var args dir's
  282. while( (dp = va_arg(vl,const cmChar_t*)) != NULL )
  283. n += strlen(dp) + 1; // add 1 for ending sep
  284. // add 1 for terminating zero and allocate memory
  285. if((rp = cmLHeapAllocZ( p->heapH, n+1 )) == NULL )
  286. {
  287. rc = _cmFileSysError(p,kMemAllocErrFsRC,0,"Unable to allocate file name memory.");
  288. goto errLabel;
  289. }
  290. va_copy(vl,vl_t);
  291. rp[n] = 0;
  292. rp[0] = 0;
  293. // copy out the prefix dir
  294. if( dir != NULL )
  295. strncat(rp,dir,n-strlen(rp));
  296. // copy out ecmh of the var arg's directories
  297. while((dp = va_arg(vl,const cmChar_t*)) != NULL )
  298. if(!_cmFileSysConcat(rp,n,pathSep,dp) )
  299. {
  300. assert(0);
  301. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  302. goto errLabel;
  303. }
  304. // copy out the file name
  305. if(!_cmFileSysConcat(rp,n,pathSep,fn))
  306. {
  307. assert(0);
  308. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  309. goto errLabel;
  310. }
  311. // copy out the extension
  312. if( ext != NULL )
  313. if(!_cmFileSysConcat(rp,n,extSep,ext))
  314. {
  315. assert(0);
  316. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  317. goto errLabel;
  318. }
  319. assert(strlen(rp)<=n);
  320. errLabel:
  321. if( rc != kOkFsRC && rp != NULL )
  322. cmLHeapFree(p->heapH, rp );
  323. return rp;
  324. }
  325. const cmChar_t* cmFileSysMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn, const cmChar_t* ext, ... )
  326. {
  327. va_list vl;
  328. va_start(vl,ext);
  329. const cmChar_t* retPtr = cmFileSysVMakeFn(h,dir,fn,ext,vl);
  330. va_end(vl);
  331. return retPtr;
  332. }
  333. void cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn )
  334. {
  335. cmFs_t* p = _cmFileSysHandleToPtr(h);
  336. if( fn == NULL )
  337. return;
  338. cmLHeapFree(p->heapH, (void*)fn);
  339. }
  340. cmFsRC_t cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir )
  341. {
  342. cmFs_t* p = _cmFileSysHandleToPtr(h);
  343. if( mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0 )
  344. return _cmFileSysError( p, kMkDirFailFsRC, errno, "The attempt to create the directory '%s' failed.",dir);
  345. return kOkFsRC;
  346. }
  347. cmFsRC_t cmFileSysMkDirAll( cmFileSysH_t h, const cmChar_t* dir )
  348. {
  349. cmFsRC_t rc = kOkFsRC;
  350. cmFs_t* p = _cmFileSysHandleToPtr(h);
  351. cmChar_t** a = NULL;
  352. unsigned i;
  353. if((a = cmFileSysDirParts(h,dir)) == NULL )
  354. return _cmFileSysError(p, kInvalidDirFsRC, 0, "The directory '%s' could not be parsed.",cmStringNullGuard(dir));
  355. for(i=0; rc==kOkFsRC && a[i]!=NULL; ++i)
  356. {
  357. cmChar_t* d = cmFileSysFormDir(h,a,i+1);
  358. if( cmFileSysIsDir(h,d)==false )
  359. if((rc = cmFileSysMkDir(h,d)) != kOkFsRC )
  360. break;
  361. cmFileSysFreeDir(h,d);
  362. }
  363. cmFileSysFreeDirParts(h,a);
  364. return rc;
  365. }
  366. cmFileSysPathPart_t* cmFileSysPathParts( cmFileSysH_t h, const cmChar_t* pathStr )
  367. {
  368. int n = 0; // char's in pathStr
  369. int dn = 0; // char's in the dir part
  370. int fn = 0; // char's in the name part
  371. int en = 0; // char's in the ext part
  372. cmChar_t* cp = NULL;
  373. cmFileSysPathPart_t* rp = NULL;
  374. cmFs_t* p = _cmFileSysHandleToPtr(h);
  375. if( pathStr==NULL )
  376. return NULL;
  377. // skip leading white space
  378. while( *pathStr )
  379. {
  380. if( isspace(*pathStr ) )
  381. ++pathStr;
  382. else
  383. break;
  384. }
  385. // get the length of pathStr
  386. if((n = strlen(pathStr)) == 0 )
  387. return NULL;
  388. // remove trailing spaces
  389. for(; n>0; --n)
  390. {
  391. if( isspace(pathStr[n-1]) )
  392. --n;
  393. else
  394. break;
  395. }
  396. //
  397. if( n == 0 )
  398. return NULL;
  399. char buf[n+1];
  400. buf[n] = 0;
  401. // Get the last word (which may be a file name) from pathStr.
  402. // (pathStr must be copied into a buf because basename()
  403. // is allowed to change the values in its arg.)
  404. strncpy(buf,pathStr,n);
  405. cp = basename(buf);
  406. if( cp != NULL )
  407. {
  408. cmChar_t* ep;
  409. // does the last word have a period in it
  410. if( (ep = strchr(cp,'.')) != NULL )
  411. {
  412. *ep = 0; // end the file name at the period
  413. ++ep; // set the ext marker
  414. en = strlen(ep); // get the length of the ext
  415. }
  416. fn = strlen(cp); //get the length of the file name
  417. }
  418. // Get the directory part.
  419. // ( pathStr must be copied into a buf because dirname()
  420. // is allowed to change the values in its arg.)
  421. strncpy(buf,pathStr,n);
  422. // if the last char in pathStr[] is '/' then the whole string is a dir.
  423. // (this is not the answer that dirname() and basename() would give).
  424. if( pathStr[n-1] == cmPathSeparatorChar[0] )
  425. {
  426. fn = 0;
  427. en = 0;
  428. dn = n;
  429. cp = buf;
  430. }
  431. else
  432. {
  433. cp = dirname(buf);
  434. }
  435. if( cp != NULL )
  436. dn = strlen(cp);
  437. // get the total size of the returned memory. (add 3 for ecmh possible terminating zero)
  438. n = sizeof(cmFileSysPathPart_t) + dn + fn + en + 3;
  439. // alloc memory
  440. if((rp = (cmFileSysPathPart_t*)cmLHeapAllocZ( p->heapH, n )) == NULL )
  441. {
  442. _cmFileSysError( p, kLHeapAllocErrFsRC, 0, "Unable to allocate the file system path part record for '%s'.",pathStr);
  443. return NULL;
  444. }
  445. // set the return pointers for ecmh of the parts
  446. rp->dirStr = (const cmChar_t* )(rp + 1);
  447. rp->fnStr = rp->dirStr + dn + 1;
  448. rp->extStr = rp->fnStr + fn + 1;
  449. // if there is a directory part
  450. if( dn>0 )
  451. strcpy((cmChar_t*)rp->dirStr,cp);
  452. else
  453. rp->dirStr = NULL;
  454. if( fn || en )
  455. {
  456. // Get the trailing word again.
  457. // pathStr must be copied into a buf because basename() may
  458. // is allowed to change the values in its arg.
  459. strncpy(buf,pathStr,n);
  460. cp = basename(buf);
  461. if( cp != NULL )
  462. {
  463. cmChar_t* ep;
  464. if( (ep = strchr(cp,'.')) != NULL )
  465. {
  466. *ep = 0;
  467. ++ep;
  468. assert( strlen(ep) == en );
  469. strcpy((cmChar_t*)rp->extStr,ep);
  470. }
  471. assert( strlen(cp) == fn );
  472. if(fn)
  473. strcpy((cmChar_t*)rp->fnStr,cp);
  474. }
  475. }
  476. if( fn == 0 )
  477. rp->fnStr = NULL;
  478. if( en == 0 )
  479. rp->extStr = NULL;
  480. return rp;
  481. }
  482. void cmFileSysFreePathParts( cmFileSysH_t h, cmFileSysPathPart_t* pp )
  483. {
  484. if( pp == NULL )
  485. return;
  486. cmFs_t* p = _cmFileSysHandleToPtr(h);
  487. cmLHeapFree(p->heapH, (void*)pp );
  488. }
  489. cmChar_t** cmFileSysDirParts( cmFileSysH_t h, const cmChar_t* dirStr )
  490. {
  491. cmFs_t* p = _cmFileSysHandleToPtr(h);
  492. const cmChar_t* s = dirStr;
  493. const cmChar_t* ep = dirStr + strlen(dirStr);
  494. char pathSep = cmPathSeparatorChar[0];
  495. unsigned n = 2;
  496. unsigned i = 0;
  497. // skip leading white space or pathsep
  498. while( isspace(*s) && s < ep )
  499. ++s;
  500. // if the directory string is empty
  501. if( s >= ep )
  502. return NULL;
  503. // set the beginning of the input dirStr past any leading white space
  504. dirStr = s;
  505. // count the number of dir segments - this might overcount the
  506. // number of segments if there are multiple repeated path seperator
  507. // char's - but this is ok because 'n' is only used to determine
  508. // the size of the returned array - which will simply have some
  509. // NULL entries at the end.
  510. for(; s < ep; ++s )
  511. if( *s == pathSep )
  512. ++n;
  513. // allocate the array
  514. cmChar_t** a = cmLhAllocZ(p->heapH,cmChar_t*,n);
  515. // reset the cur location to the begin of the buf
  516. s = dirStr;
  517. // if the path starts at the root
  518. if( *s == pathSep )
  519. {
  520. a[0] = cmPathSeparatorChar; // cmPathSeparatorChar is a static string in cmGlobal.h
  521. i = 1;
  522. ++s;
  523. }
  524. for(; i<n && s<ep; ++i)
  525. {
  526. const cmChar_t* s1;
  527. if(( s1 = strchr(s,pathSep)) == NULL )
  528. s1 = ep;
  529. if( s1!=s )
  530. {
  531. unsigned sn = (s1 - s)+1;
  532. a[i] = cmLhAlloc(p->heapH,cmChar_t,sn);
  533. strncpy(a[i],s,sn-1);
  534. a[i][sn-1] = 0;
  535. }
  536. s = s1+1;
  537. }
  538. return a;
  539. }
  540. void cmFileSysFreeDirParts( cmFileSysH_t h, cmChar_t** dirStrArray )
  541. {
  542. if( dirStrArray == NULL )
  543. return;
  544. cmFs_t* p = _cmFileSysHandleToPtr(h);
  545. unsigned i;
  546. for(i=0; dirStrArray[i]!=NULL; ++i)
  547. {
  548. // cmPathSeparatorChar is statically alloc'd in cmGlobal.h
  549. if( dirStrArray[i] != cmPathSeparatorChar )
  550. cmLHeapFree(p->heapH,dirStrArray[i]);
  551. }
  552. cmLHeapFree(p->heapH, (void*)dirStrArray );
  553. }
  554. unsigned cmFileSysDirPartsCount(cmFileSysH_t h, cmChar_t** dirStrArray )
  555. {
  556. unsigned i = 0;
  557. if( dirStrArray == NULL )
  558. return 0;
  559. while(dirStrArray[i] != NULL )
  560. ++i;
  561. return i;
  562. }
  563. cmChar_t* cmFileSysFormDir( cmFileSysH_t h, cmChar_t** a, unsigned m )
  564. {
  565. cmFs_t* p = _cmFileSysHandleToPtr(h);
  566. unsigned n;
  567. unsigned i;
  568. // determine the length of the return string
  569. for(i=0,n=0; a[i]!=NULL && i<m; ++i)
  570. n += strlen(a[i]) + strlen(cmPathSeparatorChar);
  571. if( i<m && a[i] == NULL )
  572. {
  573. _cmFileSysError(p,kInvalidDirFsRC,0,"cmFileSysFormDir() cannot form a directory string from %i parts when only %i exist.",m,i);
  574. return NULL;
  575. }
  576. n += 1;
  577. // allocate the return string
  578. cmChar_t* r = cmLhAllocZ(p->heapH,cmChar_t,n);
  579. const cmChar_t* ep = r + n;
  580. // form the return string
  581. for(i=0; a[i]!=NULL && i<m; ++i)
  582. {
  583. strcat(r,a[i]);
  584. if( strcmp(a[i],cmPathSeparatorChar)!=0 && a[i+1]!=NULL )
  585. strcat(r,cmPathSeparatorChar);
  586. assert( r + strlen(r) <= ep );
  587. }
  588. return r;
  589. }
  590. void cmFileSysFreeDir( cmFileSysH_t h, const cmChar_t* dir )
  591. {
  592. if( dir == NULL )
  593. return;
  594. cmFs_t* p = _cmFileSysHandleToPtr(h);
  595. cmLHeapFree(p->heapH,(void*)dir);
  596. }
  597. typedef struct
  598. {
  599. cmFs_t* p;
  600. unsigned filterFlags;
  601. cmFileSysDirEntry_t* rp;
  602. cmChar_t* dataPtr;
  603. cmChar_t* endPtr;
  604. unsigned entryCnt;
  605. unsigned entryIdx;
  606. unsigned dataByteCnt;
  607. unsigned passIdx;
  608. } cmFileSysDeRecd_t;
  609. cmFsRC_t _cmFileSysDirGetEntries( cmFileSysDeRecd_t* drp, const cmChar_t* dirStr )
  610. {
  611. cmFsRC_t rc = kOkFsRC;
  612. DIR* dirp = NULL;
  613. struct dirent* dp = NULL;
  614. char curDirPtr[] = "./";
  615. unsigned dn = 0;
  616. if( dirStr == NULL || strlen(dirStr) == 0 )
  617. dirStr = curDirPtr;
  618. unsigned fnCharCnt= strlen(dirStr) + PATH_MAX;
  619. char fn[ fnCharCnt + 1 ];
  620. // copy the directory into fn[] ...
  621. fn[0] =0;
  622. fn[fnCharCnt] = 0;
  623. strcpy(fn,dirStr);
  624. assert( strlen(fn)+2 < fnCharCnt );
  625. // ... and be sure that it is terminated with a path sep char
  626. if( fn[ strlen(fn)-1 ] != cmPathSeparatorChar[0] )
  627. strcat(fn,cmPathSeparatorChar);
  628. // file names will be appended to the path at this location
  629. unsigned fni = strlen(fn);
  630. // open the directory
  631. if((dirp = opendir(dirStr)) == NULL)
  632. {
  633. rc = _cmFileSysError(drp->p,kOpenDirFailFsRC,errno,"Unable to open the directory:'%s'.",dirStr);
  634. goto errLabel;
  635. }
  636. // get the next directory entry
  637. while((dp = readdir(dirp)) != NULL )
  638. {
  639. // validate d_name
  640. if( (dp->d_name != NULL) && ((dn = strlen(dp->d_name)) > 0) )
  641. {
  642. unsigned flags = 0;
  643. // handle cases where d_name begins with '.'
  644. if( dp->d_name[0] == '.' )
  645. {
  646. if( strcmp(dp->d_name,".") == 0 )
  647. {
  648. if( cmIsFlag(drp->filterFlags,kCurDirFsFl) == false )
  649. continue;
  650. flags |= kCurDirFsFl;
  651. }
  652. if( strcmp(dp->d_name,"..") == 0 )
  653. {
  654. if( cmIsFlag(drp->filterFlags,kParentDirFsFl) == false )
  655. continue;
  656. flags |= kParentDirFsFl;
  657. }
  658. if( flags == 0 )
  659. {
  660. if( cmIsFlag(drp->filterFlags,kInvisibleFsFl) == false )
  661. continue;
  662. flags |= kInvisibleFsFl;
  663. }
  664. }
  665. fn[fni] = 0;
  666. strncat( fn, dp->d_name, fnCharCnt-fni );
  667. unsigned fnN = strlen(fn);
  668. // if the filename is too long for the buffer
  669. if( fnN > fnCharCnt )
  670. {
  671. rc = _cmFileSysError(drp->p, kFnTooLongFsRC, errno, "The directory entry:'%s' was too long to be processed.",dp->d_name);
  672. goto errLabel;
  673. }
  674. // is the entry a file
  675. if( _cmFileSysIsFile(drp->p,fn) )
  676. {
  677. if( cmIsFlag(drp->filterFlags,kFileFsFl)==false )
  678. continue;
  679. flags |= kFileFsFl;
  680. }
  681. else
  682. {
  683. // is the entry a dir
  684. if( _cmFileSysIsDir(drp->p,fn) )
  685. {
  686. if( cmIsFlag(drp->filterFlags,kDirFsFl) == false)
  687. continue;
  688. flags |= kDirFsFl;
  689. if( cmIsFlag(drp->filterFlags,kRecurseFsFl) )
  690. if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC )
  691. goto errLabel;
  692. }
  693. }
  694. assert(flags != 0);
  695. if( drp->passIdx == 0 )
  696. {
  697. ++drp->entryCnt;
  698. // add 1 for the name terminating zero
  699. drp->dataByteCnt += sizeof(cmFileSysDirEntry_t) + 1;
  700. if( cmIsFlag(drp->filterFlags,kFullPathFsFl) )
  701. drp->dataByteCnt += fnN;
  702. else
  703. drp->dataByteCnt += dn;
  704. }
  705. else
  706. {
  707. assert( drp->passIdx == 1 );
  708. assert( drp->entryIdx < drp->entryCnt );
  709. unsigned n = 0;
  710. if( cmIsFlag(drp->filterFlags,kFullPathFsFl) )
  711. {
  712. n = fnN+1;
  713. assert( drp->dataPtr + n <= drp->endPtr );
  714. strcpy(drp->dataPtr,fn);
  715. }
  716. else
  717. {
  718. n = dn+1;
  719. assert( drp->dataPtr + n <= drp->endPtr );
  720. strcpy(drp->dataPtr,dp->d_name);
  721. }
  722. drp->rp[ drp->entryIdx ].flags = flags;
  723. drp->rp[ drp->entryIdx ].name = drp->dataPtr;
  724. drp->dataPtr += n;
  725. assert( drp->dataPtr <= drp->endPtr);
  726. ++drp->entryIdx;
  727. }
  728. }
  729. }
  730. errLabel:
  731. return rc;
  732. }
  733. cmFileSysDirEntry_t* cmFileSysDirEntries( cmFileSysH_t h, const cmChar_t* dirStr, unsigned filterFlags, unsigned* dirEntryCntPtr )
  734. {
  735. cmFsRC_t rc = kOkFsRC;
  736. cmFileSysDeRecd_t r;
  737. memset(&r,0,sizeof(r));
  738. r.p = _cmFileSysHandleToPtr(h);
  739. r.filterFlags = filterFlags;
  740. assert( dirEntryCntPtr != NULL );
  741. *dirEntryCntPtr = 0;
  742. for(r.passIdx=0; r.passIdx<2; ++r.passIdx)
  743. {
  744. if((rc = _cmFileSysDirGetEntries( &r, dirStr )) != kOkFsRC )
  745. goto errLabel;
  746. if( r.passIdx == 0 )
  747. {
  748. // allocate memory to hold the return values
  749. if(( r.rp = (cmFileSysDirEntry_t *)cmLHeapAllocZ( r.p->heapH, r.dataByteCnt )) == NULL )
  750. {
  751. rc= _cmFileSysError( r.p, kMemAllocErrFsRC, 0, "Unable to allocate %i bytes of dir entry memory.",r.dataByteCnt);
  752. goto errLabel;
  753. }
  754. r.dataPtr = (cmChar_t*)(r.rp + r.entryCnt);
  755. r.endPtr = ((cmChar_t*)r.rp) + r.dataByteCnt;
  756. }
  757. }
  758. errLabel:
  759. if( rc == kOkFsRC )
  760. {
  761. assert( r.entryIdx == r.entryCnt );
  762. *dirEntryCntPtr = r.entryCnt;
  763. }
  764. else
  765. {
  766. if( r.rp != NULL )
  767. cmLHeapFree(r.p->heapH,r.rp);
  768. r.rp = NULL;
  769. }
  770. return r.rp;
  771. }
  772. void cmFileSysDirFreeEntries( cmFileSysH_t h, cmFileSysDirEntry_t* dp )
  773. {
  774. cmFs_t* p = _cmFileSysHandleToPtr(h);
  775. if( dp != NULL )
  776. cmLHeapFree(p->heapH,dp);
  777. }
  778. cmFsRC_t cmFileSysErrorCode( cmFileSysH_t h )
  779. {
  780. cmFs_t* p = _cmFileSysHandleToPtr(h);
  781. return cmErrLastRC(&p->err);
  782. }
  783. //
  784. //======================================================================================================
  785. // Begin global versions
  786. //
  787. cmFileSysH_t _cmFsH = { NULL };
  788. cmFsRC_t cmFsInitialize( cmCtx_t* ctx, const cmChar_t* appNameStr )
  789. { return cmFileSysInitialize(&_cmFsH,ctx,appNameStr); }
  790. cmFsRC_t cmFsFinalize()
  791. { return cmFileSysFinalize(&_cmFsH); }
  792. const cmChar_t* cmFsAppName()
  793. { return cmFileSysAppName(_cmFsH); }
  794. const cmChar_t* cmFsPrefsDir()
  795. { return cmFileSysPrefsDir(_cmFsH); }
  796. const cmChar_t* cmFsRsrcDir()
  797. { return cmFileSysRsrcDir(_cmFsH); }
  798. const cmChar_t* cmFsUserDir()
  799. { return cmFileSysUserDir(_cmFsH); }
  800. bool cmFsIsDir( const cmChar_t* dirStr )
  801. { return cmFileSysIsDir(_cmFsH,dirStr); }
  802. bool cmFsIsFile( const cmChar_t* fnStr )
  803. { return cmFileSysIsFile(_cmFsH,fnStr); }
  804. bool cmFsIsLink( const cmChar_t* fnStr )
  805. { return cmFileSysIsLink(_cmFsH,fnStr); }
  806. const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
  807. { return cmFileSysVMakeFn(_cmFsH,dirPrefix,fn,ext,vl); }
  808. const cmChar_t* cmFsMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... )
  809. {
  810. va_list vl;
  811. va_start(vl,ext);
  812. const cmChar_t* retPtr = cmFsVMakeFn(dirPrefix,fn,ext,vl);
  813. va_end(vl);
  814. return retPtr;
  815. }
  816. void cmFsFreeFn( const cmChar_t* fn )
  817. { cmFileSysFreeFn(_cmFsH, fn); }
  818. cmFsRC_t cmFsMkDir( const cmChar_t* dir )
  819. { return cmFileSysMkDir(_cmFsH,dir); }
  820. cmFsRC_t cmFsMkDirAll( const cmChar_t* dir )
  821. { return cmFileSysMkDirAll(_cmFsH,dir); }
  822. cmFileSysPathPart_t* cmFsPathParts( const cmChar_t* pathNameStr )
  823. { return cmFileSysPathParts(_cmFsH,pathNameStr); }
  824. void cmFsFreePathParts( cmFileSysPathPart_t* p )
  825. { cmFileSysFreePathParts(_cmFsH,p); }
  826. cmChar_t** cmFsDirParts( const cmChar_t* dirStr )
  827. { return cmFileSysDirParts(_cmFsH,dirStr); }
  828. void cmFsFreeDirParts( cmChar_t** dirStrArray )
  829. { cmFileSysFreeDirParts(_cmFsH,dirStrArray); }
  830. unsigned cmFsDirPartsCount( cmChar_t** dirStrArray )
  831. { return cmFileSysDirPartsCount(_cmFsH, dirStrArray); }
  832. cmChar_t* cmFsFormDir( cmChar_t** dirStrArray, unsigned n )
  833. { return cmFileSysFormDir(_cmFsH,dirStrArray,n); }
  834. void cmFsFreeDir( const cmChar_t* dir )
  835. { cmFileSysFreeDir(_cmFsH,dir); }
  836. cmFileSysDirEntry_t* cmFsDirEntries( const cmChar_t* dirStr, unsigned includeFlags, unsigned* dirEntryCntPtr )
  837. { return cmFileSysDirEntries(_cmFsH,dirStr,includeFlags,dirEntryCntPtr); }
  838. void cmFsDirFreeEntries( cmFileSysDirEntry_t* p )
  839. { cmFileSysDirFreeEntries(_cmFsH,p); }
  840. cmFsRC_t cmFsErrorCode()
  841. { return cmFileSysErrorCode(_cmFsH); }
  842. // end global version
  843. //======================================================================================================
  844. //
  845. //{ { label:cmFileSysEx }
  846. //(
  847. //
  848. // cmFileSysTest() function gives usage and testing examples
  849. // for some of the cmFileSys functions.
  850. // Note that the HOME_DIR macro should be set to your true
  851. // $HOME directroy and SRC_DIR should be set to any existing
  852. // and accessible directory.
  853. //
  854. //)
  855. //(
  856. #if defined(OS_OSX)
  857. #define HOME_DIR "/Users/kevin"
  858. #else
  859. #define HOME_DIR "/home/kevin"
  860. #endif
  861. #define SRC_DIR HOME_DIR"/src"
  862. void _cmFileSysTestFnParser(
  863. cmFileSysH_t h,
  864. cmRpt_t* rpt,
  865. const cmChar_t* fn );
  866. cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
  867. {
  868. // The global heap manager must have been initialized
  869. // via cmMdInitialize() prior to running this function.
  870. cmFsRC_t rc = kOkFsRC;
  871. cmFileSysH_t h = cmFileSysNullHandle;
  872. const char dir0[] = SRC_DIR;
  873. const char dir1[] = HOME_DIR"/blah";
  874. const char file0[] = HOME_DIR"/.emacs";
  875. const char file1[] = HOME_DIR"/blah.txt";
  876. const char not[] = " not ";
  877. const char e[] = " ";
  878. bool fl = false;
  879. const cmChar_t* fn = NULL;
  880. // Initialize the file system.
  881. if((rc = cmFileSysInitialize(&h,ctx,"fs_test")) != kOkFsRC )
  882. return rc;
  883. //----------------------------------------------------------
  884. // Print the standard directories
  885. //
  886. printf("Prefs Dir:%s\n",cmFsPrefsDir());
  887. printf("Rsrc Dir: %s\n",cmFsRsrcDir());
  888. printf("User Dir: %s\n",cmFsUserDir());
  889. //----------------------------------------------------------
  890. // Run the file system type checker
  891. //
  892. fl = cmFileSysIsDir(h,dir0);
  893. printf("'%s' is%sa directory.\n",dir0, (fl ? e : not));
  894. fl = cmFileSysIsDir(h,dir1);
  895. printf("'%s' is%sa directory.\n",dir1, (fl ? e : not));
  896. fl = cmFileSysIsFile(h,file0);
  897. printf("'%s' is%sa file.\n",file0, (fl ? e : not));
  898. fl = cmFileSysIsFile(h,file1);
  899. printf("'%s' is%sa file.\n",file1, (fl ? e : not));
  900. //----------------------------------------------------------
  901. // Test the file name creation functions
  902. //
  903. if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
  904. "c","src","cm","src",NULL)) != NULL)
  905. {
  906. printf("File:'%s'\n",fn);
  907. }
  908. cmFileSysFreeFn(h,fn);
  909. if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
  910. ".c","/src/","/cm/","/src/",NULL)) != NULL )
  911. {
  912. printf("File:'%s'\n",fn);
  913. }
  914. cmFileSysFreeFn(h,fn);
  915. //----------------------------------------------------------
  916. // Test the file name parsing functions
  917. //
  918. _cmFileSysTestFnParser(h,&ctx->rpt,
  919. HOME_DIR"/src/cm/src/cmFileSys.c");
  920. _cmFileSysTestFnParser(h,&ctx->rpt,
  921. HOME_DIR"/src/cm/src/cmFileSys");
  922. _cmFileSysTestFnParser(h,&ctx->rpt,
  923. HOME_DIR"/src/cm/src/cmFileSys/");
  924. _cmFileSysTestFnParser(h,&ctx->rpt,"cmFileSys.c");
  925. _cmFileSysTestFnParser(h,&ctx->rpt,"/");
  926. _cmFileSysTestFnParser(h,&ctx->rpt," ");
  927. //----------------------------------------------------------
  928. // Test the directory tree walking routines.
  929. //
  930. cmRptPrintf(&ctx->rpt,"Dir Entry Test:\n");
  931. cmFileSysDirEntry_t* dep;
  932. unsigned dirEntCnt;
  933. unsigned filterFlags = kDirFsFl
  934. | kFileFsFl
  935. | kRecurseFsFl
  936. | kFullPathFsFl;
  937. if((dep = cmFileSysDirEntries(h,SRC_DIR"/doc",filterFlags,
  938. &dirEntCnt)) != NULL)
  939. {
  940. unsigned i;
  941. for(i=0; i<dirEntCnt; ++i)
  942. cmRptPrintf(&ctx->rpt,"%s\n",dep[i].name);
  943. cmFileSysDirFreeEntries(h,dep);
  944. }
  945. //----------------------------------------------------------
  946. // Test the directory parsing/building routines.
  947. //
  948. cmRptPrintf(&ctx->rpt,"Dir Parsing routings:\n");
  949. cmChar_t** a;
  950. unsigned j;
  951. for(j=0; j<2; ++j)
  952. {
  953. const cmChar_t* dstr = dir0 + j;
  954. if((a = cmFileSysDirParts(h,dstr)) == NULL)
  955. cmRptPrint(&ctx->rpt,"cmFileSysDirParts() failed.\n");
  956. else
  957. {
  958. unsigned i;
  959. cmRptPrintf(&ctx->rpt,"Input:%s\n",dstr);
  960. for(i=0; a[i]!=NULL; ++i)
  961. cmRptPrintf(&ctx->rpt,"%i : %s\n",i,a[i]);
  962. cmChar_t* d;
  963. if((d = cmFileSysFormDir(h,a,
  964. cmFileSysDirPartsCount(h,a))) != NULL )
  965. {
  966. cmRptPrintf(&ctx->rpt,"Reformed:%s\n",d);
  967. }
  968. cmFileSysFreeDirParts(h,a);
  969. }
  970. }
  971. //----------------------------------------------------------
  972. // Test the extended mkdir routine.
  973. //
  974. if( cmFileSysMkDirAll(h, HOME_DIR"/temp/doc/doc" )!=kOkFsRC )
  975. {
  976. cmRptPrint(&ctx->rpt,"cmFileSysMkDirAll() failed.\n");
  977. }
  978. // finalize the file system
  979. if((rc = cmFileSysFinalize(&h)) != kOkFsRC )
  980. return rc;
  981. cmRptPrintf(&ctx->rpt,"File Test done\n");
  982. return rc;
  983. }
  984. // Parse a file name and print the results.
  985. // Called by cmFileSysTest().
  986. void _cmFileSysTestFnParser(
  987. cmFileSysH_t h,
  988. cmRpt_t* rpt,
  989. const cmChar_t* fn )
  990. {
  991. cmFileSysPathPart_t* pp;
  992. cmRptPrintf(rpt,"Fn Parse Test:%s\n",fn);
  993. if((pp = cmFileSysPathParts(h,fn)) != NULL )
  994. {
  995. if(pp->dirStr != NULL)
  996. cmRptPrintf(rpt,"Dir:%s\n",pp->dirStr);
  997. if(pp->fnStr != NULL)
  998. cmRptPrintf(rpt,"Fn:%s\n",pp->fnStr);
  999. if(pp->extStr != NULL )
  1000. cmRptPrintf(rpt,"Ext:%s\n",pp->extStr);
  1001. cmFileSysFreePathParts(h,pp);
  1002. }
  1003. cmRptPrintf(rpt,"\n");
  1004. }
  1005. //)
  1006. //}