Skip to content

Commit 7ec1d58

Browse files
committed
Merge remote-tracking branch 'lethosor/filesystem-module-pr'
2 parents 1500729 + d599fa4 commit 7ec1d58

File tree

5 files changed

+354
-0
lines changed

5 files changed

+354
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ DFHack future
22

33
Internals:
44
- support for calling a lua function via a protobuf request (demonstrated by dfhack-run --lua).
5+
- support for basic filesystem operations (e.g. chdir, mkdir, rmdir, stat) in C++ and Lua
56
- Lua API for listing files in directory. Needed for mod-manager.
67
- Lua API for creating unit combat reports and writing to gamelog.
78
- Lua API for running arbitrary DFHack commands

library/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ include/modules/Vermin.h
127127
include/modules/World.h
128128
include/modules/Graphic.h
129129
include/modules/Once.h
130+
include/modules/Filesystem.h
130131
)
131132

132133
SET( MODULE_SOURCES
@@ -152,6 +153,7 @@ modules/World.cpp
152153
modules/Graphic.cpp
153154
modules/Windows.cpp
154155
modules/Once.cpp
156+
modules/Filesystem.cpp
155157
)
156158

157159
IF(WIN32)

library/LuaApi.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ distribution.
5252
#include "modules/Buildings.h"
5353
#include "modules/Constructions.h"
5454
#include "modules/Random.h"
55+
#include "modules/Filesystem.h"
5556

5657
#include "LuaWrapper.h"
5758
#include "LuaTools.h"
@@ -1923,6 +1924,20 @@ static const luaL_Reg dfhack_screen_funcs[] = {
19231924
{ NULL, NULL }
19241925
};
19251926

1927+
/***** Filesystem module *****/
1928+
1929+
static const LuaWrapper::FunctionReg dfhack_filesystem_module[] = {
1930+
WRAPM(Filesystem, getcwd),
1931+
WRAPM(Filesystem, chdir),
1932+
WRAPM(Filesystem, mkdir),
1933+
WRAPM(Filesystem, rmdir),
1934+
WRAPM(Filesystem, exists),
1935+
WRAPM(Filesystem, isfile),
1936+
WRAPM(Filesystem, isdir),
1937+
{NULL, NULL}
1938+
};
1939+
1940+
19261941
/***** Internal module *****/
19271942

