diff options
author | Reuben Thomas <rrt@sc3d.org> | 2011-06-09 14:23:44 +0100 |
---|---|---|
committer | Reuben Thomas <rrt@sc3d.org> | 2011-06-09 19:53:41 +0100 |
commit | 361cede4d08073183d3546a78763e7b62d6be9ef (patch) | |
tree | f86f63145c8dbd3a5cf99c785bfde60740d0a132 | |
parent | 3140ca4db8fbc35efaf44a4dd5176bfd0d99c713 (diff) | |
download | luafilesystem-361cede4d08073183d3546a78763e7b62d6be9ef.tar.gz luafilesystem-361cede4d08073183d3546a78763e7b62d6be9ef.tar.bz2 luafilesystem-361cede4d08073183d3546a78763e7b62d6be9ef.zip |
Add trivial implementation of setmode on non-Windows platforms.
-rw-r--r-- | doc/us/manual.html | 4 | ||||
-rw-r--r-- | src/lfs.c | 15 | ||||
-rw-r--r-- | tests/test.lua | 16 |
3 files changed, 12 insertions, 23 deletions
diff --git a/doc/us/manual.html b/doc/us/manual.html index a5843ed..48a8e5b 100644 --- a/doc/us/manual.html +++ b/doc/us/manual.html | |||
@@ -221,8 +221,8 @@ LuaFileSystem offers the following functions: | |||
221 | 221 | ||
222 | <dt><a name="setmode"></a><strong><code>lfs.setmode (file, mode)</code></strong></dt> | 222 | <dt><a name="setmode"></a><strong><code>lfs.setmode (file, mode)</code></strong></dt> |
223 | <dd>Sets the writing mode for a file. The mode string can be either <code>binary</code> or <code>text</code>. | 223 | <dd>Sets the writing mode for a file. The mode string can be either <code>binary</code> or <code>text</code>. |
224 | Returns the previous mode string for the file. This function is only available in Windows, so you may want to make sure that | 224 | Returns the previous mode string for the file. On non-Windows platforms, where the two modes are identical, |
225 | <code>lfs.setmode</code> exists before using it. | 225 | setting the mode has no effect, and the mode is always returned as <code>binary</code>. |
226 | </dd> | 226 | </dd> |
227 | 227 | ||
228 | <dt><a name="symlinkattributes"></a><strong><code>lfs.symlinkattributes (filepath [, aname])</code></strong></dt> | 228 | <dt><a name="symlinkattributes"></a><strong><code>lfs.symlinkattributes (filepath [, aname])</code></strong></dt> |
@@ -100,8 +100,7 @@ typedef struct dir_data { | |||
100 | #else | 100 | #else |
101 | #define _O_TEXT 0 | 101 | #define _O_TEXT 0 |
102 | #define _O_BINARY 0 | 102 | #define _O_BINARY 0 |
103 | #define lfs_setmode(L,file,m) ((void)((void)file,m), \ | 103 | #define lfs_setmode(L,file,m) 0 |
104 | luaL_error(L, LUA_QL("setmode") " not supported on this platform"), -1) | ||
105 | #define STAT_STRUCT struct stat | 104 | #define STAT_STRUCT struct stat |
106 | #define STAT_FUNC stat | 105 | #define STAT_FUNC stat |
107 | #define LSTAT_FUNC lstat | 106 | #define LSTAT_FUNC lstat |
@@ -281,10 +280,9 @@ static int lfs_unlock_dir(lua_State *L) { | |||
281 | } | 280 | } |
282 | #endif | 281 | #endif |
283 | 282 | ||
284 | #ifdef _WIN32 | ||
285 | static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | 283 | static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { |
286 | static const int mode[] = {_O_TEXT, _O_BINARY}; | 284 | static const int mode[] = {_O_BINARY, _O_TEXT}; |
287 | static const char *const modenames[] = {"text", "binary", NULL}; | 285 | static const char *const modenames[] = {"binary", "text", NULL}; |
288 | int op = luaL_checkoption(L, arg, NULL, modenames); | 286 | int op = luaL_checkoption(L, arg, NULL, modenames); |
289 | int res = lfs_setmode(L, f, mode[op]); | 287 | int res = lfs_setmode(L, f, mode[op]); |
290 | if (res != -1) { | 288 | if (res != -1) { |
@@ -307,13 +305,6 @@ static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | |||
307 | return 3; | 305 | return 3; |
308 | } | 306 | } |
309 | } | 307 | } |
310 | #else | ||
311 | static int lfs_g_setmode (lua_State *L, FILE *f, int arg) { | ||
312 | lua_pushboolean(L, 0); | ||
313 | lua_pushliteral(L, "setmode not supported on this platform"); | ||
314 | return 2; | ||
315 | } | ||
316 | #endif | ||
317 | 308 | ||
318 | static int lfs_f_setmode(lua_State *L) { | 309 | static int lfs_f_setmode(lua_State *L) { |
319 | return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); | 310 | return lfs_g_setmode(L, check_file(L, 1, "setmode"), 2); |
diff --git a/tests/test.lua b/tests/test.lua index 133bac2..6eb9f70 100644 --- a/tests/test.lua +++ b/tests/test.lua | |||
@@ -76,15 +76,13 @@ if (os.execute ("ln -s "..tmpfile.." _a_link_for_test_")) then | |||
76 | assert (os.remove"_a_link_for_test_") | 76 | assert (os.remove"_a_link_for_test_") |
77 | end | 77 | end |
78 | 78 | ||
79 | if lfs.setmode then | 79 | -- Checking text/binary modes (only has an effect in Windows) |
80 | -- Checking text/binary modes (works only in Windows) | 80 | local f = io.open(tmpfile, "w") |
81 | local f = io.open(tmpfile, "w") | 81 | local result, mode = lfs.setmode(f, "binary") |
82 | local result, mode = lfs.setmode(f, "binary") | 82 | assert(result) -- on non-Windows platforms, mode is always returned as "binary" |
83 | assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform")) | 83 | result, mode = lfs.setmode(f, "text") |
84 | result, mode = lfs.setmode(f, "text") | 84 | assert(result and mode == "binary") |
85 | assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform")) | 85 | f:close() |
86 | f:close() | ||
87 | end | ||
88 | 86 | ||
89 | -- Restore access time to current value | 87 | -- Restore access time to current value |
90 | assert (lfs.touch (tmpfile, attrib.access, attrib.modification)) | 88 | assert (lfs.touch (tmpfile, attrib.access, attrib.modification)) |