Package filelock. R topics documented: February 7, 2018

Similar documents
Package spark. July 21, 2017

Package sessioninfo. June 21, 2017

Package zip. R topics documented: March 11, Title Cross-Platform 'zip' Compression Version 2.0.1

Package rcmdcheck. R topics documented: November 10, 2018

Package rhub. November 1, 2017

Package callr. August 29, 2016

Package messaging. May 27, 2018

Package sankey. R topics documented: October 22, 2017

Package cattonum. R topics documented: May 2, Type Package Version Title Encode Categorical Features

Package cli. November 5, 2017

Package goodpractice

Package callr. January 30, 2018

Package available. November 17, 2017

Package fastdummies. January 8, 2018

Package pkgbuild. October 16, 2018

Package desc. May 1, 2018

Package synchronicity

Package bigreadr. R topics documented: August 13, Version Date Title Read Large Text Files

Package remotes. December 21, 2017

Package kirby21.base

Package knitrprogressbar

Package validara. October 19, 2017

Package ECctmc. May 1, 2018

Package clipr. June 23, 2018

Package geojsonsf. R topics documented: January 11, Type Package Title GeoJSON to Simple Feature Converter Version 1.3.

Package routr. October 26, 2017

Package crossword.r. January 19, 2018

Package pwrab. R topics documented: June 6, Type Package Title Power Analysis for AB Testing Version 0.1.0

Package projector. February 27, 2018

Package IATScore. January 10, 2018

Package dbx. July 5, 2018

Package datasets.load

Package repec. August 31, 2018

Package humanize. R topics documented: April 4, Version Title Create Values for Human Consumption

Package algorithmia. September 13, 2016

Package ecoseries. R topics documented: September 27, 2017

Package robotstxt. November 12, 2017

Package shinytest. May 7, 2018

Package pbmcapply. August 16, 2017

Processes and Threads

Package ggimage. R topics documented: November 1, Title Use Image in 'ggplot2' Version 0.0.7

Package loggit. April 9, 2018

Package redux. May 31, 2018

Package tiler. June 9, 2018

Package librarian. R topics documented:

Package farver. November 20, 2018

Package gifti. February 1, 2018

Package kdtools. April 26, 2018

Package docxtools. July 6, 2018

Package semver. January 6, 2017

Package spelling. December 18, 2017

Package gcite. R topics documented: February 2, Type Package Title Google Citation Parser Version Date Author John Muschelli

Package ggimage. R topics documented: December 5, Title Use Image in 'ggplot2' Version 0.1.0

Package catchr. R topics documented: December 30, Type Package. Title Catch, Collect, and Raise Conditions. Version 0.1.0

Package pkgload. July 8, 2018

Package jdx. R topics documented: January 9, Type Package Title 'Java' Data Exchange for 'R' and 'rjava'

Package wdman. January 23, 2017

Package githubinstall

Package sfdct. August 29, 2017

Package geogrid. August 19, 2018

Package mdftracks. February 6, 2017

Package canvasxpress

Package narray. January 28, 2018

Package combiter. December 4, 2017

Package assertive.reflection

Package texpreview. August 15, 2018

Package readxl. April 18, 2017

Package essurvey. August 23, 2018

Package keyholder. May 19, 2018

Package memoise. April 21, 2017

Package deductive. June 2, 2017

Package WordR. September 7, 2017

Package vesselr. R topics documented: April 3, Type Package

Package tidyselect. October 11, 2018

Package statsdk. September 30, 2017

Package ipc. January 12, 2019

Package slickr. March 6, 2018

Package IRkernel. January 7, 2019

Package calpassapi. August 25, 2018

Package rtika. May 2, 2018

Package wrswor. R topics documented: February 2, Type Package

Package SEMrushR. November 3, 2018

Package data.world. April 5, 2018

Package shinyfeedback

Package comparedf. February 11, 2019

Package gameofthrones

Package dkanr. July 12, 2018

Package rtext. January 23, 2019

Package rsppfp. November 20, 2018

Processes Prof. James L. Frankel Harvard University. Version of 6:16 PM 10-Feb-2017 Copyright 2017, 2015 James L. Frankel. All rights reserved.

Package gggenes. R topics documented: November 7, Title Draw Gene Arrow Maps in 'ggplot2' Version 0.3.2

Package autocogs. September 22, Title Automatic Cognostic Summaries Version 0.1.1

Package MicroStrategyR

Package shiny.semantic

Package rwars. January 14, 2017

Package testit. R topics documented: June 14, 2018

Package widyr. August 14, 2017

Package gtrendsr. October 19, 2017

Package fastqcr. April 11, 2017

Package jstree. October 24, 2017

Transcription:

Package filelock February 7, 2018 Title Portable File Locking Version 1.0.1 Author Gábor Csárdi Maintainer Gábor Csárdi <csardi.gabor@gmail.com> Description Place an exclusive or shared lock on a file. It uses 'LockFile' on Windows and 'fcntl' locks on Unix-like systems. License MIT + file LICENSE LazyData true URL https://github.com/r-lib/filelock#readme BugReports https://github.com/r-lib/filelock/issues RoxygenNote 6.0.1.9000 Suggests callr (>= 2.0.0), covr, testthat Encoding UTF-8 NeedsCompilation yes Repository CRAN Date/Publication 2018-02-07 14:28:55 UTC R topics documented: lock............................................. 2 Index 5 1

