From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mp12.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by ms0.migadu.com with LMTPS id MC/WAWeEG2L7PQAAgWs5BA (envelope-from ) for ; Sun, 27 Feb 2022 15:02:15 +0100 Received: from aspmx1.migadu.com ([2001:41d0:2:bcc0::]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)) by mp12.migadu.com with LMTPS id kMb2OWaEG2KMFgAAauVa8A (envelope-from ) for ; Sun, 27 Feb 2022 15:02:14 +0100 Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by aspmx1.migadu.com (Postfix) with ESMTPS id A35A1431C9 for ; Sun, 27 Feb 2022 15:02:14 +0100 (CET) Received: from localhost ([::1]:41508 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nOK7p-0003MP-SU for larch@yhetil.org; Sun, 27 Feb 2022 09:02:13 -0500 Received: from eggs.gnu.org ([209.51.188.92]:50668) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOK0w-0000WN-8i for guix-patches@gnu.org; Sun, 27 Feb 2022 08:55:06 -0500 Received: from debbugs.gnu.org ([209.51.188.43]:35053) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nOK0u-0001sr-7d for guix-patches@gnu.org; Sun, 27 Feb 2022 08:55:05 -0500 Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1nOK0u-0004u6-7N for guix-patches@gnu.org; Sun, 27 Feb 2022 08:55:04 -0500 X-Loop: help-debbugs@gnu.org Subject: [bug#54180] [PATCH 05/12] home: symlink-manager: Remove 'empty-directory?' and avoid TOCTTOU race. Resent-From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Original-Sender: "Debbugs-submit" Resent-CC: guix-patches@gnu.org Resent-Date: Sun, 27 Feb 2022 13:55:04 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 54180 X-GNU-PR-Package: guix-patches X-GNU-PR-Keywords: patch To: 54180@debbugs.gnu.org Cc: Ludovic =?UTF-8?Q?Court=C3=A8s?= Received: via spool by 54180-submit@debbugs.gnu.org id=B54180.164597006818724 (code B ref 54180); Sun, 27 Feb 2022 13:55:04 +0000 Received: (at 54180) by debbugs.gnu.org; 27 Feb 2022 13:54:28 +0000 Received: from localhost ([127.0.0.1]:57166 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nOK0J-0004rv-LS for submit@debbugs.gnu.org; Sun, 27 Feb 2022 08:54:28 -0500 Received: from eggs.gnu.org ([209.51.188.92]:44848) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1nOK0A-0004qF-Pl for 54180@debbugs.gnu.org; Sun, 27 Feb 2022 08:54:20 -0500 Received: from [2001:470:142:3::e] (port=50236 helo=fencepost.gnu.org) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nOJzv-0001ZV-Br; Sun, 27 Feb 2022 08:54:08 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=MIME-Version:References:In-Reply-To:Date:Subject:To: From; bh=912F7cZQA0XDyDLS2BIPLh5lsHJs3DYf4xpW2EWNTMk=; b=a+HhsUrXz6P95nIxNFkq cDMy0qyHEYqYTzzPC/usN4FAx6WQA/OjquVUkXtytrhBr8/7gb+rnua6oKVBhBNA3QGwqiiZiea3X 9cIGvsEOB/7YqJ9901fq94fXa9T0oBmNClJzOA15heWiGEuJXijTMb1Mirwj2R5FYEE4gqLcG3Hm8 0areuBCCjD31ivzVbbs9eaBiekV9zRUbAaqSIqXIMkLVn6hTxplnNyjSRdFwU4ZP+XFiJUXtoefPS 5Dgubyw12Msx8VSKpjjqrELmIn+X+g7cW0xDZ8KPC/5HdxGxVuZhC5RS3b8VoVoFLfejhn+q1tCx0 +07i4sBeTWeDTA==; Received: from 91-160-117-201.subs.proxad.net ([91.160.117.201]:55804 helo=gnu.org) by fencepost.gnu.org with esmtpsa (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1nOJzu-0007Pg-L8; Sun, 27 Feb 2022 08:54:03 -0500 From: Ludovic =?UTF-8?Q?Court=C3=A8s?= Date: Sun, 27 Feb 2022 14:53:35 +0100 Message-Id: <20220227135342.10296-5-ludo@gnu.org> X-Mailer: git-send-email 2.34.0 In-Reply-To: <20220227135342.10296-1-ludo@gnu.org> References: <20220227135342.10296-1-ludo@gnu.org> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: guix-patches@gnu.org List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guix-patches-bounces+larch=yhetil.org@gnu.org Sender: "Guix-patches" X-Migadu-Flow: FLOW_IN X-Migadu-Country: US ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=yhetil.org; s=key1; t=1645970534; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding:resent-cc: resent-from:resent-sender:resent-message-id:in-reply-to:in-reply-to: references:references:list-id:list-help:list-unsubscribe: list-subscribe:list-post:dkim-signature; bh=912F7cZQA0XDyDLS2BIPLh5lsHJs3DYf4xpW2EWNTMk=; b=pfmUP4vAPvCuGud6p77+tNsFwgFYmumfQjxy8VwcS4mb1xMlbvpt98t/WlBzFVvkZXGnmC Hez4qWZ1HmlX0Cg+u2WXguGPOwCS5XOq09/auk1HOeJGBuSV2AX22gMlwm+gKq5Q4VlEAX MoFhNdpcGAxTQm4yeNhgfnmVIJSW/KaS0aKbloIbui2jDuc0efRV5TqFD6LXivowW4y3qX 8GkLhkfpuQRAWSf6Eu3EXt6oMZPuACjurkl3/3OIchCryp3U5A6zcaCCqk7ktuO9x563h7 jrrPPBPPuEINeQRQWr41pwzW0zRhNoCK9poFpKhx4nt1JrrW+wkOToJ7RKkVXg== ARC-Seal: i=1; s=key1; d=yhetil.org; t=1645970534; a=rsa-sha256; cv=none; b=Gl7/fI71gAbFawITfBalFrkJwMDic2FXPLUOKnp2gNpl+rXvMFCZXvbAYKPxuE3CRzwS8W hVTmGtgKmls7cbeCMWF6xyLSkDqKP+MH4PSX3KzbTCIclmqrMSoE0NMopnDnCxokDwaSuW l1qWhzlScg4Y4AQYppaTVR+WbpTZSoGTeQ3y9hV+EO4I2TWWo37/L2mHBvHDNQGvH++Nft XpKHEvkDafKpf/qRhAILOjJFWgOL6JA+KrRDM5u20VVuJWb+gsobTycLx2kOsSZpp/t76v TIP7rixhC4yQhfCP/7R63oe0vXZsddJs79n2e1iIYWTc6uYxnjiIjR0ItbH9kQ== ARC-Authentication-Results: i=1; aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=a+HhsUrX; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Spam-Score: -3.41 Authentication-Results: aspmx1.migadu.com; dkim=fail ("headers rsa verify failed") header.d=gnu.org header.s=fencepost-gnu-org header.b=a+HhsUrX; dmarc=pass (policy=none) header.from=gnu.org; spf=pass (aspmx1.migadu.com: domain of "guix-patches-bounces+larch=yhetil.org@gnu.org" designates 209.51.188.17 as permitted sender) smtp.mailfrom="guix-patches-bounces+larch=yhetil.org@gnu.org" X-Migadu-Queue-Id: A35A1431C9 X-Spam-Score: -3.41 X-Migadu-Scanner: scn0.migadu.com X-TUID: GDdRHhPH5Xdf This removes three 'stat' syscalls. * gnu/home/services/symlink-manager.scm (update-symlinks-script)[empty-directory?]: Remove. [cleanup-symlinks]: Replace use of 'file-exists?', 'file-is-directory?', and 'empty-directory?' by a single 'rmdir' call. --- gnu/home/services/symlink-manager.scm | 35 ++++++++++++++------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/gnu/home/services/symlink-manager.scm b/gnu/home/services/symlink-manager.scm index f133eb17f2..6b3a9de3d1 100644 --- a/gnu/home/services/symlink-manager.scm +++ b/gnu/home/services/symlink-manager.scm @@ -1,6 +1,7 @@ ;;; GNU Guix --- Functional package management for GNU ;;; Copyright © 2021 Andrew Tropin ;;; Copyright © 2021 Xinglu Chen +;;; Copyright © 2022 Ludovic Courtès ;;; ;;; This file is part of GNU Guix. ;;; @@ -101,9 +102,6 @@ (define (get-target-path path) (define (get-backup-path path) (string-append backup-dir "/." path)) - (define (empty-directory? dir) - (equal? (scandir dir) '("." ".."))) - (define (symlink-to-store? path) (and (equal? (stat:type (lstat path)) 'symlink) (store-file-name? (readlink path)))) @@ -127,20 +125,23 @@ (define (cleanup-symlinks old-tree) (('dir . ".") (display (G_ "Cleanup finished.\n\n"))) - (('dir . path) - (if (and - (file-exists? (get-target-path path)) - (file-is-directory? (get-target-path path)) - (empty-directory? (get-target-path path))) - (begin - (format #t (G_ "Removing ~a...") - (get-target-path path)) - (rmdir (get-target-path path)) - (display (G_ " done\n"))) - (format - #t - (G_ "Skipping ~a (not an empty directory)... done\n") - (get-target-path path)))) + (('dir . directory) + (let ((directory (get-target-path directory))) + (catch 'system-error + (lambda () + (rmdir directory) + (format #t (G_ "Removed ~a.\n") directory)) + (lambda args + (let ((errno (system-error-errno args))) + (cond ((= ENOTEMPTY errno) + (format + #t + (G_ "Skipping ~a (not an empty directory)...\n") + directory)) + ((= ENOTDIR errno) + #t) + (else + (apply throw args)))))))) (('file . path) (when (file-exists? (get-target-path path)) -- 2.34.0