From 5c303d44bf094b777fb7d8eae5b4ce9cc00c597b Mon Sep 17 00:00:00 2001 From: Maxime Devos Date: Fri, 12 Mar 2021 15:10:23 +0100 Subject: [PATCH 10/17] Define delete-file-at when unlinkat exists. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit unlinkat is used for both unlinking regular files and removing empty directories. * configure.ac: Detect if unlinkat exists. * doc/ref/posix.texi (File System): Document why there is no ‘rmdirat’ procedure, and document the ‘delete-file-at’ procedure. * libguile/filesys.c (scm_rmdir): Adjust the docstring here as well. (scm_delete_file_at): Define a Scheme binding to ‘unlinkat’. * libguile/filesys.h (scm_delete_file_at): Make ‘scm_delete_file_at’ part of the C API. --- configure.ac | 2 +- doc/ref/posix.texi | 13 +++++++++++++ libguile/filesys.c | 33 ++++++++++++++++++++++++++++++++- libguile/filesys.h | 1 + 4 files changed, 47 insertions(+), 2 deletions(-) diff --git a/configure.ac b/configure.ac index 20357ce3b..dea94a364 100644 --- a/configure.ac +++ b/configure.ac @@ -482,7 +482,7 @@ AC_CHECK_HEADERS([assert.h crt_externs.h]) # AC_CHECK_FUNCS([DINFINITY DQNAN cexp chsize clog clog10 ctermid \ fesetround ftime ftruncate fchown fchmod fchdir readlinkat \ - symlinkat mkdirat renameat fchmodat getcwd geteuid getsid \ + symlinkat mkdirat renameat fchmodat unlinkat getcwd geteuid getsid \ gettimeofday getuid getgid gmtime_r ioctl lstat mkdir mkdtemp mknod \ nice readlink rename rmdir setegid seteuid \ setlocale setuid setgid setpgid setsid sigaction siginterrupt stat64 \ diff --git a/doc/ref/posix.texi b/doc/ref/posix.texi index 2bc067f74..dcea0352a 100644 --- a/doc/ref/posix.texi +++ b/doc/ref/posix.texi @@ -878,6 +878,18 @@ Deletes (or ``unlinks'') the file whose path is specified by @var{str}. @end deffn +@findex unlinkat +@deffn {Scheme Procedure} delete-file-at dir str [flags] +@deffnx {C Function} scm_delete_file_at (dir, str, flags) +Like @code{unlink}, but resolve @var{str} relative to the +directory referred to by the file port @var{dir} instead. + +The optional @var{flags} argument can be @code{AT_REMOVEDIR}, +in which case @code{delete-file-at} will act like @code{rmdir} instead +of @code{delete-file}. Why doesn't POSIX have a @code{rmdirat} function +for this instead? No idea! +@end deffn + @deffn {Scheme Procedure} copy-file oldfile newfile @deffnx {C Function} scm_copy_file (oldfile, newfile) Copy the file specified by @var{oldfile} to @var{newfile}. @@ -968,6 +980,7 @@ referred to by the file port @var{dir} instead. @deffnx {C Function} scm_rmdir (path) Remove the existing directory named by @var{path}. The directory must be empty for this to succeed. The return value is unspecified. +There is no @code{rmdirat} procedure; use @code{delete-file-at} instead. @end deffn @deffn {Scheme Procedure} opendir dirname diff --git a/libguile/filesys.c b/libguile/filesys.c index 52380cd20..baa149a33 100644 --- a/libguile/filesys.c +++ b/libguile/filesys.c @@ -1425,7 +1425,8 @@ SCM_DEFINE (scm_mkdirat, "mkdirat", 2, 1, 0, SCM_DEFINE (scm_rmdir, "rmdir", 1, 0, 0, (SCM path), "Remove the existing directory named by @var{path}. The directory must\n" - "be empty for this to succeed. The return value is unspecified.") + "be empty for this to succeed. The return value is unspecified.\n" + "There is no @code{rmdirat} procedure, use @code{delete-file-at} instead.") #define FUNC_NAME s_scm_rmdir { int val; @@ -1495,6 +1496,36 @@ SCM_DEFINE (scm_delete_file, "delete-file", 1, 0, 0, } #undef FUNC_NAME +SCM_DEFINE (scm_delete_file_at, "delete-file-at", 2, 1, 0, + (SCM dir, SCM str, SCM flags), + "Like @code{unlink}, but resolve @var{str} relative to the\n" + "directory referred to by the file port @var{dir} instead.\n\n" + "The optional @var{flags} argument can be @code{AT_REMOVEDIR},\n" + "in which case @code{delete-file-at} will act like @code{rmdir} instead\n" + "of @code{delete-file}. Why doesn't POSIX have a @code{rmdirat} function\n" + "for this instead? No idea!") +#define FUNC_NAME s_scm_delete_file_at +{ + int ans; + int dir_fdes; + int c_flags; + + if (SCM_UNBNDP (flags)) + c_flags = 0; + else + c_flags = scm_to_int (flags); + + SCM_VALIDATE_OPFPORT (SCM_ARG1, dir); + dir_fdes = SCM_FPORT_FDES (dir); + + STRING_SYSCALL (str, c_str, ans = unlinkat (dir_fdes, c_str, c_flags)); + scm_remember_upto_here_1 (dir); + if (ans != 0) + SCM_SYSERROR; + return SCM_UNSPECIFIED; +} +#undef FUNC_NAME + SCM_DEFINE (scm_access, "access?", 2, 0, 0, (SCM path, SCM how), "Test accessibility of a file under the real UID and GID of the\n" diff --git a/libguile/filesys.h b/libguile/filesys.h index 377a3795e..37d084cd5 100644 --- a/libguile/filesys.h +++ b/libguile/filesys.h @@ -51,6 +51,7 @@ SCM_API SCM scm_link (SCM oldpath, SCM newpath); SCM_API SCM scm_rename (SCM oldname, SCM newname); SCM_API SCM scm_renameat (SCM olddir, SCM oldname, SCM newdir, SCM newname); SCM_API SCM scm_delete_file (SCM str); +SCM_API SCM scm_delete_file_at (SCM dir, SCM str, SCM flags); SCM_API SCM scm_mkdir (SCM path, SCM mode); SCM_API SCM scm_mkdirat (SCM dir, SCM path, SCM mode); SCM_API SCM scm_rmdir (SCM path); -- 2.30.2