Patchwork [1,of,5] vfs: add option to not create parent directories implicitly

login
register
mail settings
Submitter Yuya Nishihara
Date Nov. 30, 2018, 1:41 p.m.
Message ID <d4060b39fec28b51ba98.1543585284@mimosa>
Download mbox | patch
Permalink /patch/36878/
State Accepted
Headers show

Comments

Yuya Nishihara - Nov. 30, 2018, 1:41 p.m.
# HG changeset patch
# User Yuya Nishihara <yuya@tcha.org>
# Date 1542720672 -32400
#      Tue Nov 20 22:31:12 2018 +0900
# Node ID d4060b39fec28b51ba98818eaa23e067da906402
# Parent  7e6834ade51d98da575cfbeef651fc8a465c38b8
vfs: add option to not create parent directories implicitly

In blackbox, we don't want to create a ".hg" directory by mistake. This
provides a race-safe option to achieve that.

Patch

diff --git a/mercurial/vfs.py b/mercurial/vfs.py
--- a/mercurial/vfs.py
+++ b/mercurial/vfs.py
@@ -345,12 +345,14 @@  class vfs(abstractvfs):
             self.audit(path, mode=mode)
 
     def __call__(self, path, mode="r", atomictemp=False, notindexed=False,
-                 backgroundclose=False, checkambig=False, auditpath=True):
+                 backgroundclose=False, checkambig=False, auditpath=True,
+                 makeparentdirs=True):
         '''Open ``path`` file, which is relative to vfs root.
 
-        Newly created directories are marked as "not to be indexed by
-        the content indexing service", if ``notindexed`` is specified
-        for "write" mode access.
+        By default, parent directories are created as needed. Newly created
+        directories are marked as "not to be indexed by the content indexing
+        service", if ``notindexed`` is specified for "write" mode access.
+        Set ``makeparentdirs=False`` to not create directories implicitly.
 
         If ``backgroundclose`` is passed, the file may be closed asynchronously.
         It can only be used if the ``self.backgroundclosing()`` context manager
@@ -389,7 +391,8 @@  class vfs(abstractvfs):
             # to a directory. Let the posixfile() call below raise IOError.
             if basename:
                 if atomictemp:
-                    util.makedirs(dirname, self.createmode, notindexed)
+                    if makeparentdirs:
+                        util.makedirs(dirname, self.createmode, notindexed)
                     return util.atomictempfile(f, mode, self.createmode,
                                                checkambig=checkambig)
                 try:
@@ -407,7 +410,8 @@  class vfs(abstractvfs):
                     if e.errno != errno.ENOENT:
                         raise
                     nlink = 0
-                    util.makedirs(dirname, self.createmode, notindexed)
+                    if makeparentdirs:
+                        util.makedirs(dirname, self.createmode, notindexed)
                 if nlink > 0:
                     if self._trustnlink is None:
                         self._trustnlink = nlink > 1 or util.checknlink(f)