sq_fs.cpp 23 KB

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