From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: Exclusive file access Date: Tue, 01 Sep 2015 16:13:14 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx02.eternal-september.org; posting-host="0903b4fccd19524a858dd481bbd547d7"; logging-data="16294"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19qg4ePiTpSXW/2nUwnC6/HdVkDECTmk58=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (darwin) Cancel-Lock: sha1:3IxFDvjeO11TBsAgRZDEq+EAByg= sha1:7Mt4l0767ka0SJz0Ec7CpM1aMnQ= Xref: news.eternal-september.org comp.lang.ada:27675 Date: 2015-09-01T16:13:14+01:00 List-Id: ahlan@marriott.org writes: > Bruce B. tells me that there are routines in the package Directories > that can be used to check if the file is already open. However that > wouldn't be a full solution because there would be nothing to stop any > other program from opening the file and messing with it. However it > would be a start. If the file was already opened (by some other > process) then I could avoid opening it. Unfortunately I couldn't find > any such routine in Ada.Directories. Am I going blind? GNAT.Directories perhaps? though I only see an Is_Open for directories, not files. GNAT.OS_Lib (a renaming of System.OS_Lib)? no, don't think so. > So far the only solution I can see is for me to write my own filing > system, one that has the ability to open the file for exclusive file > access. However I cannot believe that this is what people really do. > As it stands if I write a program that writes a log to a text file a > second occurrence of the program if run concurrently will mess up this > log. > Is there really no way for an Gnat Ada program to open a file for > exclusive access against other processes without resorting to the > program writing his/her own filing system? (Open/Read/Write/Close that > call directly the OS API) I misunderstood you. To me, "filing system" means the whole organisation of files with hierarchies of directories. I think you are rather out of luck. I can see how it might be done on OS X [open(2) with O_EXLOCK, then write(2)]. But, if you want a logger, what about for example Alog[1]? It's probably overkill. But it's an exact analog of how you'd do it for tasking within one process: open the file in a dedicated task, and send messages to be logged to the task. [1] http://www.codelabs.ch/alog/