Skip to content

Commit

Permalink
FEAT: new to-real-file native (posix version) for resolving canonic…
Browse files Browse the repository at this point in the history
…alized filenames (removing `..` and `.` path components, simplification of sequences of multiple slashes, removal of trailing slashes, and the resolution of symbolic links).
  • Loading branch information
Oldes committed Oct 12, 2021
1 parent e108cfc commit 31e7c24
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/core/n-io.c
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,36 @@ static REBSER *Read_All_File(char *fname)
return R_RET;
}

/***********************************************************************
**
*/ REBNATIVE(to_real_file)
/*
// to-real-file: native [
// "Returns canonicalized absolute pathname or none if path does not exists. Resolves symbolic links."
// path [file! string!]
// ]
***********************************************************************/
{
REBVAL *path = D_ARG(1);
REBINT len;
REBSER *ser;
REBSER *new;
char *tmp;

ser = Value_To_OS_Path(path, TRUE);
tmp = OS_REAL_PATH(cs_cast(VAL_BIN(path)));
if(!tmp) {
FREE_SERIES(ser);
return R_NONE;
}
len = strlen(tmp);
new = To_REBOL_Path(tmp, len, OS_WIDE, FALSE);
Set_Series(REB_FILE, D_RET, new);
FREE_SERIES(ser);
FREE_MEM(tmp);
return R_RET;
}

// Blog: http://www.rebol.net/cgi-bin/r3blog.r?view=0319
/***********************************************************************
**
Expand Down
17 changes: 17 additions & 0 deletions src/os/posix/host-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -663,6 +663,23 @@ int pipe2(int pipefd[2], int flags); //to avoid "implicit-function-declaration"
}


/***********************************************************************
**
*/ char* OS_Real_Path(const char *path)
/*
** Returns a null-terminated string containing the canonicalized
** absolute pathname corresponding to path. In the returned string,
** 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
}


/***********************************************************************
**
*/ void OS_File_Time(REBREQ *file, REBOL_DAT *dat)
Expand Down
15 changes: 15 additions & 0 deletions src/os/win32/host-lib.c
Original file line number Diff line number Diff line change
Expand Up @@ -658,6 +658,21 @@ static void *Task_Ready;
return GetLastError();
}

/***********************************************************************
**
*/ char* OS_Real_Path(const char *path)
/*
** Returns a null-terminated string containing the canonicalized
** absolute pathname corresponding to path. In the returned string,
** 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 NULL; // not yet implemented
}

/***********************************************************************
**
Expand Down

0 comments on commit 31e7c24

Please # to comment.