Skip to content

Commit

Permalink
CHANGE: DELETE returning false if called on not existing file or …
Browse files Browse the repository at this point in the history
…directory

resolves: Oldes/Rebol-issues#2447
  • Loading branch information
Oldes committed Feb 21, 2021
1 parent 1864b67 commit bbf89ca
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 6 deletions.
7 changes: 5 additions & 2 deletions src/core/p-dir.c
Original file line number Diff line number Diff line change
Expand Up @@ -251,8 +251,11 @@
// !!! add recursive delete (?)
result = OS_DO_DEVICE(&dir, RDC_DELETE);
///OS_FREE(dir.file.path);
if (result < 0) Trap1(RE_NO_DELETE, path);
return R_ARG2;
if (result >= 0) return R_ARG2;
if (result == -2) return R_FALSE;
// else...
Trap1(RE_NO_DELETE, path);
break;

case A_OPEN:
// !! If open fails, what if user does a READ w/o checking for error?
Expand Down
9 changes: 7 additions & 2 deletions src/core/p-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,7 @@ REBINT Mode_Syms[] = {
REBREQ *file = 0;
REBCNT args = 0;
REBCNT len;
REBINT result;
REBOOL opened = FALSE; // had to be opened (shortcut case)

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

case A_DELETE:
if (IS_OPEN(file)) Trap1(RE_NO_DELETE, path);
if (IS_OPEN(file)) Trap1(RE_NO_DELETE, path); // it's not allowed to delete opened file port
Setup_File(file, 0, path);
if (OS_DO_DEVICE(file, RDC_DELETE) < 0 ) Trap1(RE_NO_DELETE, path);
result = OS_DO_DEVICE(file, RDC_DELETE);
if (result >= 0) return R_RET; // returns port so it can be used in chained evaluation
if (result == -2) return R_FALSE;
// else...
Trap1(RE_NO_DELETE, path);
break;

case A_RENAME:
Expand Down
5 changes: 5 additions & 0 deletions src/os/host-device.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ static int Poll_Default(REBDEV *dev)
Signal_Device(req, EVT_ERROR);
}
}
else if (result < 0) {
result = req->error;
// make sure that we are consistent and error is always negative...
if (result > 0) result = -result;
}

return result;
}
Expand Down
2 changes: 0 additions & 2 deletions src/os/posix/dev-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -568,8 +568,6 @@ static int Get_File_Info(REBREQ *file)

file->error = errno;
return DR_ERROR;

return 0;
}


Expand Down
17 changes: 17 additions & 0 deletions src/tests/units/port-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,10 @@ Rebol [
not error? delete-dir %units/temp-dir/
not exists? %units/temp-dir/
]
;@@ https://github.com/Oldes/Rebol-issues/issues/2447
--assert false? try [delete %not-exists/]
--assert error? try [delete %/]

--test-- "CHANGE-DIR"
;@@ https://github.com/Oldes/Rebol-issues/issues/2446
--assert what-dir = change-dir %.
Expand Down Expand Up @@ -330,6 +334,19 @@ if system/platform = 'Windows [
"test" = read/string %issue-446.txt
not error? try [delete %issue-446.txt]
]
--test-- "DELETE file"
;@@ https://github.com/Oldes/Rebol-issues/issues/2447
--assert false? try [delete %not-exists]
; create locked file...
p: open %issue-2447
; should not be possible to delete it..
--assert error? try [delete %issue-2447]
; close the file handle...
close p
; now it may be deleted..
--assert port? try [delete %issue-2447]
; validate...
--assert not exists? %issue-2447

===end-group===

Expand Down

0 comments on commit bbf89ca

Please sign in to comment.