1 /*
2 Copyright (c) 2010 Peter "Corsix" Cawley
3 Copyright (c) 2014 Stephen E. Baker
4 
5 Permission is hereby granted, free of charge, to any person obtaining a copy of
6 this software and associated documentation files (the "Software"), to deal in
7 the Software without restriction, including without limitation the rights to
8 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9 of the Software, and to permit persons to whom the Software is furnished to do
10 so, subject to the following conditions:
11 
12 The above copyright notice and this permission notice shall be included in all
13 copies or substantial portions of the Software.
14 
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 SOFTWARE.
22 */
23 
24 #include "config.h"
25 
26 #include "th_lua_internal.h"
27 #ifdef CORSIX_TH_USE_WIN32_SDK
28 #include <windows.h>
29 #endif
30 
31 class lfs_ext {};
32 
33 namespace {
34 
l_lfs_ext_new(lua_State * L)35 int l_lfs_ext_new(lua_State* L) {
36   luaT_stdnew<lfs_ext>(L, luaT_environindex, true);
37   return 1;
38 }
39 
40 #ifdef _WIN32
41 #ifdef CORSIX_TH_USE_WIN32_SDK
l_volume_list(lua_State * L)42 int l_volume_list(lua_State* L) {
43   /* Windows, using the Win32 API. */
44   DWORD iDriveMask = GetLogicalDrives();
45   int iNDrives = 0;
46   char cDrive;
47   lua_settop(L, 0);
48   lua_newtable(L);
49   for (cDrive = 'A'; cDrive <= 'Z'; ++cDrive) {
50     if (iDriveMask & (1 << (cDrive - 'A'))) {
51       char sName[4] = {cDrive, ':', '\\', 0};
52       if (GetDriveTypeA(sName) > DRIVE_NO_ROOT_DIR) {
53         lua_pushlstring(L, sName, 2);
54         lua_rawseti(L, 1, ++iNDrives);
55       }
56     }
57   }
58   return 1;
59 }
60 #else
l_volume_list(lua_State * L)61 int l_volume_list(lua_State* L) {
62   /* Windows, without the Win32 API. */
63   int iNDrives = 0;
64   char cDrive;
65   lua_settop(L, 0);
66   lua_newtable(L);
67   lua_getfield(L, luaT_upvalueindex(1), "attributes");
68   for (cDrive = 'A'; cDrive <= 'Z'; ++cDrive) {
69     lua_pushvalue(L, 2);
70     lua_pushfstring(L, "%c:\\", cDrive);
71     lua_pushliteral(L, "mode");
72     lua_call(L, 2, 1);
73     if (lua_toboolean(L, 3) != 0) {
74       lua_pushfstring(L, "%c:", cDrive);
75       lua_rawseti(L, 1, ++iNDrives);
76     }
77     lua_pop(L, 1);
78   }
79   return 1;
80 }
81 #endif
82 #else
l_volume_list(lua_State * L)83 int l_volume_list(lua_State* L) {
84   /* Non-Windows systems. Assume that / is the root of the filesystem. */
85   lua_settop(L, 0);
86   lua_newtable(L);
87   lua_pushliteral(L, "/");
88   lua_rawseti(L, 1, 1);
89   return 1;
90 }
91 #endif
92 
93 }  // namespace
94 
lua_register_lfs_ext(const lua_register_state * pState)95 void lua_register_lfs_ext(const lua_register_state* pState) {
96   lua_class_binding<lfs_ext> lcb(pState, "lfsExt", l_lfs_ext_new,
97                                  lua_metatable::lfs_ext);
98   lcb.add_function(l_volume_list, "volumes");
99 }
100