Skip to content

Commit

Permalink
FIX: result of to-real-file not containing a trailing slash for dir…
Browse files Browse the repository at this point in the history
…ectories on Posix

resolves: Oldes/Rebol-issues#2600
  • Loading branch information
Oldes committed Jun 3, 2024
1 parent 3cc232a commit a2ec872
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
2 changes: 0 additions & 2 deletions src/core/n-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -500,13 +500,11 @@ static REBSER *Read_All_File(char *fname)
return R_NONE;
}
// Try to call realpath on posix or _fullpath on Windows
// Returned string must be released once done!
tmp = OS_REAL_PATH((REBCHR*)SERIES_DATA(ser));
if (!tmp) return R_NONE;

// Convert OS native wide string back to Rebol file type
new = To_REBOL_Path(tmp, 0, OS_WIDE, FALSE);
OS_FREE(tmp);
if (!new) return R_NONE;

Set_Series(REB_FILE, D_RET, new);
Expand Down
18 changes: 14 additions & 4 deletions src/os/posix/host-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <time.h>
#include <string.h>
#include <errno.h>
Expand Down Expand Up @@ -426,7 +427,7 @@ RL_LIB *RL; // Link back to reb-lib from embedded extensions (like for now: host
**
***********************************************************************/
{
if (!errnum) errnum = errno;
if (!errnum) errnum = errno;
strerror_r(errnum, s_cast(str), len);
return str;
}
Expand Down Expand Up @@ -696,11 +697,20 @@ RL_LIB *RL; // Link back to reb-lib from embedded extensions (like for now: host
** symbolic links are resolved, as are . and .. pathname components.
** Consecutive slash (/) characters are replaced by a single slash.
**
** The result should be freed after copy/conversion.
**
***********************************************************************/
{
return realpath(path, NULL); // Be sure to call free() after usage
static char result[PATH_MAX+2];
struct stat path_stat;
if (realpath(path, result) == NULL) return NULL;
if (stat(result, &path_stat) != 0) return NULL;

size_t len = strlen(result);
// Append the trailing slash if it is a directory
if (S_ISDIR(path_stat.st_mode)) {
result[len++] = '/';
}
result[len] = 0;
return (char*)result; // Be sure to copy or process the result soon!
}


Expand Down
16 changes: 13 additions & 3 deletions src/os/win32/host-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -678,11 +678,21 @@ void Dispose_Windows(void);
** symbolic links are resolved, as are . and .. pathname components.
** Consecutive slash (/) characters are replaced by a single slash.
**
** The result should be freed after copy/conversion.
**
***********************************************************************/
{
return _wfullpath(NULL, path, MAX_PATH);
static REBCHR result[MAX_PATH+2];
if (!_wfullpath(result, path, MAX_PATH)) return NULL;
size_t len = wcslen(result);
// if there is not a trailing slash, check if the result is not a directory anyway
if (result[len-1] != L'\\') {
// and append the slash, if it is...
// https://github.com/Oldes/Rebol-issues/issues/2600
DWORD fileAttr = GetFileAttributes(result);
if (fileAttr & FILE_ATTRIBUTE_DIRECTORY)
result[len++] = L'\\';
}
result[len] = 0;
return (REBCHR*)result; // Be sure to copy or process the result soon!
}

/***********************************************************************
Expand Down
12 changes: 12 additions & 0 deletions src/tests/units/file-test.r3
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ if find [Linux macOS] system/platform [

===end-group===


===start-group=== "to-real-file"
--test-- "On directory"
;@@ https://github.com/Oldes/Rebol-issues/issues/2600
--assert equal? what-dir to-real-file %.
--assert equal? what-dir to-real-file %./
--assert equal? what-dir to-real-file %./units/files/../../

===end-group===



===start-group=== "suffix?"
--test-- "suffix? file!"
--assert %.c = suffix? %b.c
Expand Down

0 comments on commit a2ec872

Please sign in to comment.