Skip to content

Commit 5229a22

Browse files
SmallJokerZeno-
authored andcommitted
Ignore .name directories and files
Signed-off-by: Craig Robbins <kde.psych@gmail.com>
1 parent c5f6f9f commit 5229a22

File tree

3 files changed

+77
-82
lines changed

3 files changed

+77
-82
lines changed

builtin/mainmenu/modmgr.lua

+25-23
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,32 @@
1717

1818
--------------------------------------------------------------------------------
1919
function get_mods(path,retval,modpack)
20+
local mods = core.get_dirlist(path, true)
21+
22+
for i=1, #mods, 1 do
23+
if mods[i]:sub(1,1) ~= "." then
24+
local toadd = {}
25+
local modpackfile = nil
26+
27+
toadd.name = mods[i]
28+
toadd.path = path .. DIR_DELIM .. mods[i] .. DIR_DELIM
29+
if modpack ~= nil and
30+
modpack ~= "" then
31+
toadd.modpack = modpack
32+
else
33+
local filename = path .. DIR_DELIM .. mods[i] .. DIR_DELIM .. "modpack.txt"
34+
local error = nil
35+
modpackfile,error = io.open(filename,"r")
36+
end
2037

21-
local mods = core.get_dirlist(path,true)
22-
for i=1,#mods,1 do
23-
local toadd = {}
24-
local modpackfile = nil
25-
26-
toadd.name = mods[i]
27-
toadd.path = path .. DIR_DELIM .. mods[i] .. DIR_DELIM
28-
if modpack ~= nil and
29-
modpack ~= "" then
30-
toadd.modpack = modpack
31-
else
32-
local filename = path .. DIR_DELIM .. mods[i] .. DIR_DELIM .. "modpack.txt"
33-
local error = nil
34-
modpackfile,error = io.open(filename,"r")
35-
end
36-
37-
if modpackfile ~= nil then
38-
modpackfile:close()
39-
toadd.is_modpack = true
40-
table.insert(retval,toadd)
41-
get_mods(path .. DIR_DELIM .. mods[i],retval,mods[i])
42-
else
43-
table.insert(retval,toadd)
38+
if modpackfile ~= nil then
39+
modpackfile:close()
40+
toadd.is_modpack = true
41+
table.insert(retval,toadd)
42+
get_mods(path .. DIR_DELIM .. mods[i],retval,mods[i])
43+
else
44+
table.insert(retval,toadd)
45+
end
4446
end
4547
end
4648
end

builtin/mainmenu/tab_texturepacks.lua

+6-4
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,13 @@ local function render_texture_pack_list(list)
3131
local retval = ""
3232

3333
for i, v in ipairs(list) do
34-
if retval ~= "" then
35-
retval = retval ..","
36-
end
34+
if v:sub(1,1) ~= "." then
35+
if retval ~= "" then
36+
retval = retval ..","
37+
end
3738

38-
retval = retval .. core.formspec_escape(v)
39+
retval = retval .. core.formspec_escape(v)
40+
end
3941
end
4042

4143
return retval

src/filesys.cpp

+46-55
Original file line numberDiff line numberDiff line change
@@ -52,19 +52,17 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
5252

5353
DirSpec = (LPTSTR) malloc (BUFSIZE);
5454

