From patchwork Mon May 2 09:31:06 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arnaud Charlet X-Patchwork-Id: 617437 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 3qyzZm1Mfyz9t3x for ; Mon, 2 May 2016 19:31:27 +1000 (AEST) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.b=KI9g4QxY; dkim-atps=neutral DomainKey-Signature: a=rsa-sha1; c=nofws; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; q=dns; s=default; b=qTYxmbI5hWrp6LDBxrQdHwaTjO0ZwE+TPShi/Ty2l5/iLiDm2a hE3zo/cYpNJVukL9G3KlLLzJVqMnrHgVDsODmiGWIS8D7AJ+aOKnOiLxLqYq89qn q3LtWKXZNZJJjj5Me9ycaAYKbCJmlQaw1jAXr/YcrB1pAQbVpCn34myTY= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=gcc.gnu.org; h=list-id :list-unsubscribe:list-archive:list-post:list-help:sender:date :from:to:cc:subject:message-id:mime-version:content-type; s= default; bh=p0axqTtSxcXZrSJL9guWox84DmQ=; b=KI9g4QxY+K0Z7mAQupen 74nKC8Lj2Xe7tJd8NFP/CmUEgLBYZMbz2Yj33I71U4vR6aQ9mQ/8Poq0GAK4koPp /n+2TYvoXoCFmtD0S8tVSyg1ZYtMENxTieYyWeZ86W0CB7y6alC3urIzrJmHA1CI KPWTUyC08y5s+ImsEmqq/og= Received: (qmail 19348 invoked by alias); 2 May 2016 09:31:18 -0000 Mailing-List: contact gcc-patches-help@gcc.gnu.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Archive: List-Post: List-Help: Sender: gcc-patches-owner@gcc.gnu.org Delivered-To: mailing list gcc-patches@gcc.gnu.org Received: (qmail 19306 invoked by uid 89); 2 May 2016 09:31:18 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-0.9 required=5.0 tests=BAYES_00, KAM_LAZY_DOMAIN_SECURITY, RCVD_IN_DNSWL_NONE autolearn=no version=3.3.2 spammy=235706, monitor, H*F:U*charlet, HX-Envelope-From:sk:charlet X-HELO: rock.gnat.com Received: from rock.gnat.com (HELO rock.gnat.com) (205.232.38.15) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with (AES256-SHA encrypted) ESMTPS; Mon, 02 May 2016 09:31:08 +0000 Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 597BB116B2F; Mon, 2 May 2016 05:31:06 -0400 (EDT) Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id XY5ms0jMjFlB; Mon, 2 May 2016 05:31:06 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [IPv6:2620:20:4000:0:46a8:42ff:fe0e:e294]) by rock.gnat.com (Postfix) with ESMTP id 40055116B2E; Mon, 2 May 2016 05:31:06 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4192) id 3ABC541B; Mon, 2 May 2016 05:31:06 -0400 (EDT) Date: Mon, 2 May 2016 05:31:06 -0400 From: Arnaud Charlet To: gcc-patches@gcc.gnu.org Cc: Olivier Hainque Subject: [Ada] Fix Ada.Directories.Delete_Tree not to change current directory Message-ID: <20160502093106.GA143870@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) ... which is very unfriendly to tasking programs, where a task running Delete_Tree could cause a unexpected change of current directory in another task running concurrently. The program below is expected to display No directory change observed --- with Ada.Text_IO; use Ada.Text_IO; with Ada.Directories; procedure P0 is Root_Directory : constant String := Ada.Directories.Current_Directory; Temp_Path : constant String := Root_Directory & "/tmp/"; -- The idea is to have 2 tasks: -- One which repeteadly creates and deletes a dir until requested to stop. -- and -- Another which monitors changes to its current directory. -- The two tasks aren't explicitly synchronized, except for the monitor -- requesting the creation/deletion task to stop when it's done monitoring -- for a while (fixed number of iterations). -- We expect the OS scheduler to let the monitor run concurrently with the -- creation/deletion task, hopefully while the latter is performing -- Delete_Tree, so a change of current directory there would be observed. -- This is not full proof but was showing the problem consistently on -- at least a couple of native Linux and Windows platforms before the -- correction was applied. task Create_Delete_Dir is entry Stop; end; task Monitor_Current_Dir; task body Create_Delete_Dir is Stop_Requested : Boolean := False; begin while not Stop_Requested loop select accept Stop; Stop_Requested := True; else Ada.Directories.Create_Path (Temp_Path); Ada.Directories.Delete_Tree (Temp_Path); end select; end loop; end; task body Monitor_Current_Dir is Dir_Change_Iteration : Integer := 0; begin for I in 1 .. 100000 loop if Ada.Directories.Current_Directory /= Root_Directory then Dir_Change_Iteration := I; exit; end if; end loop; if Dir_Change_Iteration > 0 then Put_Line ("Directory change at " & Integer'Image(Dir_Change_Iteration)); else Put_Line ("No directory change observed"); end if; -- Done monitoring, request the creation/deletion task -- to stop and exit. Create_Delete_Dir.Stop; end; begin null; end; Tested on x86_64-pc-linux-gnu, committed on trunk 2016-05-02 Olivier Hainque * a-direct.adb (Delete_Tree): Use full names to designate subdirs and files therein, instead of local names after a change of current directory. Index: a-direct.adb =================================================================== --- a-direct.adb (revision 235706) +++ a-direct.adb (working copy) @@ -6,7 +6,7 @@ -- -- -- B o d y -- -- -- --- Copyright (C) 2004-2015, Free Software Foundation, Inc. -- +-- Copyright (C) 2004-2016, Free Software Foundation, Inc. -- -- -- -- GNAT is free software; you can redistribute it and/or modify it under -- -- terms of the GNU General Public License as published by the Free Soft- -- @@ -597,7 +597,6 @@ ----------------- procedure Delete_Tree (Directory : String) is - Current_Dir : constant String := Current_Directory; Search : Search_Type; Dir_Ent : Directory_Entry_Type; begin @@ -611,28 +610,32 @@ raise Name_Error with '"' & Directory & """ not a directory"; else - Set_Directory (Directory); - Start_Search (Search, Directory => ".", Pattern => ""); + -- We used to change the current directory to Directory here, + -- allowing the use of a local Simple_Name for all references. This + -- turned out unfriendly to multitasking programs, where tasks + -- running in parallel of this Delete_Tree could see their current + -- directory change unpredictably. We now resort to Full_Name + -- computations to reach files and subdirs instead. + + Start_Search (Search, Directory => Directory, Pattern => ""); while More_Entries (Search) loop Get_Next_Entry (Search, Dir_Ent); declare - File_Name : constant String := Simple_Name (Dir_Ent); - + Sname : constant String := Simple_Name (Dir_Ent); + Fname : constant String := Full_Name (Dir_Ent); begin - if OS_Lib.Is_Directory (File_Name) then - if File_Name /= "." and then File_Name /= ".." then - Delete_Tree (File_Name); + if OS_Lib.Is_Directory (Fname) then + if Sname /= "." and then Sname /= ".." then + Delete_Tree (Fname); end if; - else - Delete_File (File_Name); + Delete_File (Fname); end if; end; end loop; - Set_Directory (Current_Dir); End_Search (Search); declare