Skip to content

Commit

Permalink
FIX: better results from change-dir function (on error and also on …
Browse files Browse the repository at this point in the history
…success).

resolves: Oldes/Rebol-issues#2446
  • Loading branch information
Oldes committed Feb 19, 2021
1 parent 60fb76f commit 1864b67
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 11 deletions.
15 changes: 12 additions & 3 deletions src/core/n-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -551,17 +551,26 @@ static REBSER *Read_All_File(char *fname)
REBVAL *arg = D_ARG(1);
REBSER *ser;
REBINT n;
REBVAL val;
REBVAL val, reason;

ser = Value_To_OS_Path(arg, TRUE);
if (!ser) Trap_Arg(arg); // !!! ERROR MSG
// it should be safe not to check result from Value_To_OS_Path (it always succeeds)
//if (!ser) Trap_Arg(arg); // !!! ERROR MSG

Set_String(&val, ser); // may be unicode or utf-8
Check_Security(SYM_FILE, POL_EXEC, &val);

n = OS_SET_CURRENT_DIR((void*)ser->data); // use len for bool
if (!n) Trap_Arg(arg); // !!! ERROR MSG

// convert the full OS path back to Rebol format
// used in error or as a result
ser = Value_To_REBOL_Path(&val, TRUE);
SET_FILE(arg, ser);

if (NZ(n)) {
SET_INTEGER(&reason, n);
Trap2(RE_CANNOT_OPEN, arg, &reason);
}
return R_ARG1;
}

Expand Down
8 changes: 4 additions & 4 deletions src/os/posix/host-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,14 +646,14 @@ int pipe2(int pipefd[2], int flags); //to avoid "implicit-function-declaration"

/***********************************************************************
**
*/ REBOOL OS_Set_Current_Dir(REBCHR *path)
*/ int OS_Set_Current_Dir(REBCHR *path)
/*
** Set the current directory to local path. Return FALSE
** on failure.
** Set the current directory to local path.
** Return 0 on success else error number.
**
***********************************************************************/
{
return chdir(path) == 0;
return (chdir(path) == 0) ? 0 : errno;
}


Expand Down
8 changes: 4 additions & 4 deletions src/os/win32/host-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -642,14 +642,14 @@ static void *Task_Ready;

/***********************************************************************
**
*/ REBOOL OS_Set_Current_Dir(REBCHR *path)
*/ int OS_Set_Current_Dir(REBCHR *path)
/*
** Set the current directory to local path. Return FALSE
** on failure.
** Set the current directory to local path.
** Return 0 on success else error number.
**
***********************************************************************/
{
return SetCurrentDirectory( path[0]==0 ? L"\\" : path );
return (SetCurrentDirectory(path[0] == 0 ? L"\\" : path)) ? 0 : GetLastError();
}


Expand Down
9 changes: 9 additions & 0 deletions src/tests/units/port-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,15 @@ Rebol [
not error? delete-dir %units/temp-dir/
not exists? %units/temp-dir/
]
--test-- "CHANGE-DIR"
;@@ https://github.com/Oldes/Rebol-issues/issues/2446
--assert what-dir = change-dir %.
--assert all [
error? e: try [change-dir %issues/2446]
e/id = 'cannot-open
e/arg1 = join what-dir %issues/2446/
]

if system/platform = 'Windows [
;@@ it looks that on Linux there is no lock on opened file
--assert all [
Expand Down

0 comments on commit 1864b67

Please sign in to comment.