55-
if( DirSpec == NULL )
56-
{
57-
errorstream<<"GetDirListing: Insufficient memory available"<<std::endl;
58-
retval = 1;
59-
goto Cleanup;
55+
if(DirSpec == NULL) {
56+
errorstream<<"GetDirListing: Insufficient memory available"<<std::endl;
57+
retval = 1;
58+
goto Cleanup;
6059
}
6160

6261
// Check that the input is not larger than allowed.
63-
if (pathstring.size() > (BUFSIZE - 2))
64-
{
65-
errorstream<<"GetDirListing: Input directory is too large."<<std::endl;
66-
retval = 3;
67-
goto Cleanup;
62+
if (pathstring.size() > (BUFSIZE - 2)) {
63+
errorstream<<"GetDirListing: Input directory is too large."<<std::endl;
64+
retval = 3;
65+
goto Cleanup;
6866
}
6967

7068
//_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
@@ -74,13 +72,10 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
7472
// Find the first file in the directory.
7573
hFind = FindFirstFile(DirSpec, &FindFileData);
7674

77-
if (hFind == INVALID_HANDLE_VALUE)
78-
{
75+
if (hFind == INVALID_HANDLE_VALUE) {
7976
retval = (-1);
8077
goto Cleanup;
81-
}
82-
else
83-
{
78+
} else {
8479
// NOTE:
8580
// Be very sure to not include '..' in the results, it will
8681
// result in an epic failure when deleting stuff.
@@ -92,8 +87,7 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
9287
listing.push_back(node);
9388

9489
// List all the other files in the directory.
95-
while (FindNextFile(hFind, &FindFileData) != 0)
96-
{
90+
while (FindNextFile(hFind, &FindFileData) != 0) {
9791
DirListNode node;
9892
node.name = FindFileData.cFileName;
9993
node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
@@ -103,15 +97,14 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
10397

10498
dwError = GetLastError();
10599
FindClose(hFind);
106-
if (dwError != ERROR_NO_MORE_FILES)
107-
{
100+
if (dwError != ERROR_NO_MORE_FILES) {
108101
errorstream<<"GetDirListing: FindNextFile error. Error is "
109102
<<dwError<<std::endl;
110103
retval = (-1);
111104
goto Cleanup;
112105
}
113106
}
114-
retval = 0;
107+
retval = 0;
115108

116109
Cleanup:
117110
free(DirSpec);
@@ -242,53 +235,51 @@ std::vector<DirListNode> GetDirListing(std::string pathstring)
242235
{
243236
std::vector<DirListNode> listing;
244237

245-
DIR *dp;
246-
struct dirent *dirp;
247-
if((dp = opendir(pathstring.c_str())) == NULL) {
238+
DIR *dp;
239+
struct dirent *dirp;
240+
if((dp = opendir(pathstring.c_str())) == NULL) {
248241
//infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
249-
return listing;
250-
}
242+
return listing;
243+
}
251244

252-
while ((dirp = readdir(dp)) != NULL) {
245+
while ((dirp = readdir(dp)) != NULL) {
253246
// NOTE:
254247
// Be very sure to not include '..' in the results, it will
255248
// result in an epic failure when deleting stuff.
256-
if(dirp->d_name[0]!='.'){
257-
DirListNode node;
258-
node.name = dirp->d_name;
259-
if(node.name == "." || node.name == "..")
260-
continue;
249+
if(dirp->d_name == "." || dirp->d_name == "..")
250+
continue;
251+
252+
DirListNode node;
253+
node.name = dirp->d_name;
261254

262-
int isdir = -1; // -1 means unknown
255+
int isdir = -1; // -1 means unknown
263256

264-
/*
265-
POSIX doesn't define d_type member of struct dirent and
266-
certain filesystems on glibc/Linux will only return
267-
DT_UNKNOWN for the d_type member.
257+
/*
258+
POSIX doesn't define d_type member of struct dirent and
259+
certain filesystems on glibc/Linux will only return
260+
DT_UNKNOWN for the d_type member.
268261
269-
Also we don't know whether symlinks are directories or not.
270-
*/
262+
Also we don't know whether symlinks are directories or not.
263+
*/
271264
#ifdef _DIRENT_HAVE_D_TYPE
272-
if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
273-
isdir = (dirp->d_type == DT_DIR);
265+
if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
266+
isdir = (dirp->d_type == DT_DIR);
274267
#endif /* _DIRENT_HAVE_D_TYPE */
275268

276-
/*
277-
Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
278-
If so, try stat().
279-
*/
280-
if(isdir == -1)
281-
{
282-
struct stat statbuf;
283-
if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
284-
continue;
285-
isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
286-
}
287-
node.dir = isdir;
288-
listing.push_back(node);
269+
/*
270+
Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
271+
If so, try stat().
272+
*/
273+
if(isdir == -1) {
274+
struct stat statbuf;
275+
if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
276+
continue;
277+
isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
289278
}
290-
}
291-
closedir(dp);
279+
node.dir = isdir;
280+
listing.push_back(node);
281+
}
282+
closedir(dp);
292283

293284
return listing;
294285
}

0 commit comments

Comments
 (0)