19281943
static void *checkaddr(lua_State *L, int idx, bool allow_null = false)
@@ -2324,5 +2339,6 @@ void OpenDFHackApi(lua_State *state)
23242339
OpenModule(state, "buildings", dfhack_buildings_module, dfhack_buildings_funcs);
23252340
OpenModule(state, "constructions", dfhack_constructions_module);
23262341
OpenModule(state, "screen", dfhack_screen_module, dfhack_screen_funcs);
2342+
OpenModule(state, "filesystem", dfhack_filesystem_module);
23272343
OpenModule(state, "internal", dfhack_internal_module, dfhack_internal_funcs);
23282344
}
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
/*
2+
https://github.com/peterix/dfhack
3+
Copyright (c) 2009-2012 Petr Mrázek ([email protected])
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any
7+
damages arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any
10+
purpose, including commercial applications, and to alter it and
11+
redistribute it freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must
14+
not claim that you wrote the original software. If you use this
15+
software in a product, an acknowledgment in the product documentation
16+
would be appreciated but is not required.
17+
18+
2. Altered source versions must be plainly marked as such, and
19+
must not be misrepresented as being the original software.
20+
21+
3. This notice may not be removed or altered from any source
22+
distribution.
23+
*/
24+
/* Based on luafilesystem
25+
Copyright © 2003-2014 Kepler Project.
26+
27+
Permission is hereby granted, free of charge, to any person
28+
obtaining a copy of this software and associated documentation
29+
files (the "Software"), to deal in the Software without
30+
restriction, including without limitation the rights to use, copy,
31+
modify, merge, publish, distribute, sublicense, and/or sell copies
32+
of the Software, and to permit persons to whom the Software is
33+
furnished to do so, subject to the following conditions:
34+
35+
The above copyright notice and this permission notice shall be
36+
included in all copies or substantial portions of the Software.
37+
38+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
42+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
43+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45+
SOFTWARE.
46+
*/
47+
48+
#pragma once
49+
#include "Export.h"
50+
51+
#ifndef _WIN32
52+
#ifndef _AIX
53+
#define _FILE_OFFSET_BITS 64 /* Linux, Solaris and HP-UX */
54+
#else
55+
#define _LARGE_FILES 1 /* AIX */
56+
#endif
57+
#endif
58+
59+
#ifndef _LARGEFILE64_SOURCE
60+
#define _LARGEFILE64_SOURCE
61+
#endif
62+
63+
#include <errno.h>
64+
#include <stdio.h>
65+
#include <string.h>
66+
#include <stdlib.h>
67+
#include <time.h>
68+
#include <sys/stat.h>
69+
70+
#ifdef _WIN32
71+
#include <direct.h>
72+
#include <windows.h>
73+
#include <io.h>
74+
#include <sys/locking.h>
75+
#ifdef __BORLANDC__
76+
#include <utime.h>
77+
#else
78+
#include <sys/utime.h>
79+
#endif
80+
#include <fcntl.h>
81+
#else
82+
#include <unistd.h>
83+
#include <dirent.h>
84+
#include <fcntl.h>
85+
#include <sys/types.h>
86+
#include <utime.h>
87+
#endif
88+
89+
#define LFS_VERSION "1.6.2"
90+
#define LFS_LIBNAME "lfs"
91+
92+
#if LUA_VERSION_NUM < 502
93+
# define luaL_newlib(L,l) (lua_newtable(L), luaL_register(L,NULL,l))
94+
#endif
95+
96+
/* Define 'strerror' for systems that do not implement it */
97+
#ifdef NO_STRERROR
98+
#define strerror(_) "System unable to describe the error"
99+
#endif
100+
101+
/* Define 'getcwd' for systems that do not implement it */
102+
#ifdef NO_GETCWD
103+
#define getcwd(p,s) NULL
104+
#define getcwd_error "Function 'getcwd' not provided by system"
105+
#else
106+
#define getcwd_error strerror(errno)
107+
#ifdef _WIN32
108+
/* MAX_PATH seems to be 260. Seems kind of small. Is there a better one? */
109+
#define LFS_MAXPATHLEN MAX_PATH
110+
#else
111+
/* For MAXPATHLEN: */
112+
#include <sys/param.h>
113+
#define LFS_MAXPATHLEN MAXPATHLEN
114+
#endif
115+
#endif
116+
117+
typedef struct dir_data {
118+
int closed;
119+
#ifdef _WIN32
120+
intptr_t hFile;
121+
char pattern[MAX_PATH+1];
122+
#else
123+
DIR *dir;
124+
#endif
125+
} dir_data;
126+
127+
#ifdef _WIN32
128+
#ifdef __BORLANDC__
129+
#define lfs_setmode(L,file,m) ((void)L, setmode(_fileno(file), m))
130+
#define STAT_STRUCT struct stati64
131+
#else
132+
#define lfs_setmode(L,file,m) ((void)L, _setmode(_fileno(file), m))
133+
#define STAT_STRUCT struct _stati64
134+
#endif
135+
#define STAT_FUNC _stati64
136+
#define LSTAT_FUNC STAT_FUNC
137+
#else
138+
#define _O_TEXT 0
139+
#define _O_BINARY 0
140+
#define lfs_setmode(L,file,m) ((void)L, (void)file, (void)m, 0)
141+
#define STAT_STRUCT struct stat
142+
#define STAT_FUNC stat
143+
#define LSTAT_FUNC lstat
144+
#endif
145+
146+
#ifdef _WIN32
147+
#ifndef S_ISDIR
148+
#define S_ISDIR(mode) (mode&_S_IFDIR)
149+
#endif
150+
#ifndef S_ISREG
151+
#define S_ISREG(mode) (mode&_S_IFREG)
152+
#endif
153+
#ifndef S_ISLNK
154+
#define S_ISLNK(mode) (0)
155+
#endif
156+
#ifndef S_ISSOCK
157+
#define S_ISSOCK(mode) (0)
158+
#endif
159+
#ifndef S_ISFIFO
160+
#define S_ISFIFO(mode) (0)
161+
#endif
162+
#ifndef S_ISCHR
163+
#define S_ISCHR(mode) (mode&_S_IFCHR)
164+
#endif
165+
#ifndef S_ISBLK
166+
#define S_ISBLK(mode) (0)
167+
#endif
168+
#endif
169+
170+
enum _filetype {
171+
FILETYPE_NONE = -2,
172+
FILETYPE_UNKNOWN = -1,
173+
FILETYPE_FILE = 1,
174+
FILETYPE_DIRECTORY,
175+
FILETYPE_LINK,
176+
FILETYPE_SOCKET,
177+
FILETYPE_NAMEDPIPE,
178+
FILETYPE_CHAR_DEVICE,
179+
FILETYPE_BLOCK_DEVICE
180+
};
181+
182+
namespace DFHack {
183+
namespace Filesystem {
184+
DFHACK_EXPORT bool chdir (std::string path);
185+
DFHACK_EXPORT char * getcwd ();
186+
DFHACK_EXPORT bool mkdir (std::string path);
187+
DFHACK_EXPORT bool rmdir (std::string path);
188+
DFHACK_EXPORT bool stat (std::string path, STAT_STRUCT &info);
189+
DFHACK_EXPORT bool exists (std::string path);
190+
DFHACK_EXPORT _filetype filetype (std::string path);
191+
DFHACK_EXPORT bool isfile (std::string path);
192+
DFHACK_EXPORT bool isdir (std::string path);
193+
}
194+
}

