sq_fs.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  1. /*
  2. ** LuaFileSystem
  3. ** Copyright Kepler Project 2003 (http://www.keplerproject.org/luafilesystem)
  4. **
  5. ** File system manipulation library.
  6. ** This library offers these functions:
  7. ** lfs.attributes (filepath [, attributename])
  8. ** lfs.chdir (path)
  9. ** lfs.currentdir ()
  10. ** lfs.dir (path)
  11. ** lfs.lock (fh, mode)
  12. ** lfs.lock_dir (path)
  13. ** lfs.mkdir (path)
  14. ** lfs.rmdir (path)
  15. ** lfs.setmode (filepath, mode)
  16. ** lfs.symlinkattributes (filepath [, attributename]) -- thanks to Sam Roberts
  17. ** lfs.touch (filepath [, atime [, mtime]])
  18. ** lfs.unlock (fh)
  19. **
  20. ** $Id: lfs.c,v 1.61 2009/07/04 02:10:16 mascarenhas Exp $
  21. *
  22. * Ported to Squirrel by Domingo Alvarez Duarte
  23. */
  24. #ifndef _WIN32
  25. #ifndef _AIX
  26. #define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */
  27. #else
  28. #define _LARGE_FILES 1 /* AIX */
  29. #endif
  30. #endif
  31. #define _LARGEFILE64_SOURCE
  32. #ifndef _WIN32_WCE
  33. #include <errno.h>
  34. #endif
  35. #include <stdio.h>
  36. #include <string.h>
  37. #include <stdlib.h>
  38. #include <time.h>
  39. #include <sys/stat.h>
  40. #if defined(_WIN32) || defined(_WIN32_WCE)
  41. #ifdef _WIN32_WCE
  42. #include "celibc.h"
  43. #else
  44. #include <direct.h>
  45. #endif
  46. #include <windows.h>
  47. #include <io.h>
  48. #include <sys/locking.h>
  49. #ifdef __BORLANDC__
  50. #include <utime.h>
  51. #else
  52. #include <sys/utime.h>
  53. #endif
  54. #include <fcntl.h>
  55. #else
  56. #include <unistd.h>
  57. #include <dirent.h>
  58. #include <fcntl.h>
  59. #include <sys/types.h>
  60. #include <utime.h>
  61. #endif
  62. #include "squirrel.h"
  63. #include <sqstdio.h>
  64. #include <sqstdfile.h>
  65. SQ_OPT_STRING_STRLEN();
  66. #include "sqfs.h"
  67. /* Define 'strerror' for systems that do not implement it */
  68. #ifdef NO_STRERROR
  69. #define strerror(_) "System unable to describe the error"
  70. #endif
  71. /* Define 'getcwd' for systems that do not implement it */
  72. #ifdef NO_GETCWD
  73. #define getcwd(p,s) NULL
  74. #define getcwd_error "Function 'getcwd' not provided by system"
  75. #else
  76. #define getcwd_error strerror(errno)
  77. #ifdef _WIN32
  78. /* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */
  79. #define LFS_MAXPATHLEN MAX_PATH
  80. #else
  81. /* For MAXPATHLEN: */
  82. #include <sys/param.h>
  83. #define LFS_MAXPATHLEN MAXPATHLEN
  84. #endif
  85. #endif
  86. #define DIR_METATABLE "directory metatable"
  87. typedef struct dir_data {
  88. int closed;
  89. #ifdef _WIN32
  90. long hFile;
  91. char pattern[MAX_PATH+1];
  92. #else
  93. DIR *dir;
  94. #endif
  95. } dir_data;
  96. #define LOCK_METATABLE "lock metatable"
  97. #ifdef _WIN32
  98. #ifdef __BORLANDC__
  99. #define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m))
  100. #define STAT_STRUCT struct stati64
  101. #else
  102. #define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m))
  103. #ifdef _WIN32_WCE
  104. #define STAT_STRUCT struct stat
  105. #else
  106. #define STAT_STRUCT struct _stati64
  107. #endif
  108. #endif
  109. #ifdef _WIN32_WCE
  110. #define STAT_FUNC _stat
  111. #else
  112. #define STAT_FUNC _stati64
  113. #endif
  114. #define LSTAT_FUNC STAT_FUNC
  115. #else
  116. #define _O_TEXT 0
  117. #define _O_BINARY 0
  118. #define lfs_setmode(L,file,m) ((void)L, (void)file, (void)m, 0)
  119. #define STAT_STRUCT struct stat
  120. #define STAT_FUNC stat
  121. #define LSTAT_FUNC lstat
  122. #endif
  123. static const SQChar currFileName_key[] = _SC("currFileName");
  124. #ifndef _WIN32_WCE
  125. /*
  126. ** This function changes the working (current) directory
  127. */
  128. static SQRESULT sqfs_chdir (HSQUIRRELVM v) {
  129. SQ_FUNC_VARS_NO_TOP(v);
  130. SQ_GET_STRING(v, 2, path);
  131. if (chdir(path)) {
  132. return sq_throwerror(v, "Unable to change working directory to '%s'\n%s\n",
  133. path, chdir_error);
  134. } else {
  135. sq_pushbool (v, SQTrue);
  136. return 1;
  137. }
  138. }
  139. /*
  140. ** This function returns the current directory
  141. ** If unable to get the current directory, it returns nil
  142. ** and a string describing the error
  143. */
  144. static SQRESULT sqfs_currentdir (HSQUIRRELVM v) {
  145. SQChar *path;
  146. /* Passing (NULL, 0) is not guaranteed to work. Use a temp buffer and size instead. */
  147. char buf[LFS_MAXPATHLEN];
  148. if ((path = getcwd(buf, LFS_MAXPATHLEN)) == NULL) {
  149. return sq_throwerror(v, getcwd_error);
  150. }
  151. else {
  152. sq_pushstring(v, buf, -1);
  153. return 1;
  154. }
  155. }
  156. #endif
  157. #if 0
  158. #ifdef _WIN32
  159. typedef struct sqfs_Lock {
  160. HANDLE fd;
  161. } lfs_Lock;
  162. static SQRESULT sqfs_lock_dir(HSQUIRRELVM v) {
  163. SQ_FUNC_VARS_NO_TOP(v);
  164. SQ_GET_STRING(v, 2, path);
  165. HANDLE fd;
  166. lfs_Lock *lock;
  167. char *ln;
  168. const char *lockfile = "/lockfile.sqfs";
  169. ln = (char*)sq_malloc(path_size + strlen(lockfile) + 1);
  170. if(!ln) return sq_throwerror(v, strerror(errno));
  171. strcpy(ln, path); strcat(ln, lockfile);
  172. if((fd = CreateFile(ln, GENERIC_WRITE, 0, NULL, CREATE_NEW,
  173. FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, NULL)) == INVALID_HANDLE_VALUE) {
  174. int en = GetLastError();
  175. free(ln);
  176. if(en == ERROR_FILE_EXISTS || en == ERROR_SHARING_VIOLATION)
  177. sq_pushliteral(v, "File exists");
  178. else
  179. sq_pushstring(v, strerror(en), -1);
  180. return 1;
  181. }
  182. free(ln);
  183. lock = (lfs_Lock*)lua_newuserdata(L, sizeof(lfs_Lock));
  184. lock->fd = fd;
  185. luaL_getmetatable (L, LOCK_METATABLE);
  186. lua_setmetatable (L, -2);
  187. return 1;
  188. }
  189. static SQRESULT sqfs_unlock_dir(HSQUIRRELVM v) {
  190. lfs_Lock *lock = luaL_checkudata(L, 1, LOCK_METATABLE);
  191. CloseHandle(lock->fd);
  192. return 0;
  193. }
  194. #else
  195. typedef struct sqfs_Lock {
  196. char *ln;
  197. } lfs_Lock;
  198. static SQRESULT sqfs_lock_dir(HSQUIRRELVM v) {
  199. SQ_FUNC_VARS_NO_TOP(v);
  200. SQ_GET_STRING(v, 2, path);
  201. lfs_Lock *lock;
  202. char *ln;
  203. const char *lockfile = "/lockfile.sqfs";
  204. lock = (lfs_Lock*)lua_newuserdata(L, sizeof(sqfs_Lock));
  205. ln = (char*)sq_malloc(path_size + strlen(lockfile) + 1);
  206. if(!ln) return sq_throwerror(v, strerror(errno));
  207. strcpy(ln, path); strcat(ln, lockfile);
  208. if(symlink("lock", ln) == -1) {
  209. free(ln);
  210. return sq_throwerror(v, strerror(errno));
  211. }
  212. lock->ln = ln;
  213. luaL_getmetatable (L, LOCK_METATABLE);
  214. lua_setmetatable (L, -2);
  215. return 1;
  216. }
  217. static SQRESULT lfs_unlock_dir(HSQUIRRELVM v) {
  218. lfs_Lock *lock = luaL_checkudata(L, 1, LOCK_METATABLE);
  219. if(lock->ln) {
  220. unlink(lock->ln);
  221. free(lock->ln);
  222. lock->ln = NULL;
  223. }
  224. return 0;
  225. }
  226. #endif
  227. #ifdef _WIN32
  228. static int lfs_g_setmode (HSQUIRRELVM v, FILE *f, int arg) {
  229. static const int mode[] = {_O_TEXT, _O_BINARY};
  230. static const char *const modenames[] = {"text", "binary", NULL};
  231. int op = luaL_checkoption(L, arg, NULL, modenames);
  232. int res = lfs_setmode(L, f, mode[op]);
  233. if (res != -1) {
  234. int i;
  235. lua_pushboolean(L, 1);
  236. for (i = 0; modenames[i] != NULL; i++) {
  237. if (mode[i] == res) {
  238. lua_pushstring(L, modenames[i]);
  239. goto exit;
  240. }
  241. }
  242. lua_pushnil(L);
  243. exit:
  244. return 2;
  245. } else {
  246. int en = errno;
  247. lua_pushnil(L);
  248. lua_pushfstring(L, "%s", strerror(en));
  249. lua_pushinteger(L, en);
  250. return 3;
  251. }
  252. }
  253. #else
  254. static int lfs_g_setmode (HSQUIRRELVM v, FILE *f, int arg) {
  255. return sq_throwerror(v, "setmode not supported on this platform");
  256. }
  257. #endif
  258. static int sqfs_setmode(HSQUIRRELVM v) {
  259. return lfs_g_setmode(v, check_file(v, 1, "setmode"), 2);
  260. }
  261. #endif
  262. /*
  263. **
  264. */
  265. static int _file_lock (HSQUIRRELVM v, FILE *fp, const char *mode, const long start, long len, const char *funcname) {
  266. int code;
  267. #ifdef _WIN32
  268. /* lkmode valid values are:
  269. LK_LOCK Locks the specified bytes. If the bytes cannot be locked, the program immediately tries again after 1 second. If, after 10 attempts, the bytes cannot be locked, the constant returns an error.
  270. LK_NBLCK Locks the specified bytes. If the bytes cannot be locked, the constant returns an error.
  271. LK_NBRLCK Same as _LK_NBLCK.
  272. LK_RLCK Same as _LK_LOCK.
  273. LK_UNLCK Unlocks the specified bytes, which must have been previously locked.
  274. Regions should be locked only briefly and should be unlocked before closing a file or exiting the program.
  275. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__locking.asp
  276. */
  277. int lkmode;
  278. switch (*mode) {
  279. case 'r': lkmode = LK_NBLCK; break;
  280. case 'w': lkmode = LK_NBLCK; break;
  281. case 'u': lkmode = LK_UNLCK; break;
  282. default : return sq_throwerror (v, "%s: invalid mode", funcname);
  283. }
  284. if (!len) {
  285. fseek (fp, 0L, SEEK_END);
  286. len = ftell (fp);
  287. }
  288. fseek (fp, start, SEEK_SET);
  289. int fd = fileno(fp);
  290. code = _locking (fd, lkmode, len);
  291. #else
  292. struct flock f;
  293. switch (*mode) {
  294. case 'w': f.l_type = F_WRLCK; break;
  295. case 'r': f.l_type = F_RDLCK; break;
  296. case 'u': f.l_type = F_UNLCK; break;
  297. default : return sq_throwerror(v, "%s: invalid mode", funcname);
  298. }
  299. f.l_whence = SEEK_SET;
  300. f.l_start = (off_t)start;
  301. f.l_len = (off_t)len;
  302. int fd = fileno(fp);
  303. //do {
  304. code = fcntl (fd, F_SETLK, &f);
  305. //} while(code < 0 && errno == EINTR);
  306. #endif
  307. return (code != -1);
  308. }
  309. #define GET_file_INSTANCE() SQ_GET_INSTANCE(v, 1, SQFile, SQSTD_FILE_TYPE_TAG) \
  310. if(self == NULL) return sq_throwerror(v, _SC("file object already closed"));
  311. /*
  312. ** Locks a file.
  313. ** @param #1 File handle.
  314. ** @param #2 String with lock mode ('w'rite, 'r'ead).
  315. ** @param #3 Number with start position (optional).
  316. ** @param #4 Number with length (optional).
  317. */
  318. static SQRESULT sqfs_lock(HSQUIRRELVM v) {
  319. SQ_FUNC_VARS(v);
  320. GET_file_INSTANCE();
  321. SQ_GET_STRING(v, 2, mode);
  322. SQ_OPT_INTEGER(v, 3, start, 0);
  323. SQ_OPT_INTEGER(v, 4, len, 0);
  324. FILE *fp = (FILE*)self->GetHandle();
  325. int rc = _file_lock (v, fp, mode, start, len, _SC("lock"));
  326. sq_pushbool (v, rc > 0);
  327. return 1;
  328. }
  329. /*
  330. ** Unlocks a file.
  331. ** @param #1 File handle.
  332. ** @param #2 Number with start position (optional).
  333. ** @param #3 Number with length (optional).
  334. */
  335. static SQRESULT sqfs_unlock (HSQUIRRELVM v) {
  336. SQ_FUNC_VARS(v);
  337. GET_file_INSTANCE();
  338. SQ_OPT_INTEGER(v, 2, start, 0);
  339. SQ_OPT_INTEGER(v, 3, len, 0);
  340. FILE *fp = (FILE*)self->GetHandle();
  341. int rc = _file_lock (v, fp, "u", start, len, _SC("unlock"));
  342. sq_pushbool (v, rc > 0);
  343. return 1;
  344. }
  345. #define _DECL_FILELOCK_FUNC(name,nparams,pmask) {_SC(#name),sqfs_##name,nparams,pmask}
  346. static SQRegFunction file_lock_obj_funcs[]={
  347. _DECL_FILELOCK_FUNC(lock, -2, _SC("xsii")),
  348. //_DECL_FILELOCK_FUNC(trylock, -2, _SC("xsii")),
  349. _DECL_FILELOCK_FUNC(unlock, -1, _SC("x")),
  350. {0,0}
  351. };
  352. #undef _DECL_FILELOCK_FUNC
  353. /*
  354. ** Creates a link.
  355. ** @param #1 Object to link to.
  356. ** @param #2 Name of link.
  357. ** @param #3 True if link is symbolic (optional).
  358. */
  359. static SQRESULT sqfs_link(HSQUIRRELVM v)
  360. {
  361. #ifndef _WIN32
  362. SQ_FUNC_VARS(v);
  363. SQ_GET_STRING(v, 2, oldpath);
  364. SQ_GET_STRING(v, 3, newpath);
  365. SQ_OPT_BOOL(v, 4, bsym, SQFalse);
  366. sq_pushinteger(v, (bsym ? symlink : link)(oldpath, newpath));
  367. return 1;
  368. #else
  369. return sq_throwerror(v, "make_link is not supported on Windows");
  370. #endif
  371. }
  372. static SQRESULT sqfs_mkdir (HSQUIRRELVM v) {
  373. SQ_FUNC_VARS_NO_TOP(v);
  374. SQ_GET_STRING(v, 2, path);
  375. int fail;
  376. #ifdef _WIN32_WCE
  377. fail = mkdir (path);
  378. #elif defined(_WIN32)
  379. int oldmask = umask (0);
  380. fail = _mkdir (path);
  381. #else
  382. mode_t oldmask = umask( (mode_t)0 );
  383. fail = mkdir (path, S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
  384. S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH );
  385. #endif
  386. if (fail) {
  387. return sq_throwerror (v, "%s", strerror(errno));
  388. }
  389. #ifndef _WIN32_WCE
  390. umask (oldmask);
  391. #endif
  392. sq_pushbool (v, SQTrue);
  393. return 1;
  394. }
  395. /*
  396. ** Removes a directory.
  397. ** @param #1 Directory path.
  398. */
  399. static SQRESULT sqfs_rmdir (HSQUIRRELVM v) {
  400. SQ_FUNC_VARS_NO_TOP(v);
  401. SQ_GET_STRING(v, 2, path);
  402. int fail;
  403. fail = rmdir (path);
  404. if (fail) {
  405. return sq_throwerror (v, "%s", strerror(errno));
  406. }
  407. sq_pushbool (v, SQTrue);
  408. return 1;
  409. }
  410. static const SQChar SQFS_DIR_TAG[] = _SC("sqfs_dir_tag");
  411. #define GET_dir_INSTANCE() SQ_GET_INSTANCE(v, 1, dir_data, SQFS_DIR_TAG)
  412. static void _dir_close_dir(dir_data *dir)
  413. {
  414. #ifdef _WIN32
  415. if (!dir->closed && dir->hFile) {
  416. _findclose (dir->hFile);
  417. }
  418. #else
  419. if (!dir->closed && dir->dir) {
  420. closedir (dir->dir);
  421. }
  422. #endif
  423. dir->closed = 1;
  424. }
  425. static SQRESULT _dir_releasehook(SQUserPointer p, SQInteger size, void *ep)
  426. {
  427. dir_data *dir = ((dir_data *)p);
  428. _dir_close_dir(dir);
  429. sq_free(dir, sizeof(dir_data));
  430. return 0;
  431. }
  432. static SQRESULT _dir_constructor(HSQUIRRELVM v)
  433. {
  434. SQ_FUNC_VARS_NO_TOP(v);
  435. SQ_GET_STRING(v, 2, path);
  436. dir_data *dir = (dir_data*)sq_malloc(sizeof(dir_data));
  437. dir->closed = 0;
  438. #ifdef _WIN32
  439. dir->hFile = 0L;
  440. if (strlen(path) > MAX_PATH-2)
  441. return sq_throwerror(v, "path too long: %s", path);
  442. else
  443. scsprintf (dir->pattern, sizeof(dir->pattern), "%s/*", path);
  444. #else
  445. dir->dir = opendir (path);
  446. if (dir->dir == NULL)
  447. return sq_throwerror(v, "cannot open %s: %s", path, strerror (errno));
  448. #endif
  449. //sq_pushstring(v, currFileName_key, sizeof(currFileName_key)-1); //store file name between iterations
  450. //sq_pushliteral(v, _SC(""));
  451. //if(sq_set(v, 1) != SQ_OK) return SQ_ERROR;
  452. sq_setinstanceup(v, 1, dir); //replace self for this instance with this new sqlite3_stmt
  453. sq_setreleasehook(v,1, _dir_releasehook);
  454. return 1;
  455. }
  456. static SQRESULT _dir_close(HSQUIRRELVM v)
  457. {
  458. SQ_FUNC_VARS_NO_TOP(v);
  459. GET_dir_INSTANCE();
  460. _dir_close_dir(self);
  461. return 0;
  462. }
  463. static SQRESULT _dir__get(HSQUIRRELVM v)
  464. {
  465. sq_pushstring(v, currFileName_key, sizeof(currFileName_key)-1);
  466. if(sq_get(v, 1) == SQ_ERROR) sq_pushnull(v);
  467. return 1;
  468. }
  469. static SQRESULT _dir__nexti(HSQUIRRELVM v)
  470. {
  471. SQ_FUNC_VARS_NO_TOP(v);
  472. GET_dir_INSTANCE();
  473. SQInteger idx;
  474. if(sq_gettype(v,2) == OT_NULL) idx = 0;
  475. else
  476. {
  477. if(!SQ_SUCCEEDED(sq_getinteger(v, 2, &idx)))
  478. return sq_throwerror(v,_SC("internal error (_nexti) wrong argument type"));
  479. ++idx;
  480. }
  481. #ifdef _WIN32
  482. struct _finddata_t c_file;
  483. #else
  484. struct dirent *entry;
  485. #endif
  486. const char *fname;
  487. if(self->closed) return sq_throwerror(v, "closed directory");
  488. #ifdef _WIN32
  489. if (self->hFile == 0L) { /* first entry */
  490. if ((self->hFile = _findfirst (self->pattern, &c_file)) == -1L) {
  491. self->closed = 1;
  492. return sq_throwerror(v, strerror (errno));
  493. }
  494. } else { /* next entry */
  495. if (_findnext (self->hFile, &c_file) == -1L) {
  496. /* no more entries => close directory */
  497. _findclose (self->hFile);
  498. self->closed = 1;
  499. sq_pushnull(v);
  500. return 1;
  501. }
  502. }
  503. fname = c_file.name;
  504. #else
  505. if ((entry = readdir (self->dir)) == NULL) {
  506. /* no more entries => close directory */
  507. closedir (self->dir);
  508. self->closed = 1;
  509. sq_pushnull(v);
  510. return 1;
  511. }
  512. fname = entry->d_name;
  513. #endif
  514. sq_pushstring(v, currFileName_key, sizeof(currFileName_key)-1);
  515. sq_pushstring(v, fname, -1);
  516. if(sq_set(v, 1) != SQ_OK) return SQ_ERROR;
  517. sq_pushinteger(v, idx);
  518. return 1;
  519. }
  520. #define _DECL_DIR_FUNC(name,nparams,typecheck) {_SC(#name),_dir_##name,nparams,typecheck}
  521. static SQRegFunction _dir_methods[] = {
  522. _DECL_DIR_FUNC(constructor,-1,_SC("xs")),
  523. _DECL_DIR_FUNC(close,1,_SC("x")),
  524. _DECL_DIR_FUNC(_nexti,2,_SC("x.")),
  525. _DECL_DIR_FUNC(_get,2,_SC("xn")),
  526. {0,0,0,0}
  527. };
  528. #ifdef _WIN32
  529. #ifndef S_ISDIR
  530. #define S_ISDIR(mode) (mode&_S_IFDIR)
  531. #endif
  532. #ifndef S_ISREG
  533. #define S_ISREG(mode) (mode&_S_IFREG)
  534. #endif
  535. #ifndef S_ISLNK
  536. #define S_ISLNK(mode) (0)
  537. #endif
  538. #ifndef S_ISSOCK
  539. #define S_ISSOCK(mode) (0)
  540. #endif
  541. #ifndef S_ISFIFO
  542. #define S_ISFIFO(mode) (0)
  543. #endif
  544. #ifndef S_ISCHR
  545. #define S_ISCHR(mode) (mode&_S_IFCHR)
  546. #endif
  547. #ifndef S_ISBLK
  548. #define S_ISBLK(mode) (0)
  549. #endif
  550. #endif
  551. /*
  552. ** Convert the inode protection mode to a string.
  553. */
  554. #ifdef _WIN32
  555. static const SQChar *mode2string (unsigned short mode) {
  556. #else
  557. static const SQChar *mode2string (mode_t mode) {
  558. #endif
  559. if ( S_ISREG(mode) )
  560. return _SC("file");
  561. else if ( S_ISDIR(mode) )
  562. return _SC("directory");
  563. else if ( S_ISLNK(mode) )
  564. return _SC("link");
  565. else if ( S_ISSOCK(mode) )
  566. return _SC("socket");
  567. else if ( S_ISFIFO(mode) )
  568. return _SC("named pipe");
  569. else if ( S_ISCHR(mode) )
  570. return _SC("char device");
  571. else if ( S_ISBLK(mode) )
  572. return _SC("block device");
  573. else
  574. return _SC("other");
  575. }
  576. /*
  577. ** Set access time and modification values for file
  578. */
  579. static SQRESULT sqfs_touch (HSQUIRRELVM v) {
  580. SQ_FUNC_VARS(v);
  581. SQ_GET_STRING(v, 2, file);
  582. struct utimbuf utb, *buf;
  583. if (_top_ == 2) /* set to current date/time */
  584. buf = NULL;
  585. else {
  586. SQ_OPT_INTEGER(v, 3, actime, 0);
  587. SQ_OPT_INTEGER(v, 4, modtime, actime);
  588. utb.actime = (time_t)actime;
  589. utb.modtime = (time_t)modtime;
  590. buf = &utb;
  591. }
  592. if (utime (file, buf)) {
  593. return sq_throwerror(v, "%s", strerror (errno));
  594. }
  595. sq_pushbool (v, SQTrue);
  596. return 1;
  597. }
  598. /* inode protection mode */
  599. static void push_st_mode (HSQUIRRELVM v, STAT_STRUCT *info) {
  600. sq_pushstring (v, mode2string (info->st_mode), -1);
  601. }
  602. /* device inode resides on */
  603. static void push_st_dev (HSQUIRRELVM v, STAT_STRUCT *info) {
  604. sq_pushinteger (v, (SQInteger)info->st_dev);
  605. }
  606. /* inode's number */
  607. static void push_st_ino (HSQUIRRELVM v, STAT_STRUCT *info) {
  608. sq_pushinteger (v, (SQInteger)info->st_ino);
  609. }
  610. /* number of hard links to the file */
  611. static void push_st_nlink (HSQUIRRELVM v, STAT_STRUCT *info) {
  612. sq_pushinteger (v, (SQInteger)info->st_nlink);
  613. }
  614. /* user-id of owner */
  615. static void push_st_uid (HSQUIRRELVM v, STAT_STRUCT *info) {
  616. sq_pushinteger (v, (SQInteger)info->st_uid);
  617. }
  618. /* group-id of owner */
  619. static void push_st_gid (HSQUIRRELVM v, STAT_STRUCT *info) {
  620. sq_pushinteger (v, (SQInteger)info->st_gid);
  621. }
  622. /* device type, for special file inode */
  623. static void push_st_rdev (HSQUIRRELVM v, STAT_STRUCT *info) {
  624. sq_pushinteger (v, (SQInteger)info->st_rdev);
  625. }
  626. /* time of last access */
  627. static void push_st_atime (HSQUIRRELVM v, STAT_STRUCT *info) {
  628. sq_pushinteger (v, info->st_atime);
  629. }
  630. /* time of last data modification */
  631. static void push_st_mtime (HSQUIRRELVM v, STAT_STRUCT *info) {
  632. sq_pushinteger (v, info->st_mtime);
  633. }
  634. /* time of last file status change */
  635. static void push_st_ctime (HSQUIRRELVM v, STAT_STRUCT *info) {
  636. sq_pushinteger (v, info->st_ctime);
  637. }
  638. /* file size, in bytes */
  639. static void push_st_size (HSQUIRRELVM v, STAT_STRUCT *info) {
  640. sq_pushinteger (v, (SQInteger)info->st_size);
  641. }
  642. #ifndef _WIN32
  643. /* blocks allocated for file */
  644. static void push_st_blocks (HSQUIRRELVM v, STAT_STRUCT *info) {
  645. sq_pushinteger (v, (SQInteger)info->st_blocks);
  646. }
  647. /* optimal file system I/O blocksize */
  648. static void push_st_blksize (HSQUIRRELVM v, STAT_STRUCT *info) {
  649. sq_pushinteger (v, (SQInteger)info->st_blksize);
  650. }
  651. #endif
  652. static void push_invalid (HSQUIRRELVM v, STAT_STRUCT *info) {
  653. sq_throwerror(v, "invalid attribute name");
  654. #ifndef _WIN32
  655. info->st_blksize = 0; /* never reached */
  656. #endif
  657. }
  658. typedef void (*_push_function) (HSQUIRRELVM v, STAT_STRUCT *info);
  659. struct _stat_members {
  660. const char *name;
  661. _push_function push;
  662. };
  663. struct _stat_members members[] = {
  664. { "mode", push_st_mode },
  665. { "dev", push_st_dev },
  666. { "ino", push_st_ino },
  667. { "nlink", push_st_nlink },
  668. { "uid", push_st_uid },
  669. { "gid", push_st_gid },
  670. { "rdev", push_st_rdev },
  671. { "access", push_st_atime },
  672. { "modification", push_st_mtime },
  673. { "change", push_st_ctime },
  674. { "size", push_st_size },
  675. #ifndef _WIN32
  676. { "blocks", push_st_blocks },
  677. { "blksize", push_st_blksize },
  678. #endif
  679. { NULL, push_invalid }
  680. };
  681. /*
  682. ** Get file or symbolic link information
  683. */
  684. static int _file_info_ (HSQUIRRELVM v, int (*st)(const SQChar*, STAT_STRUCT*)) {
  685. SQ_FUNC_VARS(v);
  686. SQ_GET_STRING(v, 2, file);
  687. int i;
  688. STAT_STRUCT info;
  689. if (st(file, &info)) {
  690. return sq_throwerror(v, "cannot obtain information from file `%s'", file);
  691. }
  692. if(_top_ > 2){
  693. int ptype = sq_gettype(v, 3);
  694. if (ptype == OT_STRING) {
  695. int iv;
  696. SQ_GET_STRING(v, 3, member);
  697. if (strcmp (member, "mode") == 0) iv = 0;
  698. #ifndef _WIN32
  699. else if (strcmp (member, "blocks") == 0) iv = 11;
  700. else if (strcmp (member, "blksize") == 0) iv = 12;
  701. #endif
  702. else /* look for member */
  703. for (iv = 1; members[iv].name; iv++)
  704. if (*members[iv].name == *member)
  705. break;
  706. /* push member value and return */
  707. members[iv].push (v, &info);
  708. return 1;
  709. } else if (ptype != OT_TABLE)
  710. /* creates a table if none is given */
  711. sq_newtable (v);
  712. }
  713. else sq_newtable (v);
  714. /* stores all members in table on top of the stack */
  715. for (i = 0; members[i].name; i++) {
  716. sq_pushstring (v, members[i].name, -1);
  717. members[i].push (v, &info);
  718. sq_rawset (v, -3);
  719. }
  720. return 1;
  721. }
  722. /*
  723. ** Get file information using stat.
  724. */
  725. static SQRESULT sqfs_attributes (HSQUIRRELVM v) {
  726. return _file_info_ (v, STAT_FUNC);
  727. }
  728. /*
  729. ** Get symbolic link information using lstat.
  730. */
  731. #ifndef _WIN32
  732. static SQRESULT sqfs_symlinkattributes (HSQUIRRELVM v) {
  733. return _file_info_ (v, LSTAT_FUNC);
  734. }
  735. #else
  736. static SQRESULT sqfs_symlinkattributes (HSQUIRRELVM v) {
  737. sq_pushliteral(v, "symlinkattributes not supported on this platform");
  738. return 1;
  739. }
  740. #endif
  741. /*
  742. ** Assumes the table is on top of the stack.
  743. */
  744. static void set_info (HSQUIRRELVM v) {
  745. sq_pushliteral (v, "_COPYRIGHT");
  746. sq_pushliteral (v, "Copyright (C) Copyright (C) 2003-2012 Kepler Project, Domingo Alvarez Duarte");
  747. sq_rawset (v, -3);
  748. sq_pushliteral (v, "_DESCRIPTION");
  749. sq_pushliteral (v, "LuaFileSystem is a Lua library developed to complement the set of"
  750. " functions related to file systems offered by the standard Lua distribution"
  751. ", adapted to Squirrel by Domingo Alvarez Duarte");
  752. sq_rawset (v, -3);
  753. sq_pushliteral (v, "_VERSION");
  754. sq_pushliteral (v, "LuaFileSystem 1.61");
  755. sq_rawset (v, -3);
  756. }
  757. #define _DECL_FUNC(name,nparams,tycheck) {_SC(#name), sqfs_##name,nparams,tycheck}
  758. static SQRegFunction sqfs_methods[] =
  759. {
  760. _DECL_FUNC(attributes, -2, _SC(".ss")),
  761. #ifndef _WIN32_WCE
  762. _DECL_FUNC(chdir, 2, _SC(".s")),
  763. _DECL_FUNC(currentdir, 1, _SC(".")),
  764. #endif
  765. /*
  766. _DECL_FUNC(lock, -3, _SC("xsii")),
  767. */
  768. _DECL_FUNC(link, -3, _SC(".ssb")),
  769. _DECL_FUNC(mkdir, 2, _SC(".s")),
  770. _DECL_FUNC(rmdir, 2, _SC(".s")),
  771. _DECL_FUNC(symlinkattributes, -2, _SC(".ss")),
  772. // _DECL_FUNC(setmode, 3, _SC(".ss")),
  773. _DECL_FUNC(touch, -2, _SC(".sii")),
  774. /*
  775. _DECL_FUNC(unlock, -2, _SC(".sii")),
  776. _DECL_FUNC(lock_dir, -2, _SC(".si")),
  777. */
  778. {0,0}
  779. };
  780. #ifdef __cplusplus
  781. extern "C" {
  782. #endif
  783. SQRESULT sqext_register_sqfs(HSQUIRRELVM v)
  784. {
  785. sq_pushstring(v, SQSTD_FILE_CLASS_TYPE_TAG, -1);
  786. if(sq_getonregistrytable(v) != SQ_OK){
  787. return sq_throwerror(v, _SC("file class not found"));
  788. }
  789. sq_insert_reg_funcs(v, file_lock_obj_funcs);
  790. sq_poptop(v);
  791. sq_pushstring(v,_SC("sqfs"),-1);
  792. sq_newtable(v);
  793. set_info(v);
  794. sq_insert_reg_funcs(v, sqfs_methods);
  795. sq_pushliteral(v, _SC("dir"));
  796. sq_newclass(v, SQFalse);
  797. sq_settypetag(v,-1,(void*)SQFS_DIR_TAG);
  798. sq_insert_reg_funcs(v, _dir_methods);
  799. sq_pushstring(v, currFileName_key, sizeof(currFileName_key)-1); //store file name between iterations
  800. sq_pushnull(v);
  801. sq_newslot(v,-3,SQFalse);
  802. sq_newslot(v,-3,SQTrue);
  803. sq_newslot(v,-3,SQTrue);
  804. return 1;
  805. }
  806. #ifdef __cplusplus
  807. }
  808. #endif