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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  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 a link
  691. if( _cmFileSysIsLink(drp->p,fn) )
  692. {
  693. if( cmIsFlag(drp->filterFlags,kLinkFsFl) == false )
  694. continue;
  695. flags |= kLinkFsFl;
  696. if( cmIsFlag(drp->filterFlags,kRecurseLinksFsFl) )
  697. if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC )
  698. goto errLabel;
  699. }
  700. else
  701. {
  702. // is the entry a file
  703. if( _cmFileSysIsFile(drp->p,fn) )
  704. {
  705. if( cmIsFlag(drp->filterFlags,kFileFsFl)==false )
  706. continue;
  707. flags |= kFileFsFl;
  708. }
  709. else
  710. {
  711. // is the entry a dir
  712. if( _cmFileSysIsDir(drp->p,fn) )
  713. {
  714. if( cmIsFlag(drp->filterFlags,kDirFsFl) == false)
  715. continue;
  716. flags |= kDirFsFl;
  717. if( cmIsFlag(drp->filterFlags,kRecurseFsFl) )
  718. if((rc = _cmFileSysDirGetEntries(drp,fn)) != kOkFsRC )
  719. goto errLabel;
  720. }
  721. else
  722. {
  723. continue;
  724. }
  725. }
  726. }
  727. //assert(flags != 0);
  728. if( drp->passIdx == 0 )
  729. {
  730. ++drp->entryCnt;
  731. // add 1 for the name terminating zero
  732. drp->dataByteCnt += sizeof(cmFileSysDirEntry_t) + 1;
  733. if( cmIsFlag(drp->filterFlags,kFullPathFsFl) )
  734. drp->dataByteCnt += fnN;
  735. else
  736. drp->dataByteCnt += dn;
  737. }
  738. else
  739. {
  740. assert( drp->passIdx == 1 );
  741. assert( drp->entryIdx < drp->entryCnt );
  742. unsigned n = 0;
  743. if( cmIsFlag(drp->filterFlags,kFullPathFsFl) )
  744. {
  745. n = fnN+1;
  746. assert( drp->dataPtr + n <= drp->endPtr );
  747. strcpy(drp->dataPtr,fn);
  748. }
  749. else
  750. {
  751. n = dn+1;
  752. assert( drp->dataPtr + n <= drp->endPtr );
  753. strcpy(drp->dataPtr,dp->d_name);
  754. }
  755. drp->rp[ drp->entryIdx ].flags = flags;
  756. drp->rp[ drp->entryIdx ].name = drp->dataPtr;
  757. drp->dataPtr += n;
  758. assert( drp->dataPtr <= drp->endPtr);
  759. ++drp->entryIdx;
  760. }
  761. }
  762. }
  763. errLabel:
  764. if( dirp != NULL )
  765. closedir(dirp);
  766. return rc;
  767. }
  768. cmFileSysDirEntry_t* cmFileSysDirEntries( cmFileSysH_t h, const cmChar_t* dirStr, unsigned filterFlags, unsigned* dirEntryCntPtr )
  769. {
  770. cmFsRC_t rc = kOkFsRC;
  771. cmFileSysDeRecd_t r;
  772. memset(&r,0,sizeof(r));
  773. r.p = _cmFileSysHandleToPtr(h);
  774. r.filterFlags = filterFlags;
  775. assert( dirEntryCntPtr != NULL );
  776. *dirEntryCntPtr = 0;
  777. for(r.passIdx=0; r.passIdx<2; ++r.passIdx)
  778. {
  779. if((rc = _cmFileSysDirGetEntries( &r, dirStr )) != kOkFsRC )
  780. goto errLabel;
  781. if( r.passIdx == 0 && r.dataByteCnt>0 )
  782. {
  783. // allocate memory to hold the return values
  784. if(( r.rp = (cmFileSysDirEntry_t *)cmLHeapAllocZ( r.p->heapH, r.dataByteCnt )) == NULL )
  785. {
  786. rc= _cmFileSysError( r.p, kMemAllocErrFsRC, 0, "Unable to allocate %i bytes of dir entry memory.",r.dataByteCnt);
  787. goto errLabel;
  788. }
  789. r.dataPtr = (cmChar_t*)(r.rp + r.entryCnt);
  790. r.endPtr = ((cmChar_t*)r.rp) + r.dataByteCnt;
  791. }
  792. }
  793. errLabel:
  794. if( rc == kOkFsRC )
  795. {
  796. assert( r.entryIdx == r.entryCnt );
  797. *dirEntryCntPtr = r.entryCnt;
  798. }
  799. else
  800. {
  801. if( r.rp != NULL )
  802. cmLHeapFree(r.p->heapH,r.rp);
  803. r.rp = NULL;
  804. }
  805. return r.rp;
  806. }
  807. void cmFileSysDirFreeEntries( cmFileSysH_t h, cmFileSysDirEntry_t* dp )
  808. {
  809. cmFs_t* p = _cmFileSysHandleToPtr(h);
  810. if( dp != NULL )
  811. cmLHeapFree(p->heapH,dp);
  812. }
  813. cmFsRC_t cmFileSysErrorCode( cmFileSysH_t h )
  814. {
  815. cmFs_t* p = _cmFileSysHandleToPtr(h);
  816. return cmErrLastRC(&p->err);
  817. }
  818. //
  819. //======================================================================================================
  820. // Begin global versions
  821. //
  822. cmFileSysH_t _cmFsH = { NULL };
  823. cmFsRC_t cmFsInitialize( cmCtx_t* ctx, const cmChar_t* appNameStr )
  824. { return cmFileSysInitialize(&_cmFsH,ctx,appNameStr); }
  825. cmFsRC_t cmFsFinalize()
  826. { return cmFileSysFinalize(&_cmFsH); }
  827. const cmChar_t* cmFsAppName()
  828. { return cmFileSysAppName(_cmFsH); }
  829. const cmChar_t* cmFsPrefsDir()
  830. { return cmFileSysPrefsDir(_cmFsH); }
  831. const cmChar_t* cmFsRsrcDir()
  832. { return cmFileSysRsrcDir(_cmFsH); }
  833. const cmChar_t* cmFsUserDir()
  834. { return cmFileSysUserDir(_cmFsH); }
  835. bool cmFsIsDir( const cmChar_t* dirStr )
  836. { return cmFileSysIsDir(_cmFsH,dirStr); }
  837. bool cmFsIsFile( const cmChar_t* fnStr )
  838. { return cmFileSysIsFile(_cmFsH,fnStr); }
  839. bool cmFsIsLink( const cmChar_t* fnStr )
  840. { return cmFileSysIsLink(_cmFsH,fnStr); }
  841. const cmChar_t* cmFsVMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, va_list vl )
  842. { return cmFileSysVMakeFn(_cmFsH,dirPrefix,fn,ext,vl); }
  843. const cmChar_t* cmFsMakeFn( const cmChar_t* dirPrefix, const cmChar_t* fn, const cmChar_t* ext, ... )
  844. {
  845. va_list vl;
  846. va_start(vl,ext);
  847. const cmChar_t* retPtr = cmFsVMakeFn(dirPrefix,fn,ext,vl);
  848. va_end(vl);
  849. return retPtr;
  850. }
  851. void cmFsFreeFn( const cmChar_t* fn )
  852. { cmFileSysFreeFn(_cmFsH, fn); }
  853. cmFsRC_t cmFsMkDir( const cmChar_t* dir )
  854. { return cmFileSysMkDir(_cmFsH,dir); }
  855. cmFsRC_t cmFsMkDirAll( const cmChar_t* dir )
  856. { return cmFileSysMkDirAll(_cmFsH,dir); }
  857. cmFileSysPathPart_t* cmFsPathParts( const cmChar_t* pathNameStr )
  858. { return cmFileSysPathParts(_cmFsH,pathNameStr); }
  859. void cmFsFreePathParts( cmFileSysPathPart_t* p )
  860. { cmFileSysFreePathParts(_cmFsH,p); }
  861. cmChar_t** cmFsDirParts( const cmChar_t* dirStr )
  862. { return cmFileSysDirParts(_cmFsH,dirStr); }
  863. void cmFsFreeDirParts( cmChar_t** dirStrArray )
  864. { cmFileSysFreeDirParts(_cmFsH,dirStrArray); }
  865. unsigned cmFsDirPartsCount( cmChar_t** dirStrArray )
  866. { return cmFileSysDirPartsCount(_cmFsH, dirStrArray); }
  867. cmChar_t* cmFsFormDir( cmChar_t** dirStrArray, unsigned n )
  868. { return cmFileSysFormDir(_cmFsH,dirStrArray,n); }
  869. void cmFsFreeDir( const cmChar_t* dir )
  870. { cmFileSysFreeDir(_cmFsH,dir); }
  871. cmFileSysDirEntry_t* cmFsDirEntries( const cmChar_t* dirStr, unsigned includeFlags, unsigned* dirEntryCntPtr )
  872. { return cmFileSysDirEntries(_cmFsH,dirStr,includeFlags,dirEntryCntPtr); }
  873. void cmFsDirFreeEntries( cmFileSysDirEntry_t* p )
  874. { cmFileSysDirFreeEntries(_cmFsH,p); }
  875. cmFsRC_t cmFsErrorCode()
  876. { return cmFileSysErrorCode(_cmFsH); }
  877. // end global version
  878. //======================================================================================================
  879. //
  880. //{ { label:cmFileSysEx }
  881. //(
  882. //
  883. // cmFileSysTest() function gives usage and testing examples
  884. // for some of the cmFileSys functions.
  885. // Note that the HOME_DIR macro should be set to your true
  886. // $HOME directroy and SRC_DIR should be set to any existing
  887. // and accessible directory.
  888. //
  889. //)
  890. //(
  891. #if defined(OS_OSX)
  892. #define HOME_DIR "/Users/kevin"
  893. #else
  894. #define HOME_DIR "/home/kevin"
  895. #endif
  896. #define SRC_DIR HOME_DIR"/src"
  897. void _cmFileSysTestFnParser(
  898. cmFileSysH_t h,
  899. cmRpt_t* rpt,
  900. const cmChar_t* fn );
  901. cmFsRC_t cmFileSysTest( cmCtx_t* ctx )
  902. {
  903. // The global heap manager must have been initialized
  904. // via cmMdInitialize() prior to running this function.
  905. cmFsRC_t rc = kOkFsRC;
  906. cmFileSysH_t h = cmFileSysNullHandle;
  907. const char dir0[] = SRC_DIR;
  908. const char dir1[] = HOME_DIR"/blah";
  909. const char file0[] = HOME_DIR"/.emacs";
  910. const char file1[] = HOME_DIR"/blah.txt";
  911. const char not[] = " not ";
  912. const char e[] = " ";
  913. bool fl = false;
  914. const cmChar_t* fn = NULL;
  915. // Initialize the file system.
  916. if((rc = cmFileSysInitialize(&h,ctx,"fs_test")) != kOkFsRC )
  917. return rc;
  918. //----------------------------------------------------------
  919. // Print the standard directories
  920. //
  921. printf("Prefs Dir:%s\n",cmFsPrefsDir());
  922. printf("Rsrc Dir: %s\n",cmFsRsrcDir());
  923. printf("User Dir: %s\n",cmFsUserDir());
  924. //----------------------------------------------------------
  925. // Run the file system type checker
  926. //
  927. fl = cmFileSysIsDir(h,dir0);
  928. printf("'%s' is%sa directory.\n",dir0, (fl ? e : not));
  929. fl = cmFileSysIsDir(h,dir1);
  930. printf("'%s' is%sa directory.\n",dir1, (fl ? e : not));
  931. fl = cmFileSysIsFile(h,file0);
  932. printf("'%s' is%sa file.\n",file0, (fl ? e : not));
  933. fl = cmFileSysIsFile(h,file1);
  934. printf("'%s' is%sa file.\n",file1, (fl ? e : not));
  935. //----------------------------------------------------------
  936. // Test the file name creation functions
  937. //
  938. if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
  939. "c","src","cm","src",NULL)) != NULL)
  940. {
  941. printf("File:'%s'\n",fn);
  942. }
  943. cmFileSysFreeFn(h,fn);
  944. if((fn = cmFileSysMakeFn(h,HOME_DIR,"cmFileSys",
  945. ".c","/src/","/cm/","/src/",NULL)) != NULL )
  946. {
  947. printf("File:'%s'\n",fn);
  948. }
  949. cmFileSysFreeFn(h,fn);
  950. //----------------------------------------------------------
  951. // Test the file name parsing functions
  952. //
  953. _cmFileSysTestFnParser(h,&ctx->rpt,
  954. HOME_DIR"/src/cm/src/cmFileSys.c");
  955. _cmFileSysTestFnParser(h,&ctx->rpt,
  956. HOME_DIR"/src/cm/src/cmFileSys");
  957. _cmFileSysTestFnParser(h,&ctx->rpt,
  958. HOME_DIR"/src/cm/src/cmFileSys/");
  959. _cmFileSysTestFnParser(h,&ctx->rpt,"cmFileSys.c");
  960. _cmFileSysTestFnParser(h,&ctx->rpt,"/");
  961. _cmFileSysTestFnParser(h,&ctx->rpt," ");
  962. //----------------------------------------------------------
  963. // Test the directory tree walking routines.
  964. //
  965. cmRptPrintf(&ctx->rpt,"Dir Entry Test:\n");
  966. cmFileSysDirEntry_t* dep;
  967. unsigned dirEntCnt;
  968. unsigned filterFlags = kDirFsFl
  969. | kFileFsFl
  970. | kRecurseFsFl
  971. | kFullPathFsFl;
  972. if((dep = cmFileSysDirEntries(h,SRC_DIR"/doc",filterFlags,
  973. &dirEntCnt)) != NULL)
  974. {
  975. unsigned i;
  976. for(i=0; i<dirEntCnt; ++i)
  977. cmRptPrintf(&ctx->rpt,"%s\n",dep[i].name);
  978. cmFileSysDirFreeEntries(h,dep);
  979. }
  980. //----------------------------------------------------------
  981. // Test the directory parsing/building routines.
  982. //
  983. cmRptPrintf(&ctx->rpt,"Dir Parsing routings:\n");
  984. cmChar_t** a;
  985. unsigned j;
  986. for(j=0; j<2; ++j)
  987. {
  988. const cmChar_t* dstr = dir0 + j;
  989. if((a = cmFileSysDirParts(h,dstr)) == NULL)
  990. cmRptPrint(&ctx->rpt,"cmFileSysDirParts() failed.\n");
  991. else
  992. {
  993. unsigned i;
  994. cmRptPrintf(&ctx->rpt,"Input:%s\n",dstr);
  995. for(i=0; a[i]!=NULL; ++i)
  996. cmRptPrintf(&ctx->rpt,"%i : %s\n",i,a[i]);
  997. cmChar_t* d;
  998. if((d = cmFileSysFormDir(h,a,
  999. cmFileSysDirPartsCount(h,a))) != NULL )
  1000. {
  1001. cmRptPrintf(&ctx->rpt,"Reformed:%s\n",d);
  1002. }
  1003. cmFileSysFreeDirParts(h,a);
  1004. }
  1005. }
  1006. //----------------------------------------------------------
  1007. // Test the extended mkdir routine.
  1008. //
  1009. if( cmFileSysMkDirAll(h, HOME_DIR"/temp/doc/doc" )!=kOkFsRC )
  1010. {
  1011. cmRptPrint(&ctx->rpt,"cmFileSysMkDirAll() failed.\n");
  1012. }
  1013. // finalize the file system
  1014. if((rc = cmFileSysFinalize(&h)) != kOkFsRC )
  1015. return rc;
  1016. cmRptPrintf(&ctx->rpt,"File Test done\n");
  1017. return rc;
  1018. }
  1019. // Parse a file name and print the results.
  1020. // Called by cmFileSysTest().
  1021. void _cmFileSysTestFnParser(
  1022. cmFileSysH_t h,
  1023. cmRpt_t* rpt,
  1024. const cmChar_t* fn )
  1025. {
  1026. cmFileSysPathPart_t* pp;
  1027. cmRptPrintf(rpt,"Fn Parse Test:%s\n",fn);
  1028. if((pp = cmFileSysPathParts(h,fn)) != NULL )
  1029. {
  1030. if(pp->dirStr != NULL)
  1031. cmRptPrintf(rpt,"Dir:%s\n",pp->dirStr);
  1032. if(pp->fnStr != NULL)
  1033. cmRptPrintf(rpt,"Fn:%s\n",pp->fnStr);
  1034. if(pp->extStr != NULL )
  1035. cmRptPrintf(rpt,"Ext:%s\n",pp->extStr);
  1036. cmFileSysFreePathParts(h,pp);
  1037. }
  1038. cmRptPrintf(rpt,"\n");
  1039. }
  1040. //)
  1041. //}