Skip to content

Commit bbed124

Browse files
committed
Make lfs.dir error out when the directory can't be opened
Both platform implementations of DirectoryIterator swallow open failures and produce an empty iteration, but require 'lfs' in automation scripts transparently resolves to aegisub.lfs, and vanilla lfs.dir raises "cannot open <path>: <reason>" immediately. Scripts using pcall lfs.dir to detect missing or unreadable directories took the wrong branch under Aegisub. Validate the path in dir_new rather than changing DirectoryIterator, since the rest of the app plausibly relies on silently scanning possibly-missing directories. This also makes the error path in lfs.moon's dir reachable, which until now was dead code on every platform.
1 parent 38e57a3 commit bbed124

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

automation/tests/modules/lfs.moon

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,3 +85,16 @@ describe 'lfs', ->
8585
os.remove name .. '/a'
8686
os.remove name .. '/b'
8787
lfs.rmdir name
88+
89+
it 'should error for a nonexistent path like vanilla lfs', ->
90+
ok, err = pcall -> lfs.dir temp_name!
91+
assert.is.false ok
92+
assert.is.truthy err\find 'cannot open', 1, true
93+
94+
it 'should error when given a file rather than a directory', ->
95+
name = temp_name!
96+
lfs.touch name
97+
ok, err = pcall -> lfs.dir name
98+
assert.is.false ok
99+
assert.is.truthy err\find 'cannot open', 1, true
100+
os.remove name

libaegisub/lua/modules/lfs.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include "libaegisub/lua/ffi.h"
1919

2020
#include <chrono>
21+
#include <stdexcept>
22+
#include <system_error>
2123

2224
using namespace agi::fs;
2325
using namespace agi::lua;
@@ -94,6 +96,12 @@ void dir_free(DirectoryIterator *it, char **) {
9496

9597
DirectoryIterator *dir_new(const char *path, char **err) {
9698
return wrap(err, [=]{
99+
// DirectoryIterator gives a silent empty iteration when the directory
100+
// can't be opened, but lfs.dir is documented to error out instead
101+
std::error_code ec;
102+
sfs::directory_iterator(agi::fs::path(path), ec);
103+
if (ec)
104+
throw std::runtime_error("cannot open " + std::string(path) + ": " + ec.message());
97105
return new DirectoryIterator(path, "");
98106
});
99107
}

0 commit comments

Comments
 (0)