Skip to content

Commit bbf89ca

Browse files
committed
CHANGE: DELETE returning false if called on not existing file or directory
resolves: Oldes/Rebol-issues#2447
1 parent 1864b67 commit bbf89ca

File tree

5 files changed

+34
-6
lines changed

5 files changed

+34
-6
lines changed

src/core/p-dir.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -251,8 +251,11 @@
251251
// !!! add recursive delete (?)
252252
result = OS_DO_DEVICE(&dir, RDC_DELETE);
253253
///OS_FREE(dir.file.path);
254-
if (result < 0) Trap1(RE_NO_DELETE, path);
255-
return R_ARG2;
254+
if (result >= 0) return R_ARG2;
255+
if (result == -2) return R_FALSE;
256+
// else...
257+
Trap1(RE_NO_DELETE, path);
258+
break;
256259

257260
case A_OPEN:
258261
// !! If open fails, what if user does a READ w/o checking for error?

src/core/p-file.c

+7-2
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,7 @@ REBINT Mode_Syms[] = {
416416
REBREQ *file = 0;
417417
REBCNT args = 0;
418418
REBCNT len;
419+
REBINT result;
419420
REBOOL opened = FALSE; // had to be opened (shortcut case)
420421

421422
//Print("FILE ACTION: %r", Get_Action_Word(action));
@@ -542,9 +543,13 @@ REBINT Mode_Syms[] = {
542543
break;
543544

544545
case A_DELETE:
545-
if (IS_OPEN(file)) Trap1(RE_NO_DELETE, path);
546+
if (IS_OPEN(file)) Trap1(RE_NO_DELETE, path); // it's not allowed to delete opened file port
546547
Setup_File(file, 0, path);
547-
if (OS_DO_DEVICE(file, RDC_DELETE) < 0 ) Trap1(RE_NO_DELETE, path);
548+
result = OS_DO_DEVICE(file, RDC_DELETE);
549+
if (result >= 0) return R_RET; // returns port so it can be used in chained evaluation
550+
if (result == -2) return R_FALSE;
551+
// else...
552+
Trap1(RE_NO_DELETE, path);
548553
break;
549554

550555
case A_RENAME:

src/os/host-device.c

+5
Original file line numberDiff line numberDiff line change
@@ -337,6 +337,11 @@ static int Poll_Default(REBDEV *dev)
337337
Signal_Device(req, EVT_ERROR);
338338
}
339339
}
340+
else if (result < 0) {
341+
result = req->error;
342+
// make sure that we are consistent and error is always negative...
343+
if (result > 0) result = -result;
344+
}
340345

341346
return result;
342347
}

src/os/posix/dev-file.c

-2
Original file line numberDiff line numberDiff line change
@@ -568,8 +568,6 @@ static int Get_File_Info(REBREQ *file)
568568

569569
file->error = errno;
570570
return DR_ERROR;
571-
572-
return 0;
573571
}
574572

575573

src/tests/units/port-test.r3

+17
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ Rebol [
135135
not error? delete-dir %units/temp-dir/
136136
not exists? %units/temp-dir/
137137
]
138+
;@@ https://github.com/Oldes/Rebol-issues/issues/2447
139+
--assert false? try [delete %not-exists/]
140+
--assert error? try [delete %/]
141+
138142
--test-- "CHANGE-DIR"
139143
;@@ https://github.com/Oldes/Rebol-issues/issues/2446
140144
--assert what-dir = change-dir %.
@@ -330,6 +334,19 @@ if system/platform = 'Windows [
330334
"test" = read/string %issue-446.txt
331335
not error? try [delete %issue-446.txt]
332336
]
337+
--test-- "DELETE file"
338+
;@@ https://github.com/Oldes/Rebol-issues/issues/2447
339+
--assert false? try [delete %not-exists]
340+
; create locked file...
341+
p: open %issue-2447
342+
; should not be possible to delete it..
343+
--assert error? try [delete %issue-2447]
344+
; close the file handle...
345+
close p
346+
; now it may be deleted..
347+
--assert port? try [delete %issue-2447]
348+
; validate...
349+
--assert not exists? %issue-2447
333350

334351
===end-group===
335352

0 commit comments

Comments
 (0)