2 lock lock Advisory File Locking and Unlocking Description Usage There are two kinds of locks, exclusive and shared, see the exclusive argument and other details below. lock(path, exclusive = TRUE, timeout = Inf) unlock(lock) Arguments path exclusive timeout lock Path to the file to lock. If the file does not exist, it will be created, but the directory of the file must exist. Do not place the lock on a file that you want to read from or write to! *Always use a special lock file. See details below. Whether to acquire an exclusive lock. An exclusive lock gives the process exclusive access to the file, no other processes can place any kind of lock on it. A non-exclusive lock is a shared lock. Multiple processes can hold a shared lock on the same file. A process that writes to a file typically requests an exclusive lock, and a process that reads from it typically requests a shared lock. Timeout to acquire the lock in milliseconds. If Inf, then the process will wait indefinitely to acquire the lock. If zero, then the function it returns immediately, with or without acquiring the lock The lock object to unlock. It is not an error to try to unlock an already unlocked lock. It is not possible to lock an unlocked lock again, a new lock has to be requested. Value lock returns a filelock_lock object if the lock was successfully acquired, and NULL if a timeout happened. unlock returns TRUE, always. Warning Always use special files for locking. I.e. if you want to restict access to a certain file, do not place the lock on this file. Create a special file, e.g. by appending.lock to the original file name and place the lock on that. (The lock() function creates the file for you, actually, if it does not exist.) Reading from or writing to a locked file has undefined behavior! (See more about this below at the Internals Section.) It is hard to determine whether and when it is safe to remove these special files, so our current recommendation is just to leave them around.

lock 3 It is best to leave the special lock file empty, simply because on some OSes you cannot write to it (or read from it), once the lock is in place. Advisory Locks All locks set by this package might be advisory. A process that does not respect this locking machanism may be able to read and write the locked file, or even remove it (assuming it has capabilities to do so). Unlock on Termination If a process terminates (with a normal exit, a crash or on a signal), the lock(s) it is holding are automatically released. If the R object that represents the lock (the return value of lock) goes out of scope, then the lock will be released automatically as soon as the object is garbage collected. This is more of a safety mechanism, and the user should still unlock() locks manually, maybe using base::on.exit(), so that the lock is released in case of errors as well, as soon as possible. Special File Systems File locking needs support from the file system, and some non-standard file systems do not support it. For example on network file systems like NFS or CIFS, user mode file systems like sshfs or ftpfs, etc., support might vary. Recent Linux versions and recent NFS versions (from version 3) do support file locking, if enabled. In theory it is possible to simply test for lock support, using two child processes and a timeout, but filelock does not do this currently. Locking Part of a File While this is possible in general, filelock does not suport it currently. The main purpose of filelock is to lock using special lock files, and locking part of these is not really useful. Internals on Unix On Unix (i.e. Linux, macos, etc.), we use fcntl to acquire and release the locks. You can read more about it here: https://www.gnu.org/software/libc/manual/html_node/file-locks.html Some important points: The lock is put on a file descriptor, which is kept open, until the lock is released. A process can only have one kind of lock set for a given file. When any file descriptor for that file is closed by the process, all of the locks that process holds on that file are released, even if the locks were made using other descriptors that remain open. Note that in R, using a one-shot function call to modify the file opens and closes a file descriptor to it, so the lock will be released. (This is one of the main reasons for using special lock files, instead of putting the lock on the actual file.) Locks are not inherited by child processes created using fork.

4 lock For lock requests with finite timeout intervals, we set an alarm, and temporarily install a signal handler for it. R is single threaded, so no other code can be running, while the process is waiting to acquire the lock. The signal handler is restored to its original value immediately after the lock is acquired or the timeout expires. (It is actually restored from the signal handler, so there should be no race conditions here. However, if multiple SIGALRM signals are delivered via a single call to the signal handler, then alarms might get lost. Currently base R does not use the SIGALRM signal for anything, but other packages might.) Internals on Windows On Windows, LockFileEx is used to create the lock on the file. If a finite timeout is specified for the lock request, asynchronous (overlapped) I/O is used to wait for the locking event with a timeout. See more about LockFileEx here: https://msdn.microsoft.com/en-us/library/aa365203.aspx Some important points: Examples LockFileEx locks are mandatory (as opposed to advisory), so indeed no other processes have access to the locked file. Actually, even the locking process has no access to it through a different file handle, than the one used for locking. In general, R cannot read from the locked file, and cannot write to it. (Although, the current R version does not fail, it just does nothing, which is quite puzzling.) Remember, always use a special lock file, instead of putting the lock on the main file, so that you are not affected by these problems. Inherited handles do not provide access to the child process. ## ------------------------------------------------------------- ## R process 1 gets an exclusive lock ## Warning: if you want to lock file 'myfile', always create a ## separate lock file instead of placing the lock on this file directly! lck <- lock(mylockfile) ## ------------------------------------------------------------- ## R process 2 fails to acquire a lock lock(mylockfile, timeout = 0) ## Let's wait for 5 seconds, before giving up lock(mylockfile, timeout = 5000) ## Wait indefinetely lock(mylockfile, timeout = Inf)

Index base::on.exit(), 3 filelock (lock), 2 lock, 2 unlock (lock), 2 5