library/modules/Filesystem.cpp

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
https://github.com/peterix/dfhack
3+
Copyright (c) 2009-2012 Petr Mrázek ([email protected])
4+
5+
This software is provided 'as-is', without any express or implied
6+
warranty. In no event will the authors be held liable for any
7+
damages arising from the use of this software.
8+
9+
Permission is granted to anyone to use this software for any
10+
purpose, including commercial applications, and to alter it and
11+
redistribute it freely, subject to the following restrictions:
12+
13+
1. The origin of this software must not be misrepresented; you must
14+
not claim that you wrote the original software. If you use this
15+
software in a product, an acknowledgment in the product documentation
16+
would be appreciated but is not required.
17+
18+
2. Altered source versions must be plainly marked as such, and
19+
must not be misrepresented as being the original software.
20+
21+
3. This notice may not be removed or altered from any source
22+
distribution.
23+
*/
24+
/* Based on luafilesystem
25+
Copyright © 2003-2014 Kepler Project.
26+
27+
Permission is hereby granted, free of charge, to any person
28+
obtaining a copy of this software and associated documentation
29+
files (the "Software"), to deal in the Software without
30+
restriction, including without limitation the rights to use, copy,
31+
modify, merge, publish, distribute, sublicense, and/or sell copies
32+
of the Software, and to permit persons to whom the Software is
33+
furnished to do so, subject to the following conditions:
34+
35+
The above copyright notice and this permission notice shall be
36+
included in all copies or substantial portions of the Software.
37+
38+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
39+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
40+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
41+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
42+
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
43+
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
45+
SOFTWARE.
46+
*/
47+
48+
#include <string>
49+
50+
#include "modules/Filesystem.h"
51+
52+
bool DFHack::Filesystem::chdir (std::string path)
53+
{
54+
return !(bool)::chdir(path.c_str());
55+
}
56+
57+
char * DFHack::Filesystem::getcwd ()
58+
{
59+
char *path;
60+
char buf[LFS_MAXPATHLEN];
61+
if ((path = ::getcwd(buf, LFS_MAXPATHLEN)) == NULL)
62+
return NULL;
63+
else
64+
return path;
65+
}
66+
67+
bool DFHack::Filesystem::mkdir (std::string path)
68+
{
69+
int fail;
70+
#ifdef _WIN32
71+
fail = ::_mkdir(path.c_str());
72+
#else
73+
fail = ::mkdir(path.c_str(), S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP |
74+
S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH);
75+
#endif
76+
return !(bool)fail;
77+
}
78+
79+
bool DFHack::Filesystem::rmdir (std::string path)
80+
{
81+
int fail;
82+
#ifdef _WIN32
83+
fail = ::_rmdir(path.c_str());
84+
#else
85+
fail = ::rmdir(path.c_str());
86+
#endif
87+
return !(bool)fail;
88+
}
89+
90+
#ifdef _WIN32
91+
_filetype *mode2type (unsigned short mode) {
92+
#else
93+
_filetype mode2type (mode_t mode) {
94+
#endif
95+
if (S_ISREG(mode))
96+
return FILETYPE_FILE;
97+
else if (S_ISDIR(mode))
98+
return FILETYPE_DIRECTORY;
99+
else if (S_ISLNK(mode))
100+
return FILETYPE_LINK;
101+
else if (S_ISSOCK(mode))
102+
return FILETYPE_SOCKET;
103+
else if (S_ISFIFO(mode))
104+
return FILETYPE_NAMEDPIPE;
105+
else if (S_ISCHR(mode))
106+
return FILETYPE_CHAR_DEVICE;
107+
else if (S_ISBLK(mode))
108+
return FILETYPE_BLOCK_DEVICE;
109+
else
110+
return FILETYPE_UNKNOWN;
111+
}
112+
113+
bool DFHack::Filesystem::stat (std::string path, STAT_STRUCT &info)
114+
{
115+
return !(bool)(STAT_FUNC(path.c_str(), &info));
116+
}
117+
118+
bool DFHack::Filesystem::exists (std::string path)
119+
{
120+
STAT_STRUCT info;
121+
return (bool)DFHack::Filesystem::stat(path.c_str(), info);
122+
}
123+
124+
#include <iostream>
125+
_filetype DFHack::Filesystem::filetype (std::string path)
126+
{
127+
STAT_STRUCT info;
128+
DFHack::Filesystem::stat(path, info);
129+
std::cout << info.st_mode << std::endl;
130+
return mode2type(info.st_mode);
131+
}
132+
133+
bool DFHack::Filesystem::isfile (std::string path)
134+
{
135+
return DFHack::Filesystem::filetype(path) == FILETYPE_FILE;
136+
}
137+
138+
bool DFHack::Filesystem::isdir (std::string path)
139+
{
140+
return DFHack::Filesystem::filetype(path) == FILETYPE_DIRECTORY;
141+
}

0 commit comments

Comments
 (0)