libcm is a C development framework with an emphasis on audio signal processing applications.
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

cmFileSys.c 31KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342
  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( lstat(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 _cmFileSysIsSocket( cmFs_t* p, const cmChar_t* fnStr )
  232. {
  233. struct stat s;
  234. errno = 0;
  235. if( stat(fnStr,&s) != 0 )
  236. {
  237. // if the file does not exist
  238. if( errno == ENOENT )
  239. return false;
  240. _cmFileSysError( p, kStatFailFsRC, errno, "'stat' failed on '%s'.",fnStr);
  241. return false;
  242. }
  243. return S_ISSOCK(s.st_mode);
  244. }
  245. bool _cmFileSysConcat( cmChar_t* rp, unsigned rn, char sepChar, const cmChar_t* suffixStr )
  246. {
  247. unsigned m = strlen(rp);
  248. // m==0 if no sep needs to be inserted or removed
  249. //if( m == 0 )
  250. // return false;
  251. if( m != 0 )
  252. {
  253. // if a sep char needs to be inserted
  254. if( rp[m-1] != sepChar && suffixStr[0] != sepChar )
  255. {
  256. assert((m+1)<rn);
  257. if((m+1)>=rn)
  258. return false;
  259. rp[m] = sepChar;
  260. rp[m+1]= 0;
  261. ++m;
  262. }
  263. else
  264. // if a sep char needs to be removed
  265. if( rp[m-1] == sepChar && suffixStr[0] == sepChar )
  266. {
  267. rp[m-1] = 0;
  268. --m;
  269. }
  270. }
  271. assert( rn>=m && strlen(rp)+strlen(suffixStr) <= rn );
  272. strncat(rp,suffixStr,rn-m);
  273. return true;
  274. }
  275. const cmChar_t* cmFileSysVMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
  276. {
  277. cmFsRC_t rc = kOkFsRC;
  278. cmChar_t* rp = NULL;
  279. const cmChar_t* dp = NULL;
  280. unsigned n = 0;
  281. cmFs_t* p = _cmFileSysHandleToPtr(h);
  282. char pathSep = cmPathSeparatorChar[0];
  283. char extSep = '.';
  284. va_list vl_t;
  285. va_copy(vl_t,vl);
  286. assert( fn != NULL );
  287. // get prefix directory length
  288. if( dir != NULL )
  289. n += strlen(dir) + 1; // add 1 for ending sep
  290. // get file name length
  291. n += strlen(fn);
  292. // get extension length
  293. if( ext != NULL )
  294. n += strlen(ext) + 1; // add 1 for period
  295. // get length of all var args dir's
  296. while( (dp = va_arg(vl,const cmChar_t*)) != NULL )
  297. n += strlen(dp) + 1; // add 1 for ending sep
  298. // add 1 for terminating zero and allocate memory
  299. if((rp = cmLHeapAllocZ( p->heapH, n+1 )) == NULL )
  300. {
  301. rc = _cmFileSysError(p,kMemAllocErrFsRC,0,"Unable to allocate file name memory.");
  302. goto errLabel;
  303. }
  304. va_copy(vl,vl_t);
  305. rp[n] = 0;
  306. rp[0] = 0;
  307. // copy out the prefix dir
  308. if( dir != NULL )
  309. strncat(rp,dir,n-strlen(rp));
  310. // copy out ecmh of the var arg's directories
  311. while((dp = va_arg(vl,const cmChar_t*)) != NULL )
  312. if(!_cmFileSysConcat(rp,n,pathSep,dp) )
  313. {
  314. assert(0);
  315. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  316. goto errLabel;
  317. }
  318. // copy out the file name
  319. if(!_cmFileSysConcat(rp,n,pathSep,fn))
  320. {
  321. assert(0);
  322. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  323. goto errLabel;
  324. }
  325. // copy out the extension
  326. if( ext != NULL )
  327. if(!_cmFileSysConcat(rp,n,extSep,ext))
  328. {
  329. assert(0);
  330. rc = _cmFileSysError(p,kAssertFailFsRC,0,"Assert failed.");
  331. goto errLabel;
  332. }
  333. assert(strlen(rp)<=n);
  334. errLabel:
  335. if( rc != kOkFsRC && rp != NULL )
  336. cmLHeapFree(p->heapH, rp );
  337. return rp;
  338. }
  339. const cmChar_t* cmFileSysMakeFn( cmFileSysH_t h, const cmChar_t* dir, const cmChar_t* fn, const cmChar_t* ext, ... )
  340. {
  341. va_list vl;
  342. va_start(vl,ext);
  343. const cmChar_t* retPtr = cmFileSysVMakeFn(h,dir,fn,ext,vl);
  344. va_end(vl);
  345. return retPtr;
  346. }
  347. void cmFileSysFreeFn( cmFileSysH_t h, const cmChar_t* fn )
  348. {
  349. cmFs_t* p = _cmFileSysHandleToPtr(h);
  350. if( fn == NULL )
  351. return;
  352. cmLHeapFree(p->heapH, (void*)fn);
  353. }
  354. cmFsRC_t cmFileSysMkDir( cmFileSysH_t h, const cmChar_t* dir )
  355. {
  356. cmFs_t* p = _cmFileSysHandleToPtr(h);
  357. if( mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) != 0 )
  358. return _cmFileSysError( p, kMkDirFailFsRC, errno, "The attempt to create the directory '%s' failed.",dir);
  359. return kOkFsRC;
  360. }
  361. cmFsRC_t cmFileSysMkDirAll( cmFileSysH_t h, const cmChar_t* dir )
  362. {
  363. cmFsRC_t rc = kOkFsRC;
  364. cmFs_t* p = _cmFileSysHandleToPtr(h);
  365. cmChar_t** a = NULL;
  366. unsigned i;
  367. if((a = cmFileSysDirParts(h,dir)) == NULL )
  368. return _cmFileSysError(p, kInvalidDirFsRC, 0, "The directory '%s' could not be parsed.",cmStringNullGuard(dir));
  369. for(i=0; rc==kOkFsRC && a[i]!=NULL; ++i)
  370. {
  371. cmChar_t* d = cmFileSysFormDir(h,a,i+1);
  372. if( cmFileSysIsDir(h,d)==false )
  373. if((rc = cmFileSysMkDir(h,d)) != kOkFsRC )
  374. break;
  375. cmFileSysFreeDir(h,d);
  376. }
  377. cmFileSysFreeDirParts(h,a);
  378. return rc;
  379. }
  380. cmFileSysPathPart_t* cmFileSysPathParts( cmFileSysH_t h, const cmChar_t* pathStr )
  381. {
  382. int n = 0; // char's in pathStr
  383. int dn = 0; // char's in the dir part
  384. int fn = 0; // char's in the name part
  385. int en = 0; // char's in the ext part
  386. cmChar_t* cp = NULL;
  387. cmFileSysPathPart_t* rp = NULL;
  388. cmFs_t* p = _cmFileSysHandleToPtr(h);
  389. if( pathStr==NULL )
  390. return NULL;
  391. // skip leading white space
  392. while( *pathStr )
  393. {
  394. if( isspace(*pathStr ) )
  395. ++pathStr;
  396. else
  397. break;
  398. }
  399. // get the length of pathStr
  400. if((n = strlen(pathStr)) == 0 )
  401. return NULL;
  402. // remove trailing spaces
  403. for(; n>0; --n)
  404. {
  405. if( isspace(pathStr[n-1]) )
  406. --n;
  407. else
  408. break;
  409. }
  410. //
  411. if( n == 0 )
  412. return NULL;
  413. char buf[n+1];
  414. buf[n] = 0;
  415. // Get the last word (which may be a file name) from pathStr.
  416. // (pathStr must be copied into a buf because basename()
  417. // is allowed to change the values in its arg.)
  418. strncpy(buf,pathStr,n);
  419. cp = basename(buf);
  420. if( cp != NULL )
  421. {
  422. cmChar_t* ep;
  423. // does the last word have a period in it
  424. if( (ep = strchr(cp,'.')) != NULL )
  425. {
  426. *ep = 0; // end the file name at the period
  427. ++ep; // set the ext marker
  428. en = strlen(ep); // get the length of the ext
  429. }
  430. fn = strlen(cp); //get the length of the file name
  431. }
  432. // Get the directory part.
  433. // ( pathStr must be copied into a buf because dirname()
  434. // is allowed to change the values in its arg.)
  435. strncpy(buf,pathStr,n);
  436. // if the last char in pathStr[] is '/' then the whole string is a dir.
  437. // (this is not the answer that dirname() and basename() would give).
  438. if( pathStr[n-1] == cmPathSeparatorChar[0] )
  439. {
  440. fn = 0;
  441. en = 0;
  442. dn = n;
  443. cp = buf;
  444. }
  445. else
  446. {
  447. cp = dirname(buf);
  448. }
  449. if( cp != NULL )
  450. dn = strlen(cp);
  451. // get the total size of the returned memory. (add 3 for ecmh possible terminating zero)
  452. n = sizeof(cmFileSysPathPart_t) + dn + fn + en + 3;
  453. // alloc memory
  454. if((rp = (cmFileSysPathPart_t*)cmLHeapAllocZ( p->heapH, n )) == NULL )
  455. {
  456. _cmFileSysError( p, kLHeapAllocErrFsRC, 0, "Unable to allocate the file system path part record for '%s'.",pathStr);
  457. return NULL;
  458. }
  459. // set the return pointers for ecmh of the parts
  460. rp->dirStr = (const cmChar_t* )(rp + 1);
  461. rp->fnStr = rp->dirStr + dn + 1;
  462. rp->extStr = rp->fnStr + fn + 1;
  463. // if there is a directory part
  464. if( dn>0 )
  465. strcpy((cmChar_t*)rp->dirStr,cp);
  466. else
  467. rp->dirStr = NULL;
  468. if( fn || en )
  469. {
  470. // Get the trailing word again.
  471. // pathStr must be copied into a buf because basename() may
  472. // is allowed to change the values in its arg.
  473. strncpy(buf,pathStr,n);
  474. cp = basename(buf);
  475. if( cp != NULL )
  476. {
  477. cmChar_t* ep;
  478. if( (ep = strchr(cp,'.')) != NULL )
  479. {
  480. *ep = 0;
  481. ++ep;
  482. assert( strlen(ep) == en );
  483. strcpy((cmChar_t*)rp->extStr,ep);
  484. }
  485. assert( strlen(cp) == fn );
  486. if(fn)
  487. strcpy((cmChar_t*)rp->fnStr,cp);
  488. }
  489. }
  490. if( fn == 0 )
  491. rp->fnStr = NULL;
  492. if( en == 0 )
  493. rp->extStr = NULL;
  494. return rp;
  495. }
  496. void cmFileSysFreePathParts( cmFileSysH_t h, cmFileSysPathPart_t* pp )
  497. {
  498. if( pp == NULL )
  499. return;
  500. cmFs_t* p = _cmFileSysHandleToPtr(h);
  501. cmLHeapFree(p->heapH, (void*)pp );
  502. }
  503. cmChar_t** cmFileSysDirParts( cmFileSysH_t h, const cmChar_t* dirStr )
  504. {
  505. cmFs_t* p = _cmFileSysHandleToPtr(h);
  506. const cmChar_t* s = dirStr;
  507. const cmChar_t* ep = dirStr + strlen(dirStr);
  508. char pathSep = cmPathSeparatorChar[0];
  509. unsigned n = 2;
  510. unsigned i = 0;
  511. // skip leading white space or pathsep
  512. while( isspace(*s) && s < ep )
  513. ++s;
  514. // if the directory string is empty
  515. if( s >= ep )
  516. return NULL;
  517. // set the beginning of the input dirStr past any leading white space
  518. dirStr = s;
  519. // count the number of dir segments - this might overcount the
  520. // number of segments if there are multiple repeated path seperator
  521. // char's - but this is ok because 'n' is only used to determine
  522. // the size of the returned array - which will simply have some
  523. // NULL entries at the end.
  524. for(; s < ep; ++s )
  525. if( *s == pathSep )
  526. ++n;
  527. // allocate the array
  528. cmChar_t** a = cmLhAllocZ(p->heapH,cmChar_t*,n);
  529. // reset the cur location to the begin of the buf
  530. s = dirStr;
  531. // if the path starts at the root
  532. if( *s == pathSep )
  533. {
  534. a[0] = cmPathSeparatorChar; // cmPathSeparatorChar is a static string in cmGlobal.h
  535. i = 1;
  536. ++s;
  537. }
  538. for(; i<n && s<ep; ++i)
  539. {
  540. const cmChar_t* s1;
  541. if(( s1 = strchr(s,pathSep)) == NULL )
  542. s1 = ep;
  543. if( s1!=s )
  544. {
  545. unsigned sn = (s1 - s)+1;
  546. a[i] = cmLhAlloc(p->heapH,cmChar_t,sn);
  547. strncpy(a[i],s,sn-1);
  548. a[i][sn-1] = 0;
  549. }
  550. s = s1+1;
  551. }
  552. return a;
  553. }
  554. void cmFileSysFreeDirParts( cmFileSysH_t h, cmChar_t** dirStrArray )
  555. {
  556. if( dirStrArray == NULL )
  557. return;
  558. cmFs_t* p = _cmFileSysHandleToPtr(h);
  559. unsigned i;
  560. for(i=0; dirStrArray[i]!=NULL; ++i)
  561. {
  562. // cmPathSeparatorChar is statically alloc'd in cmGlobal.h
  563. if( dirStrArray[i] != cmPathSeparatorChar )
  564. cmLHeapFree(p->heapH,dirStrArray[i]);
  565. }
  566. cmLHeapFree(p->heapH, (void*)dirStrArray );
  567. }
  568. unsigned cmFileSysDirPartsCount(cmFileSysH_t h, cmChar_t** dirStrArray )
  569. {
  570. unsigned i = 0;
  571. if( dirStrArray == NULL )
  572. return 0;
  573. while(dirStrArray[i] != NULL )
  574. ++i;
  575. return i;
  576. }
  577. cmChar_t* cmFileSysFormDir( cmFileSysH_t h, cmChar_t** a, unsigned m )
  578. {
  579. cmFs_t* p = _cmFileSysHandleToPtr(h);
  580. unsigned n;
  581. unsigned i;
  582. // determine the length of the return string
  583. for(i=0,n=0; a[i]!=NULL && i<m; ++i)
  584. n += strlen(a[i]) + strlen(cmPathSeparatorChar);
  585. if( i<m && a[i] == NULL )
  586. {
  587. _cmFileSysError(p,kInvalidDirFsRC,0,"cmFileSysFormDir() cannot form a directory string from %i parts when only %i exist.",m,i);
  588. return NULL;
  589. }
  590. n += 1;
  591. // allocate the return string
  592. cmChar_t* r = cmLhAllocZ(p->heapH,cmChar_t,n);
  593. const cmChar_t* ep = r + n;
  594. // form the return string
  595. for(i=0; a[i]!=NULL && i<m; ++i)
  596. {
  597. strcat(r,a[i]);
  598. if( strcmp(a[i],cmPathSeparatorChar)!=0 && a[i+1]!=NULL )
  599. strcat(r,cmPathSeparatorChar);
  600. assert( r + strlen(r) <= ep );
  601. }
  602. return r;
  603. }
  604. void cmFileSysFreeDir( cmFileSysH_t h, const cmChar_t* dir )
  605. {
  606. if( dir == NULL )
  607. return;
  608. cmFs_t* p = _cmFileSysHandleToPtr(h);
  609. cmLHeapFree(p->heapH,(void*)dir);
  610. }
  611. typedef struct
  612. {
  613. cmFs_t* p;
  614. unsigned filterFlags;
  615. cmFileSysDirEntry_t* rp;
  616. cmChar_t* dataPtr;
  617. cmChar_t* endPtr;
  618. unsigned entryCnt;
  619. unsigned entryIdx;
  620. unsigned dataByteCnt;
  621. unsigned passIdx;
  622. } cmFileSysDeRecd_t;
  623. cmFsRC_t _cmFileSysDirGetEntries( cmFileSysDeRecd_t* drp, const cmChar_t* dirStr )
  624. {
  625. cmFsRC_t rc = kOkFsRC;
  626. DIR* dirp = NULL;
  627. struct dirent* dp = NULL;
  628. char curDirPtr[] = "./";
  629. unsigned dn = 0;
  630. if( dirStr == NULL || strlen(dirStr) == 0 )
  631. dirStr = curDirPtr;
  632. if( _cmFileSysIsDir(drp->p,dirStr) == false )
  633. return rc;
  634. unsigned fnCharCnt= strlen(dirStr) + PATH_MAX;
  635. char fn[ fnCharCnt + 1 ];
  636. // copy the directory into fn[] ...
  637. fn[0] =0;
  638. fn[fnCharCnt] = 0;
  639. strcpy(fn,dirStr);
  640. assert( strlen(fn)+2 < fnCharCnt );
  641. // ... and be sure that it is terminated with a path sep char
  642. if( fn[ strlen(fn)-1 ] != cmPathSeparatorChar[0] )
  643. strcat(fn,cmPathSeparatorChar);
  644. // file names will be appended to the path at this location
  645. unsigned fni = strlen(fn);
  646. // open the directory
  647. if((dirp = opendir(dirStr)) == NULL)
  648. {
  649. rc = _cmFileSysError(drp->p,kOpenDirFailFsRC,errno,"Unable to open the directory:'%s'.",dirStr);
  650. goto errLabel;
  651. }
  652. // get the next directory entry
  653. while((dp = readdir(dirp)) != NULL )
  654. {
  655. // validate d_name
  656. if( (dp->d_name != NULL) && ((dn = strlen(dp->d_name)) > 0) )
  657. {
  658. unsigned flags = 0;
  659. // handle cases where d_name begins with '.'
  660. if( dp->d_name[0] == '.' )
  661. {
  662. if( strcmp(dp->d_name,".") == 0 )
  663. {
  664. if( cmIsFlag(drp->filterFlags,kCurDirFsFl) == false )
  665. continue;
  666. flags |= kCurDirFsFl;
  667. }
  668. if( strcmp(dp->d_name,"..") == 0 )
  669. {
  670. if( cmIsFlag(drp->filterFlags,kParentDirFsFl) == false )
  671. continue;
  672. flags |= kParentDirFsFl;
  673. }
  674. if( flags == 0 )
  675. {
  676. if( cmIsFlag(drp->filterFlags,kInvisibleFsFl) == false )
  677. continue;
  678. flags |= kInvisibleFsFl;
  679. }
  680. }
  681. fn[fni] = 0;
  682. strncat( fn, dp->d_name, fnCharCnt-fni );
  683. unsigned fnN = strlen(fn);
  684. // if the filename is too long for the buffer
  685. if( fnN > fnCharCnt )
  686. {
  687. rc = _cmFileSysError(drp->p, kFnTooLongFsRC, errno, "The directory entry:'%s' was too long to be processed.",dp->d_name);
  688. goto errLabel;
  689. }
  690. // is the entry a file
  691. if( _cmFileSysIsFile(drp->p,fn) )
  692. {
  693. if( cmIsFlag(drp->filterFlags,kFileFsFl)==false )
  694. continue;
  695. flags |= kFileFsFl;
  696. }
  697. else
  698. {
  699. // is the entry a dir
  700. if( _cmFileSysIsDir(drp->p,fn) )
  701. {
  702. if( cmIsFlag(drp->filterFlags,kDirFsFl) == false)
  703. continue;
  704. flags |= kDirFsFl;
  705. if( cmIsFlag(drp->filterFlags,kRecurseFsFl) )
  706. if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC )
  707. goto errLabel;
  708. }
  709. else
  710. {
  711. if( _cmFileSysIsLink(drp->p,fn) )
  712. {
  713. if( cmIsFlag(drp->filterFlags,kLinkFsFl) == false )
  714. continue;
  715. flags |= kLinkFsFl;
  716. if( cmIsFlag(drp->filterFlags,kRecurseLinksFsFl) )
  717. if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC )
  718. goto errLabel;
  719. }
  720. else
  721. {
  722. continue;
  723. }
  724. }
  725. }
  726. //assert(flags != 0);
  727. if( drp->passIdx == 0 )
  728. {
  729. ++drp->entryCnt;
  730. // add 1 for the name terminating zero
  731. drp->dataByteCnt += sizeof(cmFileSysDirEntry_t) + 1;
  732. if( cmIsFlag(drp->filterFlags,kFullPathFsFl) )
  733. drp->dataByteCnt += fnN;
  734. else
  735. drp->dataByteCnt += dn;
  736. }
  737. else
  738. {
  739. assert( drp->passIdx == 1 );
  740. assert( drp->entryIdx < drp->entryCnt );
  741. unsigned n = 0;
  742. if( cmIsFlag(drp->filterFlags,kFullPathFsFl) )
  743. {
  744. n = fnN+1;
  745. assert( drp->dataPtr + n <= drp->endPtr );
  746. strcpy(drp->dataPtr,fn);
  747. }
  748. else
  749. {
  750. n = dn+1;
  751. assert( drp->dataPtr + n <= drp->endPtr );
  752. strcpy(drp->dataPtr,dp->d_name);
  753. }
  754. drp->rp[ drp->entryIdx ].flags = flags;
  755. drp->rp[ drp->entryIdx ].name = drp->dataPtr;
  756. drp->dataPtr += n;
  757. assert( drp->dataPtr <= drp->endPtr);
  758. ++drp->entryIdx;
  759. }
  760. }
  761. }
  762. errLabel:
  763. if( dirp != NULL )
  764. closedir(dirp);
  765. return rc;
  766. }
  767. cmFileSysDirEntry_t* cmFileSysDirEntries( cmFileSysH_t h, const cmChar_t* dirStr, unsigned filterFlags, unsigned* dirEntryCntPtr )
  768. {
  769. cmFsRC_t rc = kOkFsRC;
  770. cmFileSysDeRecd_t r;
  771. memset(&r,0,sizeof(r));
  772. r.p = _cmFileSysHandleToPtr(h);
  773. r.filterFlags = filterFlags;
  774. assert( dirEntryCntPtr != NULL );
  775. *dirEntryCntPtr = 0;
  776. for(r.passIdx=0; r.passIdx<2; ++r.passIdx)
  777. {
  778. if((rc = _cmFileSysDirGetEntries( &r, dirStr )) != kOkFsRC )
  779. goto errLabel;
  780. if( r.passIdx == 0 && r.dataByteCnt>0 )
  781. {
  782. // allocate memory to hold the return values
  783. if(( r.rp = (cmFileSysDirEntry_t *)cmLHeapAllocZ( r.p->heapH, r.dataByteCnt )) == NULL )
  784. {
  785. rc= _cmFileSysError( r.p, kMemAllocErrFsRC, 0, "Unable to allocate %i bytes of dir entry memory.",r.dataByteCnt);
  786. goto errLabel;
  787. }
  788. r.dataPtr = (cmChar_t*)(r.rp + r.entryCnt);
  789. r.endPtr = ((cmChar_t*)r.rp) + r.dataByteCnt;
  790. }
  791. }
  792. errLabel:
  793. if( rc == kOkFsRC )
  794. {
  795. assert( r.entryIdx == r.entryCnt );
  796. *dirEntryCntPtr = r.entryCnt;
  797. }
  798. else
  799. {
  800. if( r.rp != NULL )
  801. cmLHeapFree(r.p->heapH,r.rp);
  802. r.rp = NULL;
  803. }
  804. return r.rp;
  805. }
  806. void cmFileSysDirFreeEntries( cmFileSysH_t h, cmFileSysDirEntry_t* dp )
  807. {
  808. cmFs_t* p = _cmFileSysHandleToPtr(h);
  809. if( dp != NULL )
  810. cmLHeapFree(p->heapH,dp);
  811. }
  812. cmFsRC_t cmFileSysErrorCode( cmFileSysH_t h )
  813. {
  814. cmFs_t* p = _cmFileSysHandleToPtr(h);
  815. return cmErrLastRC(&p->err);
  816. }
  817. //
  818. //======================================================================================================
  819. // Begin global versions
  820. //
  821. cmFileSysH_t _cmFsH = { NULL };
  822. cmFsRC_t cmFsInitialize( cmCtx_t* ctx, const cmChar_t* appNameStr )
  823. { return cmFileSysInitialize(&_cmFsH,ctx,appNameStr); }
  824. cmFsRC_t cmFsFinalize()
  825. { return cmFileSysFinalize(&_cmFsH); }
  826. const cmChar_t* cmFsAppName()
  827. { return cmFileSysAppName(_cmFsH); }
  828. const cmChar_t* cmFsPrefsDir()
  829. { return cmFileSysPrefsDir(_cmFsH); }
  830. const cmChar_t* cmFsRsrcDir()
  831. { return cmFileSysRsrcDir(_cmFsH); }
  832. const cmChar_t* cmFsUserDir()
  833. { return cmFileSysUserDir(_cmFsH); }
  834. bool cmFsIsDir( const cmChar_t* dirStr )
  835. { return cmFileSysIsDir(_cmFsH,dirStr); }
  836. bool cmFsIsFile( const cmChar_t* fnStr )
  837. { return cmFileSysIsFile(_cmFsH,fnStr); }
  838. bool cmFsIsLink( const cmChar_t* fnStr )
  839. { return cmFileSysIsLink(_cmFsH,fnStr); }
  840. const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
  841. { return cmFileSysVMakeFn(_cmFsH,dirPrefix,fn,ext,vl); }
  842. const cmChar_t* cmFsMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... )
  843. {
  844. va_list vl;
  845. va_start(vl,ext);
  846. const cmChar_t* retPtr = cmFsVMakeFn(dirPrefix,fn,ext,vl);
  847. va_end(vl);
  848. return retPtr;
  849. }
  850. void cmFsFreeFn( const cmChar_t* fn )
  851. { cmFileSysFreeFn(_cmFsH, fn); }
  852. cmFsRC_t cmFsMkDir( const cmChar_t* dir )
  853. { return cmFileSysMkDir(_cmFsH,dir); }
  854. cmFsRC_t cmFsMkDirAll( const cmChar_t* dir )
  855. { return cmFileSysMkDirAll(_cmFsH,dir); }
  856. cmFileSysPathPart_t* cmFsPathParts( const cmChar_t* pathNameStr )
  857. { return cmFileSysPathParts(_cmFsH,pathNameStr); }
  858. void cmFsFreePathParts( cmFileSysPathPart_t* p )
  859. { cmFileSysFreePathParts(_cmFsH,p); }
  860. cmChar_t** cmFsDirParts( const cmChar_t* dirStr )
  861. { return cmFileSysDirParts(_cmFsH,dirStr); }
  862. void cmFsFreeDirParts( cmChar_t** dirStrArray )
  863. { cmFileSysFreeDirParts(_cmFsH,dirStrArray); }
  864. unsigned cmFsDirPartsCount( cmChar_t** dirStrArray )
  865. { return cmFileSysDirPartsCount(_cmFsH, dirStrArray); }
  866. cmChar_t* cmFsFormDir( cmChar_t** dirStrArray, unsigned n )
  867. { return cmFileSysFormDir(_cmFsH,dirStrArray,n); }
  868. void cmFsFreeDir( const cmChar_t* dir )
  869. { cmFileSysFreeDir(_cmFsH,dir); }
  870. cmFileSysDirEntry_t* cmFsDirEntries( const cmChar_t* dirStr, unsigned includeFlags, unsigned* dirEntryCntPtr )
  871. { return cmFileSysDirEntries(_cmFsH,dirStr,includeFlags,dirEntryCntPtr); }
  872. void cmFsDirFreeEntries( cmFileSysDirEntry_t* p )
  873. { cmFileSysDirFreeEntries(_cmFsH,p); }
  874. cmFsRC_t cmFsErrorCode()
  875. { return cmFileSysErrorCode(_cmFsH); }
  876. // end global version
  877. //======================================================================================================
  878. //
  879. //{ { label:cmFileSysEx }
  880. //(
  881. //
  882. // cmFileSysTest() function gives usage and testing examples
  883. // for some of the cmFileSys functions.
  884. // Note that the HOME_DIR macro should be set to your true
  885. // $HOME directroy and SRC_DIR should be set to any existing
  886. // and accessible directory.
  887. //
  888. //)
  889. //(
  890. #if defined(OS_OSX)
  891. #define HOME_DIR "/Users/kevin"
  892. #else
  893. #define HOME_DIR "/home/kevin"
  894. #endif
  895. #define SRC_DIR HOME_DIR"/src"
  896. void _cmFileSysTestFnParser(
  897. cmFileSysH_t h,
  898. cmRpt_t* rpt,
  899. const cmChar_t* fn );
  900. cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
  901. {
  902. // The global heap manager must have been initialized
  903. // via cmMdInitialize() prior to running this function.
  904. cmFsRC_t rc = kOkFsRC;
  905. cmFileSysH_t h = cmFileSysNullHandle;
  906. const char dir0[] = SRC_DIR;
  907. const char dir1[] = HOME_DIR"/blah";
  908. const char file0[] = HOME_DIR"/.emacs";
  909. const char file1[] = HOME_DIR"/blah.txt";
  910. const char not[] = " not ";
  911. const char e[] = " ";
  912. bool fl = false;
  913. const cmChar_t* fn = NULL;
  914. // Initialize the file system.
  915. if((rc = cmFileSysInitialize(&h,ctx,"fs_test")) != kOkFsRC )
  916. return rc;
  917. //----------------------------------------------------------
  918. // Print the standard directories
  919. //
  920. printf("Prefs Dir:%s\n",cmFsPrefsDir());
  921. printf("Rsrc Dir: %s\n",cmFsRsrcDir());
  922. printf("User Dir: %s\n",cmFsUserDir());
  923. //----------------------------------------------------------
  924. // Run the file system type checker
  925. //
  926. fl = cmFileSysIsDir(h,dir0);
  927. printf("'%s' is%sa directory.\n",dir0, (fl ? e : not));
  928. fl = cmFileSysIsDir(h,dir1);
  929. printf("'%s' is%sa directory.\n",dir1, (fl ? e : not));
  930. fl = cmFileSysIsFile(h,file0);
  931. printf("'%s' is%sa file.\n",file0, (fl ? e : not));
  932. fl = cmFileSysIsFile(h,file1);
  933. printf("'%s' is%sa file.\n",file1, (fl ? e : not));
  934. //----------------------------------------------------------
  935. // Test the file name creation functions
  936. //
  937. if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
  938. "c","src","cm","src",NULL)) != NULL)
  939. {
  940. printf("File:'%s'\n",fn);
  941. }
  942. cmFileSysFreeFn(h,fn);
  943. if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
  944. ".c","/src/","/cm/","/src/",NULL)) != NULL )
  945. {
  946. printf("File:'%s'\n",fn);
  947. }
  948. cmFileSysFreeFn(h,fn);
  949. //----------------------------------------------------------
  950. // Test the file name parsing functions
  951. //
  952. _cmFileSysTestFnParser(h,&ctx->rpt,
  953. HOME_DIR"/src/cm/src/cmFileSys.c");
  954. _cmFileSysTestFnParser(h,&ctx->rpt,
  955. HOME_DIR"/src/cm/src/cmFileSys");
  956. _cmFileSysTestFnParser(h,&ctx->rpt,
  957. HOME_DIR"/src/cm/src/cmFileSys/");
  958. _cmFileSysTestFnParser(h,&ctx->rpt,"cmFileSys.c");
  959. _cmFileSysTestFnParser(h,&ctx->rpt,"/");
  960. _cmFileSysTestFnParser(h,&ctx->rpt," ");
  961. //----------------------------------------------------------
  962. // Test the directory tree walking routines.
  963. //
  964. cmRptPrintf(&ctx->rpt,"Dir Entry Test:\n");
  965. cmFileSysDirEntry_t* dep;
  966. unsigned dirEntCnt;
  967. unsigned filterFlags = kDirFsFl
  968. | kFileFsFl
  969. | kRecurseFsFl
  970. | kFullPathFsFl;
  971. if((dep = cmFileSysDirEntries(h,SRC_DIR"/doc",filterFlags,
  972. &dirEntCnt)) != NULL)
  973. {
  974. unsigned i;
  975. for(i=0; i<dirEntCnt; ++i)
  976. cmRptPrintf(&ctx->rpt,"%s\n",dep[i].name);
  977. cmFileSysDirFreeEntries(h,dep);
  978. }
  979. //----------------------------------------------------------
  980. // Test the directory parsing/building routines.
  981. //
  982. cmRptPrintf(&ctx->rpt,"Dir Parsing routings:\n");
  983. cmChar_t** a;
  984. unsigned j;
  985. for(j=0; j<2; ++j)
  986. {
  987. const cmChar_t* dstr = dir0 + j;
  988. if((a = cmFileSysDirParts(h,dstr)) == NULL)
  989. cmRptPrint(&ctx->rpt,"cmFileSysDirParts() failed.\n");
  990. else
  991. {
  992. unsigned i;
  993. cmRptPrintf(&ctx->rpt,"Input:%s\n",dstr);
  994. for(i=0; a[i]!=NULL; ++i)
  995. cmRptPrintf(&ctx->rpt,"%i : %s\n",i,a[i]);
  996. cmChar_t* d;
  997. if((d = cmFileSysFormDir(h,a,
  998. cmFileSysDirPartsCount(h,a))) != NULL )
  999. {
  1000. cmRptPrintf(&ctx->rpt,"Reformed:%s\n",d);
  1001. }
  1002. cmFileSysFreeDirParts(h,a);
  1003. }
  1004. }
  1005. //----------------------------------------------------------
  1006. // Test the extended mkdir routine.
  1007. //
  1008. if( cmFileSysMkDirAll(h, HOME_DIR"/temp/doc/doc" )!=kOkFsRC )
  1009. {
  1010. cmRptPrint(&ctx->rpt,"cmFileSysMkDirAll() failed.\n");
  1011. }
  1012. // finalize the file system
  1013. if((rc = cmFileSysFinalize(&h)) != kOkFsRC )
  1014. return rc;
  1015. cmRptPrintf(&ctx->rpt,"File Test done\n");
  1016. return rc;
  1017. }
  1018. // Parse a file name and print the results.
  1019. // Called by cmFileSysTest().
  1020. void _cmFileSysTestFnParser(
  1021. cmFileSysH_t h,
  1022. cmRpt_t* rpt,
  1023. const cmChar_t* fn )
  1024. {
  1025. cmFileSysPathPart_t* pp;
  1026. cmRptPrintf(rpt,"Fn Parse Test:%s\n",fn);
  1027. if((pp = cmFileSysPathParts(h,fn)) != NULL )
  1028. {
  1029. if(pp->dirStr != NULL)
  1030. cmRptPrintf(rpt,"Dir:%s\n",pp->dirStr);
  1031. if(pp->fnStr != NULL)
  1032. cmRptPrintf(rpt,"Fn:%s\n",pp->fnStr);
  1033. if(pp->extStr != NULL )
  1034. cmRptPrintf(rpt,"Ext:%s\n",pp->extStr);
  1035. cmFileSysFreePathParts(h,pp);
  1036. }
  1037. cmRptPrintf(rpt,"\n");
  1038. }
  1039. //)
  1040. //}