Skip to content

Commit 985e4cc

Browse files
authored
Merge pull request #323 from fglock/fix/mkdir-eexist
Fix mkdir to set $! errno correctly (EEXIST)
2 parents 892ae94 + 7d9fabf commit 985e4cc

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

src/main/java/org/perlonjava/runtime/operators/Directory.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ public static RuntimeScalar mkdir(RuntimeList args) {
252252

253253
try {
254254
Path path = RuntimeIO.resolvePath(fileName);
255-
Files.createDirectories(path);
255+
// Use createDirectory (not createDirectories) so it throws FileAlreadyExistsException
256+
// when the directory exists. This matches Perl's behavior where mkdir() fails
257+
// with EEXIST if the directory already exists.
258+
Files.createDirectory(path);
256259

257260
// Set permissions only if the file system supports POSIX permissions
258261
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
@@ -263,9 +266,9 @@ public static RuntimeScalar mkdir(RuntimeList args) {
263266

264267
return scalarTrue;
265268
} catch (IOException e) {
266-
// Set $! (errno) in case of failure
267-
getGlobalVariable("main::!").set(e.getMessage());
268-
return scalarFalse;
269+
// Set $! (errno) properly using handleIOException which maps
270+
// FileAlreadyExistsException to EEXIST (17), etc.
271+
return handleIOException(e, fileName, 0);
269272
}
270273
}
271274
}

0 commit comments

Comments
 (0)