From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Maxime Devos Newsgroups: gmane.lisp.guile.devel Subject: [PATCH v2 01/14] =?UTF-8?q?Allow=20file=20ports=20in=20=E2=80=98c?= =?UTF-8?q?hdir=E2=80=99=20when=20supported.?= Date: Tue, 16 Nov 2021 11:06:24 +0000 Message-ID: <20211116110637.125579-2-maximedevos@telenet.be> References: <175c3a6572e832d84927937b309a3095cadf5702.camel@telenet.be> <20211116110637.125579-1-maximedevos@telenet.be> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="32657"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Maxime Devos To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Tue Nov 16 12:17:12 2021 Return-path: Envelope-to: guile-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1mmwSe-0008Fz-AN for guile-devel@m.gmane-mx.org; Tue, 16 Nov 2021 12:17:12 +0100 Original-Received: from localhost ([::1]:49766 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mmwSb-0000DK-Be for guile-devel@m.gmane-mx.org; Tue, 16 Nov 2021 06:17:09 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:34348) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mmwRM-00070s-ME for guile-devel@gnu.org; Tue, 16 Nov 2021 06:15:52 -0500 Original-Received: from leibniz.telenet-ops.be ([195.130.137.77]:49198) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mmwRK-0006Wg-9L for guile-devel@gnu.org; Tue, 16 Nov 2021 06:15:52 -0500 Original-Received: from baptiste.telenet-ops.be (baptiste.telenet-ops.be [IPv6:2a02:1800:120:4::f00:13]) by leibniz.telenet-ops.be (Postfix) with ESMTPS id 4Htjtb4n92zMrHtR for ; Tue, 16 Nov 2021 12:06:47 +0100 (CET) Original-Received: from localhost.localdomain ([IPv6:2a02:1811:8c09:9d00:3c5f:2eff:feb0:ba5a]) by baptiste.telenet-ops.be with bizsmtp id Jz6j2600Q4UW6Th01z6lH4; Tue, 16 Nov 2021 12:06:45 +0100 X-Mailer: git-send-email 2.30.2 In-Reply-To: <20211116110637.125579-1-maximedevos@telenet.be> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=telenet.be; s=r21; t=1637060805; bh=WGZ1zjoXdGSzQ/lYQexL0gYf9A54p3w6Km4CP8+uRtM=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=P9YXvfZRl21HQHiwJ0irgCNVvWy/r+IlIq2Zl57a+vvuij6mE1JCRpbMTvrd7mMse 2yd2Pw3obPzTbws4fPYGeGFzcKOmSo06m+CXG00XraiIOnbBDJmQ+2XQFP12AyFXob 6JGZzdZutcsthh3Vrc53T4xPEGHopmjHSEs6hj6EdN9y7e/0UUJxYy16YpKNE2wxOn gEO5arqJRKThS7vXYePzhO2Gc28uk46sjSbxp9hic/ll5ATpJml4eYysjfNAS0j08/ mBAJNAU0lv0omR/uqa1zs/tpbL3gxDjZIy+jCfN/Sqw72Graphw8iGu4qNMWC1s3El lnWooBhfntZAQ== Received-SPF: pass client-ip=195.130.137.77; envelope-from=maximedevos@telenet.be; helo=leibniz.telenet-ops.be X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_LOW=-0.7, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.io gmane.lisp.guile.devel:20946 Archived-At: * configure.ac: Check for ‘fchdir’. * libguile/filesys.c (scm_chdir): Support file ports. (scm_init_filesys): Report support of file ports. * doc/ref/posix.texi (Processes): Update accordingly. * doc/ref/guile.texi: Add copyright line for new documentation in this patch and later patches. * test-suite/tests/filesys.test ("chdir"): Test it. --- configure.ac | 3 ++- doc/ref/guile.texi | 3 ++- doc/ref/posix.texi | 5 ++++- libguile/filesys.c | 23 +++++++++++++++++++- test-suite/tests/filesys.test | 41 +++++++++++++++++++++++++++++++++++ 5 files changed, 71 insertions(+), 4 deletions(-) diff --git a/configure.ac b/configure.ac index bd49bf162..b7e4663f7 100644 --- a/configure.ac +++ b/configure.ac @@ -484,7 +484,8 @@ AC_CHECK_HEADERS([assert.h crt_externs.h]) # sendfile - non-POSIX, found in glibc # AC_CHECK_FUNCS([DINFINITY DQNAN cexp chsize clog clog10 ctermid \ - fesetround ftime ftruncate fchown fchmod getcwd geteuid getsid \ + fesetround ftime ftruncate fchown fchmod fchdir \ + 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/guile.texi b/doc/ref/guile.texi index 660b1ae90..48af1f820 100644 --- a/doc/ref/guile.texi +++ b/doc/ref/guile.texi @@ -14,7 +14,8 @@ This manual documents Guile version @value{VERSION}. Copyright (C) 1996-1997, 2000-2005, 2009-2021 Free Software Foundation, -Inc. +Inc. \\ +Copyright (C) 2021 Maxime Devos Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or diff --git a/doc/ref/posix.texi b/doc/ref/posix.texi index 7633bd5a3..7555f9319 100644 --- a/doc/ref/posix.texi +++ b/doc/ref/posix.texi @@ -2,6 +2,7 @@ @c This is part of the GNU Guile Reference Manual. @c Copyright (C) 1996, 1997, 2000, 2001, 2002, 2003, 2004, 2006, 2007, @c 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2017, 2021 Free Software Foundation, Inc. +@c Copyright (C) 2021 Maxime Devos @c See the file guile.texi for copying conditions. @node POSIX @@ -1605,7 +1606,9 @@ The return value is unspecified. @deffn {Scheme Procedure} chdir str @deffnx {C Function} scm_chdir (str) @cindex current directory -Change the current working directory to @var{str}. +Change the current working directory to @var{str}. @var{str} can be a +string containing a file name, or a port if supported by the system. +@code{(provided? 'chdir-port)} reports whether ports are supported. The return value is unspecified. @end deffn diff --git a/libguile/filesys.c b/libguile/filesys.c index 6247734e8..2a9c36a12 100644 --- a/libguile/filesys.c +++ b/libguile/filesys.c @@ -1,5 +1,6 @@ /* Copyright 1996-2002,2004,2006,2009-2019,2021 Free Software Foundation, Inc. + Copyright 2021 Maxime Devos This file is part of Guile. @@ -621,12 +622,28 @@ SCM_DEFINE (scm_link, "link", 2, 0, 0, SCM_DEFINE (scm_chdir, "chdir", 1, 0, 0, (SCM str), "Change the current working directory to @var{str}.\n" + "@var{str} can be a string containing a file name,\n" + "or a port if supported by the system.\n" + "@code{(provided? 'chdir-port)} reports whether ports " + "are supported." "The return value is unspecified.") #define FUNC_NAME s_scm_chdir { int ans; - STRING_SYSCALL (str, c_str, ans = chdir (c_str)); +#ifdef HAVE_FCHDIR + if (SCM_OPFPORTP (str)) + { + int fdes; + fdes = SCM_FPORT_FDES (str); + SCM_SYSCALL (ans = fchdir (fdes)); + scm_remember_upto_here_1 (str); + } + else +#endif + { + STRING_SYSCALL (str, c_str, ans = chdir (c_str)); + } if (ans != 0) SCM_SYSERROR; return SCM_UNSPECIFIED; @@ -2066,5 +2083,9 @@ scm_init_filesys () scm_dot_string = scm_from_utf8_string ("."); +#ifdef HAVE_FCHDIR + scm_add_feature("chdir-port"); +#endif + #include "filesys.x" } diff --git a/test-suite/tests/filesys.test b/test-suite/tests/filesys.test index 6fed981e5..6b09a2ba0 100644 --- a/test-suite/tests/filesys.test +++ b/test-suite/tests/filesys.test @@ -1,6 +1,7 @@ ;;;; filesys.test --- test file system functions -*- scheme -*- ;;;; ;;;; Copyright (C) 2004, 2006, 2013, 2019, 2021 Free Software Foundation, Inc. +;;;; Copyright (C) 2021 Maxime Devos ;;;; ;;;; This library is free software; you can redistribute it and/or ;;;; modify it under the terms of the GNU Lesser General Public @@ -265,3 +266,43 @@ (result (eqv? 'directory (stat:type _stat)))) (false-if-exception (rmdir name)) result))))) + +(with-test-prefix "chdir" + (pass-if-equal "current directory" (getcwd) + (begin (chdir ".") (getcwd))) + (define file (search-path %load-path "ice-9/boot-9.scm")) + + + (pass-if-equal "test directory" (dirname file) + (let ((olddir (getcwd)) + (dir #f)) + (chdir (dirname file)) + (set! dir (getcwd)) + (chdir olddir) + dir)) + + (pass-if-equal "test directory, via port" (dirname file) + (unless (provided? 'chdir-port) + (throw 'unresolved)) + (let ((olddir (getcwd)) + (port (open (dirname file) O_RDONLY)) + (dir #f)) + (chdir port) + (set! dir (getcwd)) + (chdir olddir) + dir)) + + (pass-if-exception "closed port" exception:wrong-type-arg + (unless (provided? 'chdir-port) + (throw 'unresolved)) + (let ((port (open (dirname file) O_RDONLY)) + (olddir (getcwd))) + (close-port port) + (chdir port) + (chdir olddir))) ; should not be reached + + (pass-if-exception "not a port or file name" exception:wrong-type-arg + (chdir '(stuff))) + + (pass-if-exception "non-file port" exception:wrong-type-arg + (chdir (open-input-string "")))) -- 2.30.2