author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
Tue, 04 Jul 2017 23:13:47 +0900 | |
changeset 33281 | 6af0f023d014 |
parent 33280 | 646352291f5b |
child 33282 | d1db7af81548 |
permissions | -rw-r--r-- |
31227
0f31830fbfc4
vfs: extract 'vfs' class and related code to a new 'vfs' module (API)
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
31045
diff
changeset
|
1 |
# vfs.py - Mercurial 'vfs' classes |
13962
8b252e826c68
add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff
changeset
|
2 |
# |
8b252e826c68
add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff
changeset
|
3 |
# Copyright Matt Mackall <mpm@selenic.com> |
8b252e826c68
add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff
changeset
|
4 |
# |
8b252e826c68
add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff
changeset
|
5 |
# This software may be used and distributed according to the terms of the |
8b252e826c68
add: introduce a warning message for non-portable filenames (issue2756) (BC)
Adrian Buehlmann <adrian@cadifra.com>
parents:
diff
changeset
|
6 |
# GNU General Public License version 2 or any later version. |
27482
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
7 |
from __future__ import absolute_import |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
8 |
|
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
9 |
import contextlib |
27482
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
10 |
import errno |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
11 |
import os |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
12 |
import shutil |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
13 |
import stat |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
14 |
import tempfile |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
15 |
import threading |
27482
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
16 |
|
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
17 |
from .i18n import _ |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
18 |
from . import ( |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
19 |
error, |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
20 |
pathutil, |
30318
af7c60988f6e
py3: make scmutil.rcpath() return bytes
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30109
diff
changeset
|
21 |
pycompat, |
27482
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
22 |
util, |
dde3da2246f1
scmutil: use absolute_import
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27093
diff
changeset
|
23 |
) |
18690
4c6f7f0dadab
scmutil: split platform-specific bits into their own modules
Kevin Bullock <kbullock@ringworld.org>
parents:
18678
diff
changeset
|
24 |
|
33280
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
25 |
def _avoidambig(path, oldstat): |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
26 |
"""Avoid file stat ambiguity forcibly |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
27 |
|
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
28 |
This function causes copying ``path`` file, if it is owned by |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
29 |
another (see issue5418 and issue5584 for detail). |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
30 |
""" |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
31 |
def checkandavoid(): |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
32 |
newstat = util.filestat.frompath(path) |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
33 |
# return whether file stat ambiguity is (already) avoided |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
34 |
return (not newstat.isambig(oldstat) or |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
35 |
newstat.avoidambig(path, oldstat)) |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
36 |
if not checkandavoid(): |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
37 |
# simply copy to change owner of path to get privilege to |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
38 |
# advance mtime (see issue5418) |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
39 |
util.rename(util.mktempcopy(path), path) |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
40 |
checkandavoid() |
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
41 |
|
17649
f65c6a5f256c
scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17561
diff
changeset
|
42 |
class abstractvfs(object): |
14089
d3f7e110c3c0
opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14068
diff
changeset
|
43 |
"""Abstract base class; cannot be instantiated""" |
d3f7e110c3c0
opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14068
diff
changeset
|
44 |
|
d3f7e110c3c0
opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14068
diff
changeset
|
45 |
def __init__(self, *args, **kwargs): |
d3f7e110c3c0
opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14068
diff
changeset
|
46 |
'''Prevent instantiation; don't call this from subclasses.''' |
d3f7e110c3c0
opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14068
diff
changeset
|
47 |
raise NotImplementedError('attempted instantiating ' + str(type(self))) |
d3f7e110c3c0
opener: introduce an abstact superclass of it
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14068
diff
changeset
|
48 |
|
16455
154219f3a6a4
opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents:
16436
diff
changeset
|
49 |
def tryread(self, path): |
16479
fc04698fa778
opener: coding style, use triple quotes for doc string
Thomas Arendsen Hein <thomas@intevation.de>
parents:
16455
diff
changeset
|
50 |
'''gracefully return an empty string for missing files''' |
16455
154219f3a6a4
opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents:
16436
diff
changeset
|
51 |
try: |
154219f3a6a4
opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents:
16436
diff
changeset
|
52 |
return self.read(path) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
53 |
except IOError as inst: |
16455
154219f3a6a4
opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents:
16436
diff
changeset
|
54 |
if inst.errno != errno.ENOENT: |
154219f3a6a4
opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents:
16436
diff
changeset
|
55 |
raise |
154219f3a6a4
opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents:
16436
diff
changeset
|
56 |
return "" |
154219f3a6a4
opener: introduce tryread helper
Matt Mackall <mpm@selenic.com>
parents:
16436
diff
changeset
|
57 |
|
23368
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
58 |
def tryreadlines(self, path, mode='rb'): |
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
59 |
'''gracefully return an empty array for missing files''' |
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
60 |
try: |
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
61 |
return self.readlines(path, mode=mode) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
62 |
except IOError as inst: |
23368
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
63 |
if inst.errno != errno.ENOENT: |
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
64 |
raise |
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
65 |
return [] |
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
66 |
|
29730
2dd8c225e94c
vfs: use propertycache for open
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
29727
diff
changeset
|
67 |
@util.propertycache |
2dd8c225e94c
vfs: use propertycache for open
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
29727
diff
changeset
|
68 |
def open(self): |
23370
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
69 |
'''Open ``path`` file, which is relative to vfs root. |
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
70 |
|
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
71 |
Newly created directories are marked as "not to be indexed by |
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
72 |
the content indexing service", if ``notindexed`` is specified |
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
73 |
for "write" mode access. |
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
74 |
''' |
29730
2dd8c225e94c
vfs: use propertycache for open
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
29727
diff
changeset
|
75 |
return self.__call__ |
19897
896a4568def7
vfs: add "open()" for newly added code paths which open files via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19896
diff
changeset
|
76 |
|
14167
0e4753807c93
util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14138
diff
changeset
|
77 |
def read(self, path): |
27706
22e362da27cf
scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27651
diff
changeset
|
78 |
with self(path, 'rb') as fp: |
14097
ca3376f044f8
opener: add read & write utility methods
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14090
diff
changeset
|
79 |
return fp.read() |
ca3376f044f8
opener: add read & write utility methods
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14090
diff
changeset
|
80 |
|
23368
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
81 |
def readlines(self, path, mode='rb'): |
27706
22e362da27cf
scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27651
diff
changeset
|
82 |
with self(path, mode=mode) as fp: |
23368
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
83 |
return fp.readlines() |
bf8c3172255c
vfs: add "readlines" and "tryreadlines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23259
diff
changeset
|
84 |
|
28197
2ada62388bb1
scmutil: support background closing for write()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28017
diff
changeset
|
85 |
def write(self, path, data, backgroundclose=False): |
2ada62388bb1
scmutil: support background closing for write()
Gregory Szorc <gregory.szorc@gmail.com>
parents:
28017
diff
changeset
|
86 |
with self(path, 'wb', backgroundclose=backgroundclose) as fp: |
14167
0e4753807c93
util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14138
diff
changeset
|
87 |
return fp.write(data) |
0e4753807c93
util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14138
diff
changeset
|
88 |
|
23371
1df6519eb3ab
vfs: add "writelines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23370
diff
changeset
|
89 |
def writelines(self, path, data, mode='wb', notindexed=False): |
27706
22e362da27cf
scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27651
diff
changeset
|
90 |
with self(path, mode=mode, notindexed=notindexed) as fp: |
23371
1df6519eb3ab
vfs: add "writelines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23370
diff
changeset
|
91 |
return fp.writelines(data) |
1df6519eb3ab
vfs: add "writelines"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23370
diff
changeset
|
92 |
|
14167
0e4753807c93
util & scmutil: adapt read/write helpers as request by mpm
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14138
diff
changeset
|
93 |
def append(self, path, data): |
27706
22e362da27cf
scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27651
diff
changeset
|
94 |
with self(path, 'ab') as fp: |
14097
ca3376f044f8
opener: add read & write utility methods
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14090
diff
changeset
|
95 |
return fp.write(data) |
ca3376f044f8
opener: add read & write utility methods
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14090
diff
changeset
|
96 |
|
25770
39de2e9cc6f4
vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25739
diff
changeset
|
97 |
def basename(self, path): |
39de2e9cc6f4
vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25739
diff
changeset
|
98 |
"""return base element of a path (as os.path.basename would do) |
39de2e9cc6f4
vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25739
diff
changeset
|
99 |
|
39de2e9cc6f4
vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25739
diff
changeset
|
100 |
This exists to allow handling of strange encoding if needed.""" |
39de2e9cc6f4
vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25739
diff
changeset
|
101 |
return os.path.basename(path) |
39de2e9cc6f4
vfs: add basename
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25739
diff
changeset
|
102 |
|
20086
f3df2612f3c3
vfs: add "chmod()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20085
diff
changeset
|
103 |
def chmod(self, path, mode): |
f3df2612f3c3
vfs: add "chmod()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20085
diff
changeset
|
104 |
return os.chmod(self.join(path), mode) |
f3df2612f3c3
vfs: add "chmod()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20085
diff
changeset
|
105 |
|
25772
5471965af5cb
vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25770
diff
changeset
|
106 |
def dirname(self, path): |
5471965af5cb
vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25770
diff
changeset
|
107 |
"""return dirname element of a path (as os.path.dirname would do) |
5471965af5cb
vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25770
diff
changeset
|
108 |
|
5471965af5cb
vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25770
diff
changeset
|
109 |
This exists to allow handling of strange encoding if needed.""" |
5471965af5cb
vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25770
diff
changeset
|
110 |
return os.path.dirname(path) |
5471965af5cb
vfs: add dirname
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
25770
diff
changeset
|
111 |
|
17161
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
112 |
def exists(self, path=None): |
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
113 |
return os.path.exists(self.join(path)) |
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
114 |
|
19899
8c3dcbbfb5de
changelog: use "vfs.fstat()" instead of "util.fstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19897
diff
changeset
|
115 |
def fstat(self, fp): |
8c3dcbbfb5de
changelog: use "vfs.fstat()" instead of "util.fstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19897
diff
changeset
|
116 |
return util.fstat(fp) |
8c3dcbbfb5de
changelog: use "vfs.fstat()" instead of "util.fstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19897
diff
changeset
|
117 |
|
17161
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
118 |
def isdir(self, path=None): |
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
119 |
return os.path.isdir(self.join(path)) |
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
120 |
|
20085
589d6bb5b18d
vfs: add "isfile()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20045
diff
changeset
|
121 |
def isfile(self, path=None): |
589d6bb5b18d
vfs: add "isfile()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20045
diff
changeset
|
122 |
return os.path.isfile(self.join(path)) |
589d6bb5b18d
vfs: add "isfile()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20045
diff
changeset
|
123 |
|
18949
138978f20180
localrepo: use "vfs.islink()" instead of "os.path.islink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18948
diff
changeset
|
124 |
def islink(self, path=None): |
138978f20180
localrepo: use "vfs.islink()" instead of "os.path.islink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18948
diff
changeset
|
125 |
return os.path.islink(self.join(path)) |
138978f20180
localrepo: use "vfs.islink()" instead of "os.path.islink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18948
diff
changeset
|
126 |
|
27571
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
127 |
def isfileorlink(self, path=None): |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
128 |
'''return whether path is a regular file or a symlink |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
129 |
|
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
130 |
Unlike isfile, this doesn't follow symlinks.''' |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
131 |
try: |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
132 |
st = self.lstat(path) |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
133 |
except OSError: |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
134 |
return False |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
135 |
mode = st.st_mode |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
136 |
return stat.S_ISREG(mode) or stat.S_ISLNK(mode) |
6a6e78f84cc6
merge: while checking for unknown files don't follow symlinks (issue5027)
Siddharth Agarwal <sid0@fb.com>
parents:
26836
diff
changeset
|
137 |
|
23581
aed981c7bebf
vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23539
diff
changeset
|
138 |
def reljoin(self, *paths): |
aed981c7bebf
vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23539
diff
changeset
|
139 |
"""join various elements of a path together (as os.path.join would do) |
aed981c7bebf
vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23539
diff
changeset
|
140 |
|
aed981c7bebf
vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23539
diff
changeset
|
141 |
The vfs base is not injected so that path stay relative. This exists |
aed981c7bebf
vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23539
diff
changeset
|
142 |
to allow handling of strange encoding if needed.""" |
aed981c7bebf
vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23539
diff
changeset
|
143 |
return os.path.join(*paths) |
aed981c7bebf
vfs: add a 'reljoin' function for joining relative paths
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23539
diff
changeset
|
144 |
|
23582
7559dc8c4238
vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23581
diff
changeset
|
145 |
def split(self, path): |
7559dc8c4238
vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23581
diff
changeset
|
146 |
"""split top-most element of a path (as os.path.split would do) |
7559dc8c4238
vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23581
diff
changeset
|
147 |
|
7559dc8c4238
vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23581
diff
changeset
|
148 |
This exists to allow handling of strange encoding if needed.""" |
7559dc8c4238
vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23581
diff
changeset
|
149 |
return os.path.split(path) |
7559dc8c4238
vfs: add a 'split' method
Pierre-Yves David <pierre-yves.david@fb.com>
parents:
23581
diff
changeset
|
150 |
|
21563
764b691b8bda
vfs: add lexists() in current api
Chinmay Joshi <c@chinmayjoshi.com>
parents:
21111
diff
changeset
|
151 |
def lexists(self, path=None): |
764b691b8bda
vfs: add lexists() in current api
Chinmay Joshi <c@chinmayjoshi.com>
parents:
21111
diff
changeset
|
152 |
return os.path.lexists(self.join(path)) |
764b691b8bda
vfs: add lexists() in current api
Chinmay Joshi <c@chinmayjoshi.com>
parents:
21111
diff
changeset
|
153 |
|
19900
7c21e3398931
context: use "vfs.lstat()" instead of "os.lstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19899
diff
changeset
|
154 |
def lstat(self, path=None): |
7c21e3398931
context: use "vfs.lstat()" instead of "os.lstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19899
diff
changeset
|
155 |
return os.lstat(self.join(path)) |
7c21e3398931
context: use "vfs.lstat()" instead of "os.lstat()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19899
diff
changeset
|
156 |
|
21799
dfacdd6a111e
vfs: add listdir for os.listdir in vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents:
21716
diff
changeset
|
157 |
def listdir(self, path=None): |
dfacdd6a111e
vfs: add listdir for os.listdir in vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents:
21716
diff
changeset
|
158 |
return os.listdir(self.join(path)) |
dfacdd6a111e
vfs: add listdir for os.listdir in vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents:
21716
diff
changeset
|
159 |
|
17161
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
160 |
def makedir(self, path=None, notindexed=True): |
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
161 |
return util.makedir(self.join(path), notindexed) |
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
162 |
|
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
163 |
def makedirs(self, path=None, mode=None): |
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
164 |
return util.makedirs(self.join(path), mode) |
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
165 |
|
20090
88d8e568add1
vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20086
diff
changeset
|
166 |
def makelock(self, info, path): |
88d8e568add1
vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20086
diff
changeset
|
167 |
return util.makelock(info, self.join(path)) |
88d8e568add1
vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20086
diff
changeset
|
168 |
|
17723
ab23768746fd
scmutil: reorder newly added functions for vfs support in dictionary order
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17681
diff
changeset
|
169 |
def mkdir(self, path=None): |
ab23768746fd
scmutil: reorder newly added functions for vfs support in dictionary order
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17681
diff
changeset
|
170 |
return os.mkdir(self.join(path)) |
ab23768746fd
scmutil: reorder newly added functions for vfs support in dictionary order
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17681
diff
changeset
|
171 |
|
20980
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
172 |
def mkstemp(self, suffix='', prefix='tmp', dir=None, text=False): |
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
173 |
fd, name = tempfile.mkstemp(suffix=suffix, prefix=prefix, |
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
174 |
dir=self.join(dir), text=text) |
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
175 |
dname, fname = util.split(name) |
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
176 |
if dir: |
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
177 |
return fd, os.path.join(dir, fname) |
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
178 |
else: |
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
179 |
return fd, fname |
6fb4c94ae300
vfs: add "mkstemp()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20862
diff
changeset
|
180 |
|
17747
aad3bce98f76
store: invoke "osutil.listdir()" via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17726
diff
changeset
|
181 |
def readdir(self, path=None, stat=None, skip=None): |
32248
d74b0cff94a9
osutil: proxy through util (and platform) modules (API)
Yuya Nishihara <yuya@tcha.org>
parents:
31649
diff
changeset
|
182 |
return util.listdir(self.join(path), stat, skip) |
17747
aad3bce98f76
store: invoke "osutil.listdir()" via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17726
diff
changeset
|
183 |
|
20090
88d8e568add1
vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20086
diff
changeset
|
184 |
def readlock(self, path): |
88d8e568add1
vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20086
diff
changeset
|
185 |
return util.readlock(self.join(path)) |
88d8e568add1
vfs: add "makelock()" and "readlock()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
20086
diff
changeset
|
186 |
|
29203
731ced087a4b
vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29202
diff
changeset
|
187 |
def rename(self, src, dst, checkambig=False): |
29367
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
188 |
"""Rename from src to dst |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
189 |
|
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
190 |
checkambig argument is used with util.filestat, and is useful |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
191 |
only if destination file is guarded by any lock |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
192 |
(e.g. repo.lock or repo.wlock). |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
193 |
""" |
32768
ed66ec39933f
vfs: create copy at renaming to avoid file stat ambiguity if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
32767
diff
changeset
|
194 |
srcpath = self.join(src) |
29203
731ced087a4b
vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29202
diff
changeset
|
195 |
dstpath = self.join(dst) |
32792
7ad95626f6a7
filestat: move __init__ to frompath constructor
Siddharth Agarwal <sid0@fb.com>
parents:
32768
diff
changeset
|
196 |
oldstat = checkambig and util.filestat.frompath(dstpath) |
29203
731ced087a4b
vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29202
diff
changeset
|
197 |
if oldstat and oldstat.stat: |
33281
6af0f023d014
vfs: replace avoiding ambiguity in abstractvfs.rename with _avoidambig
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33280
diff
changeset
|
198 |
ret = util.rename(srcpath, dstpath) |
6af0f023d014
vfs: replace avoiding ambiguity in abstractvfs.rename with _avoidambig
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33280
diff
changeset
|
199 |
_avoidambig(dstpath, oldstat) |
29203
731ced087a4b
vfs: make rename avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29202
diff
changeset
|
200 |
return ret |
32768
ed66ec39933f
vfs: create copy at renaming to avoid file stat ambiguity if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
32767
diff
changeset
|
201 |
return util.rename(srcpath, dstpath) |
18948
2f05fa162316
localrepo: use "vfs.rename()" instead of "util.rename()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18945
diff
changeset
|
202 |
|
18950
647e3b0c8751
localrepo: use "vfs.readlink()" instead of "os.readlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18949
diff
changeset
|
203 |
def readlink(self, path): |
647e3b0c8751
localrepo: use "vfs.readlink()" instead of "os.readlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18949
diff
changeset
|
204 |
return os.readlink(self.join(path)) |
647e3b0c8751
localrepo: use "vfs.readlink()" instead of "os.readlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18949
diff
changeset
|
205 |
|
24693
0d28b0df77ea
vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24689
diff
changeset
|
206 |
def removedirs(self, path=None): |
0d28b0df77ea
vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24689
diff
changeset
|
207 |
"""Remove a leaf directory and all empty intermediate ones |
0d28b0df77ea
vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24689
diff
changeset
|
208 |
""" |
0d28b0df77ea
vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24689
diff
changeset
|
209 |
return util.removedirs(self.join(path)) |
0d28b0df77ea
vfs: add removedirs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24689
diff
changeset
|
210 |
|
24689
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
211 |
def rmtree(self, path=None, ignore_errors=False, forcibly=False): |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
212 |
"""Remove a directory tree recursively |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
213 |
|
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
214 |
If ``forcibly``, this tries to remove READ-ONLY files, too. |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
215 |
""" |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
216 |
if forcibly: |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
217 |
def onerror(function, path, excinfo): |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
218 |
if function is not os.remove: |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
219 |
raise |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
220 |
# read-only files cannot be unlinked under Windows |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
221 |
s = os.stat(path) |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
222 |
if (s.st_mode & stat.S_IWRITE) != 0: |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
223 |
raise |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
224 |
os.chmod(path, stat.S_IMODE(s.st_mode) | stat.S_IWRITE) |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
225 |
os.remove(path) |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
226 |
else: |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
227 |
onerror = None |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
228 |
return shutil.rmtree(self.join(path), |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
229 |
ignore_errors=ignore_errors, onerror=onerror) |
ca3a90096c95
vfs: add rmtree
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
24635
diff
changeset
|
230 |
|
18951
d13916a00b7e
localrepo: use "vfs.setflags()" instead of "util.setflags()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18950
diff
changeset
|
231 |
def setflags(self, path, l, x): |
d13916a00b7e
localrepo: use "vfs.setflags()" instead of "util.setflags()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18950
diff
changeset
|
232 |
return util.setflags(self.join(path), l, x) |
d13916a00b7e
localrepo: use "vfs.setflags()" instead of "util.setflags()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18950
diff
changeset
|
233 |
|
17726
7cb7e17c23b2
store: invoke "os.stat()" for "createmode" initialization via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17725
diff
changeset
|
234 |
def stat(self, path=None): |
7cb7e17c23b2
store: invoke "os.stat()" for "createmode" initialization via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17725
diff
changeset
|
235 |
return os.stat(self.join(path)) |
7cb7e17c23b2
store: invoke "os.stat()" for "createmode" initialization via vfs
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17725
diff
changeset
|
236 |
|
19895
37c0d93fb166
bookmarks: use "vfs.unlink()" instead of "util.unlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19655
diff
changeset
|
237 |
def unlink(self, path=None): |
37c0d93fb166
bookmarks: use "vfs.unlink()" instead of "util.unlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19655
diff
changeset
|
238 |
return util.unlink(self.join(path)) |
37c0d93fb166
bookmarks: use "vfs.unlink()" instead of "util.unlink()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19655
diff
changeset
|
239 |
|
31548 | 240 |
def tryunlink(self, path=None): |
241 |
"""Attempt to remove a file, ignoring missing file errors.""" |
|
242 |
util.tryunlink(self.join(path)) |
|
243 |
||
21716
90f9be5adade
vfs: add unlinkpath to vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents:
21563
diff
changeset
|
244 |
def unlinkpath(self, path=None, ignoremissing=False): |
31318
8908f985570c
vfs: use repo.wvfs.unlinkpath
Mads Kiilerich <madski@unity3d.com>
parents:
31227
diff
changeset
|
245 |
return util.unlinkpath(self.join(path), ignoremissing=ignoremissing) |
21716
90f9be5adade
vfs: add unlinkpath to vfs
Chinmay Joshi <c@chinmayjoshi.com>
parents:
21563
diff
changeset
|
246 |
|
19896
af03279c766a
bookmarks: use "vfs.utime()" instead of "os.utime()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19895
diff
changeset
|
247 |
def utime(self, path=None, t=None): |
af03279c766a
bookmarks: use "vfs.utime()" instead of "os.utime()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19895
diff
changeset
|
248 |
return os.utime(self.join(path), t) |
af03279c766a
bookmarks: use "vfs.utime()" instead of "os.utime()"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
19895
diff
changeset
|
249 |
|
24725 | 250 |
def walk(self, path=None, onerror=None): |
251 |
"""Yield (dirpath, dirs, files) tuple for each directories under path |
|
252 |
||
253 |
``dirpath`` is relative one from the root of this vfs. This |
|
254 |
uses ``os.sep`` as path separator, even you specify POSIX |
|
255 |
style ``path``. |
|
256 |
||
257 |
"The root of this vfs" is represented as empty ``dirpath``. |
|
258 |
""" |
|
259 |
root = os.path.normpath(self.join(None)) |
|
260 |
# when dirpath == root, dirpath[prefixlen:] becomes empty |
|
261 |
# because len(dirpath) < prefixlen. |
|
262 |
prefixlen = len(pathutil.normasprefix(root)) |
|
263 |
for dirpath, dirs, files in os.walk(self.join(path), onerror=onerror): |
|
264 |
yield (dirpath[prefixlen:], dirs, files) |
|
265 |
||
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
266 |
@contextlib.contextmanager |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
267 |
def backgroundclosing(self, ui, expectedcount=-1): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
268 |
"""Allow files to be closed asynchronously. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
269 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
270 |
When this context manager is active, ``backgroundclose`` can be passed |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
271 |
to ``__call__``/``open`` to result in the file possibly being closed |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
272 |
asynchronously, on a background thread. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
273 |
""" |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
274 |
# This is an arbitrary restriction and could be changed if we ever |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
275 |
# have a use case. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
276 |
vfs = getattr(self, 'vfs', self) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
277 |
if getattr(vfs, '_backgroundfilecloser', None): |
29389
98e8313dcd9e
i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents:
29373
diff
changeset
|
278 |
raise error.Abort( |
98e8313dcd9e
i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents:
29373
diff
changeset
|
279 |
_('can only have 1 active background file closer')) |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
280 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
281 |
with backgroundfilecloser(ui, expectedcount=expectedcount) as bfc: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
282 |
try: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
283 |
vfs._backgroundfilecloser = bfc |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
284 |
yield bfc |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
285 |
finally: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
286 |
vfs._backgroundfilecloser = None |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
287 |
|
17649
f65c6a5f256c
scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17561
diff
changeset
|
288 |
class vfs(abstractvfs): |
f65c6a5f256c
scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17561
diff
changeset
|
289 |
'''Operate files relative to a base directory |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
290 |
|
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
291 |
This class is used to hide the details of COW semantics and |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
292 |
remote file access from higher level code. |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
293 |
''' |
18945
e75b72fffdfe
vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18900
diff
changeset
|
294 |
def __init__(self, base, audit=True, expandpath=False, realpath=False): |
e75b72fffdfe
vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18900
diff
changeset
|
295 |
if expandpath: |
e75b72fffdfe
vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18900
diff
changeset
|
296 |
base = util.expandpath(base) |
e75b72fffdfe
vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18900
diff
changeset
|
297 |
if realpath: |
e75b72fffdfe
vfs: split "expand" into "realpath"/"expandpath" to apply each separately
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
18900
diff
changeset
|
298 |
base = os.path.realpath(base) |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
299 |
self.base = base |
33259
6fb5c5096887
vfs: drop the 'mustaudit' API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33257
diff
changeset
|
300 |
self._audit = audit |
6fb5c5096887
vfs: drop the 'mustaudit' API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33257
diff
changeset
|
301 |
if audit: |
20033
f962870712da
pathutil: tease out a new library to break an import cycle from canonpath use
Augie Fackler <raf@durin42.com>
parents:
20006
diff
changeset
|
302 |
self.audit = pathutil.pathauditor(self.base) |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
303 |
else: |
18327
4aecdb91443c
scmutil: simplify vfs.audit - drop wrapped vfs.auditor
Mads Kiilerich <mads@kiilerich.com>
parents:
18316
diff
changeset
|
304 |
self.audit = util.always |
33259
6fb5c5096887
vfs: drop the 'mustaudit' API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33257
diff
changeset
|
305 |
self.createmode = None |
6fb5c5096887
vfs: drop the 'mustaudit' API
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33257
diff
changeset
|
306 |
self._trustnlink = None |
17554
5450c8ad9d98
scmutil: turn opener._audit into a property, mustaudit
Bryan O'Sullivan <bryano@fb.com>
parents:
17248
diff
changeset
|
307 |
|
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
308 |
@util.propertycache |
14261
e3649bcca3f6
opener: rename _can_symlink to _cansymlink
Adrian Buehlmann <adrian@cadifra.com>
parents:
14236
diff
changeset
|
309 |
def _cansymlink(self): |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
310 |
return util.checklink(self.base) |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
311 |
|
18192
f9a89bdd64a6
scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents:
17850
diff
changeset
|
312 |
@util.propertycache |
f9a89bdd64a6
scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents:
17850
diff
changeset
|
313 |
def _chmod(self): |
f9a89bdd64a6
scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents:
17850
diff
changeset
|
314 |
return util.checkexec(self.base) |
f9a89bdd64a6
scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents:
17850
diff
changeset
|
315 |
|
17763
13070de77c86
vfs: backout fchmod change from 76b73ce0ffac
Matt Mackall <mpm@selenic.com>
parents:
17752
diff
changeset
|
316 |
def _fixfilemode(self, name): |
18192
f9a89bdd64a6
scmutil: don't try to match modes on filesystems without modes (issue3740)
Matt Mackall <mpm@selenic.com>
parents:
17850
diff
changeset
|
317 |
if self.createmode is None or not self._chmod: |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
318 |
return |
25658
e93036747902
global: mass rewrite to use modern octal syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25629
diff
changeset
|
319 |
os.chmod(name, self.createmode & 0o666) |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
320 |
|
23370
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
321 |
def __call__(self, path, mode="r", text=False, atomictemp=False, |
33257
15e9cbe6ae49
vfs: simplify path audit disabling in stream clone
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33234
diff
changeset
|
322 |
notindexed=False, backgroundclose=False, checkambig=False, |
15e9cbe6ae49
vfs: simplify path audit disabling in stream clone
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33234
diff
changeset
|
323 |
auditpath=True): |
23370
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
324 |
'''Open ``path`` file, which is relative to vfs root. |
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
325 |
|
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
326 |
Newly created directories are marked as "not to be indexed by |
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
327 |
the content indexing service", if ``notindexed`` is specified |
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
328 |
for "write" mode access. |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
329 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
330 |
If ``backgroundclose`` is passed, the file may be closed asynchronously. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
331 |
It can only be used if the ``self.backgroundclosing()`` context manager |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
332 |
is active. This should only be specified if the following criteria hold: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
333 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
334 |
1. There is a potential for writing thousands of files. Unless you |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
335 |
are writing thousands of files, the performance benefits of |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
336 |
asynchronously closing files is not realized. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
337 |
2. Files are opened exactly once for the ``backgroundclosing`` |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
338 |
active duration and are therefore free of race conditions between |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
339 |
closing a file on a background thread and reopening it. (If the |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
340 |
file were opened multiple times, there could be unflushed data |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
341 |
because the original file handle hasn't been flushed/closed yet.) |
29202
76f1ea360c7e
vfs: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29119
diff
changeset
|
342 |
|
29367
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
343 |
``checkambig`` argument is passed to atomictemplfile (valid |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
344 |
only for writing), and is useful only if target file is |
4e6e280e238f
doc: describe detail about checkambig optional argument
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29341
diff
changeset
|
345 |
guarded by any lock (e.g. repo.lock or repo.wlock). |
23370
46265d0f0c7b
vfs: add "notindexed" argument to invoke "ensuredir" with it in write mode
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
23368
diff
changeset
|
346 |
''' |
33257
15e9cbe6ae49
vfs: simplify path audit disabling in stream clone
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33234
diff
changeset
|
347 |
if auditpath: |
15e9cbe6ae49
vfs: simplify path audit disabling in stream clone
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33234
diff
changeset
|
348 |
if self._audit: |
15e9cbe6ae49
vfs: simplify path audit disabling in stream clone
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33234
diff
changeset
|
349 |
r = util.checkosfilename(path) |
15e9cbe6ae49
vfs: simplify path audit disabling in stream clone
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33234
diff
changeset
|
350 |
if r: |
15e9cbe6ae49
vfs: simplify path audit disabling in stream clone
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33234
diff
changeset
|
351 |
raise error.Abort("%s: %r" % (r, path)) |
15e9cbe6ae49
vfs: simplify path audit disabling in stream clone
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33234
diff
changeset
|
352 |
self.audit(path) |
16199
8181bd808dc5
scmutil: add join method to opener to construct path relative to base
Idan Kamara <idankk86@gmail.com>
parents:
16198
diff
changeset
|
353 |
f = self.join(path) |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
354 |
|
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
355 |
if not text and "b" not in mode: |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
356 |
mode += "b" # for that other OS |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
357 |
|
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
358 |
nlink = -1 |
17937
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
359 |
if mode not in ('r', 'rb'): |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
360 |
dirname, basename = util.split(f) |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
361 |
# If basename is empty, then the path is malformed because it points |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
362 |
# to a directory. Let the posixfile() call below raise IOError. |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
363 |
if basename: |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
364 |
if atomictemp: |
29017
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28819
diff
changeset
|
365 |
util.makedirs(dirname, self.createmode, notindexed) |
29202
76f1ea360c7e
vfs: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29119
diff
changeset
|
366 |
return util.atomictempfile(f, mode, self.createmode, |
76f1ea360c7e
vfs: make atomictempfile avoid ambiguity of file stat if needed
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29119
diff
changeset
|
367 |
checkambig=checkambig) |
17937
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
368 |
try: |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
369 |
if 'w' in mode: |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
370 |
util.unlink(f) |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
371 |
nlink = 0 |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
372 |
else: |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
373 |
# nlinks() may behave differently for files on Windows |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
374 |
# shares if the file is open. |
27706
22e362da27cf
scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27651
diff
changeset
|
375 |
with util.posixfile(f): |
22e362da27cf
scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27651
diff
changeset
|
376 |
nlink = util.nlinks(f) |
22e362da27cf
scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27651
diff
changeset
|
377 |
if nlink < 1: |
22e362da27cf
scmutil: use context managers for file handles
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27651
diff
changeset
|
378 |
nlink = 2 # force mktempcopy (issue1922) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
379 |
except (OSError, IOError) as e: |
17937
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
380 |
if e.errno != errno.ENOENT: |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
381 |
raise |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
382 |
nlink = 0 |
29017
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28819
diff
changeset
|
383 |
util.makedirs(dirname, self.createmode, notindexed) |
17937
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
384 |
if nlink > 0: |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
385 |
if self._trustnlink is None: |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
386 |
self._trustnlink = nlink > 1 or util.checknlink(f) |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
387 |
if nlink > 1 or not self._trustnlink: |
3cb032d50447
vfs: optimize __call__ by not calling util.split for reads
Adrian Buehlmann <adrian@cadifra.com>
parents:
17850
diff
changeset
|
388 |
util.rename(util.mktempcopy(f), f) |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
389 |
fp = util.posixfile(f, mode) |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
390 |
if nlink == 0: |
17763
13070de77c86
vfs: backout fchmod change from 76b73ce0ffac
Matt Mackall <mpm@selenic.com>
parents:
17752
diff
changeset
|
391 |
self._fixfilemode(f) |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
392 |
|
30000
9766d88c2465
vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29999
diff
changeset
|
393 |
if checkambig: |
9766d88c2465
vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29999
diff
changeset
|
394 |
if mode in ('r', 'rb'): |
9766d88c2465
vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29999
diff
changeset
|
395 |
raise error.Abort(_('implementation error: mode %s is not' |
9766d88c2465
vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29999
diff
changeset
|
396 |
' valid for checkambig=True') % mode) |
9766d88c2465
vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29999
diff
changeset
|
397 |
fp = checkambigatclosing(fp) |
9766d88c2465
vfs: use checkambigatclosing in checkambig=True but atomictemp=False case
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29999
diff
changeset
|
398 |
|
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
399 |
if backgroundclose: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
400 |
if not self._backgroundfilecloser: |
29389
98e8313dcd9e
i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents:
29373
diff
changeset
|
401 |
raise error.Abort(_('backgroundclose can only be used when a ' |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
402 |
'backgroundclosing context manager is active') |
29389
98e8313dcd9e
i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents:
29373
diff
changeset
|
403 |
) |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
404 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
405 |
fp = delayclosedfile(fp, self._backgroundfilecloser) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
406 |
|
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
407 |
return fp |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
408 |
|
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
409 |
def symlink(self, src, dst): |
18327
4aecdb91443c
scmutil: simplify vfs.audit - drop wrapped vfs.auditor
Mads Kiilerich <mads@kiilerich.com>
parents:
18316
diff
changeset
|
410 |
self.audit(dst) |
16199
8181bd808dc5
scmutil: add join method to opener to construct path relative to base
Idan Kamara <idankk86@gmail.com>
parents:
16198
diff
changeset
|
411 |
linkname = self.join(dst) |
31555 | 412 |
util.tryunlink(linkname) |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
413 |
|
29017
07be86828e79
util: fix race in makedirs()
Adam Simpkins <simpkins@fb.com>
parents:
28819
diff
changeset
|
414 |
util.makedirs(os.path.dirname(linkname), self.createmode) |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
415 |
|
14261
e3649bcca3f6
opener: rename _can_symlink to _cansymlink
Adrian Buehlmann <adrian@cadifra.com>
parents:
14236
diff
changeset
|
416 |
if self._cansymlink: |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
417 |
try: |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
418 |
os.symlink(src, linkname) |
25660
328739ea70c3
global: mass rewrite to use modern exception syntax
Gregory Szorc <gregory.szorc@gmail.com>
parents:
25658
diff
changeset
|
419 |
except OSError as err: |
13970
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
420 |
raise OSError(err.errno, _('could not symlink to %r: %s') % |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
421 |
(src, err.strerror), linkname) |
d13913355390
move opener from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13962
diff
changeset
|
422 |
else: |
17768
9837cafc25b1
vfs: use self.write to write symlink placeholders
Matt Mackall <mpm@selenic.com>
parents:
17763
diff
changeset
|
423 |
self.write(dst, src) |
13971
bfeaa88b875d
move canonpath from util to scmutil
Adrian Buehlmann <adrian@cadifra.com>
parents:
13970
diff
changeset
|
424 |
|
24628
a0b47885a1c5
vfs: make it possible to pass multiple path elements to join
Matt Harbison <matt_harbison@yahoo.com>
parents:
24582
diff
changeset
|
425 |
def join(self, path, *insidef): |
17161
be016e96117a
localrepo: use file API via vfs while ensuring repository directory
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17157
diff
changeset
|
426 |
if path: |
24628
a0b47885a1c5
vfs: make it possible to pass multiple path elements to join
Matt Harbison <matt_harbison@yahoo.com>
parents:
24582
diff
changeset
|
427 |
return os.path.join(self.base, path, *insidef) |
17681
a41fd730f230
scmutil: backout 83785bb56062 (issue3643)
Matt Mackall <mpm@selenic.com>
parents:
17675
diff
changeset
|
428 |
else: |
a41fd730f230
scmutil: backout 83785bb56062 (issue3643)
Matt Mackall <mpm@selenic.com>
parents:
17675
diff
changeset
|
429 |
return self.base |
16199
8181bd808dc5
scmutil: add join method to opener to construct path relative to base
Idan Kamara <idankk86@gmail.com>
parents:
16198
diff
changeset
|
430 |
|
17649
f65c6a5f256c
scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17561
diff
changeset
|
431 |
opener = vfs |
f65c6a5f256c
scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17561
diff
changeset
|
432 |
|
17845
408ded42c5ec
scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents:
17821
diff
changeset
|
433 |
class auditvfs(object): |
408ded42c5ec
scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents:
17821
diff
changeset
|
434 |
def __init__(self, vfs): |
408ded42c5ec
scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents:
17821
diff
changeset
|
435 |
self.vfs = vfs |
408ded42c5ec
scmutil: abstract out mustaudit delegation
Bryan O'Sullivan <bryano@fb.com>
parents:
17821
diff
changeset
|
436 |
|
27879
52a4ad62b006
cleanup: use modern @property/@foo.setter property specification
Augie Fackler <augie@google.com>
parents:
27851
diff
changeset
|
437 |
@property |
29727
69109052d9ac
auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents:
29417
diff
changeset
|
438 |
def options(self): |
69109052d9ac
auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents:
29417
diff
changeset
|
439 |
return self.vfs.options |
69109052d9ac
auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents:
29417
diff
changeset
|
440 |
|
69109052d9ac
auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents:
29417
diff
changeset
|
441 |
@options.setter |
69109052d9ac
auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents:
29417
diff
changeset
|
442 |
def options(self, value): |
69109052d9ac
auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents:
29417
diff
changeset
|
443 |
self.vfs.options = value |
69109052d9ac
auditvfs: forward options property from nested vfs
Augie Fackler <augie@google.com>
parents:
29417
diff
changeset
|
444 |
|
17846
f42cf30873dc
scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents:
17845
diff
changeset
|
445 |
class filtervfs(abstractvfs, auditvfs): |
17649
f65c6a5f256c
scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17561
diff
changeset
|
446 |
'''Wrapper vfs for filtering filenames with a function.''' |
14090
e24b5e3c2f27
add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14089
diff
changeset
|
447 |
|
17846
f42cf30873dc
scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents:
17845
diff
changeset
|
448 |
def __init__(self, vfs, filter): |
f42cf30873dc
scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents:
17845
diff
changeset
|
449 |
auditvfs.__init__(self, vfs) |
14090
e24b5e3c2f27
add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14089
diff
changeset
|
450 |
self._filter = filter |
e24b5e3c2f27
add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14089
diff
changeset
|
451 |
|
e24b5e3c2f27
add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14089
diff
changeset
|
452 |
def __call__(self, path, *args, **kwargs): |
17846
f42cf30873dc
scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents:
17845
diff
changeset
|
453 |
return self.vfs(self._filter(path), *args, **kwargs) |
14090
e24b5e3c2f27
add filteropener abstraction for store openers
Dan Villiom Podlaski Christiansen <danchr@gmail.com>
parents:
14089
diff
changeset
|
454 |
|
24628
a0b47885a1c5
vfs: make it possible to pass multiple path elements to join
Matt Harbison <matt_harbison@yahoo.com>
parents:
24582
diff
changeset
|
455 |
def join(self, path, *insidef): |
17725
ffd589d4b785
vfs: define "join()" in each classes derived from "abstractvfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17723
diff
changeset
|
456 |
if path: |
24628
a0b47885a1c5
vfs: make it possible to pass multiple path elements to join
Matt Harbison <matt_harbison@yahoo.com>
parents:
24582
diff
changeset
|
457 |
return self.vfs.join(self._filter(self.vfs.reljoin(path, *insidef))) |
17725
ffd589d4b785
vfs: define "join()" in each classes derived from "abstractvfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17723
diff
changeset
|
458 |
else: |
17846
f42cf30873dc
scmutil: add mustaudit delegation to filtervfs (issue3673)
Bryan O'Sullivan <bryano@fb.com>
parents:
17845
diff
changeset
|
459 |
return self.vfs.join(path) |
17725
ffd589d4b785
vfs: define "join()" in each classes derived from "abstractvfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17723
diff
changeset
|
460 |
|
17649
f65c6a5f256c
scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17561
diff
changeset
|
461 |
filteropener = filtervfs |
f65c6a5f256c
scmutil: rename classes from "opener" to "vfs"
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
17561
diff
changeset
|
462 |
|
18213
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
463 |
class readonlyvfs(abstractvfs, auditvfs): |
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
464 |
'''Wrapper vfs preventing any writing.''' |
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
465 |
|
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
466 |
def __init__(self, vfs): |
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
467 |
auditvfs.__init__(self, vfs) |
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
468 |
|
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
469 |
def __call__(self, path, mode='r', *args, **kw): |
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
470 |
if mode not in ('r', 'rb'): |
29389
98e8313dcd9e
i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents:
29373
diff
changeset
|
471 |
raise error.Abort(_('this vfs is read only')) |
18213
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
472 |
return self.vfs(path, mode, *args, **kw) |
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
473 |
|
26156
a112fffdb632
scmutil.readonlyvfs: implement join
Siddharth Agarwal <sid0@fb.com>
parents:
26098
diff
changeset
|
474 |
def join(self, path, *insidef): |
a112fffdb632
scmutil.readonlyvfs: implement join
Siddharth Agarwal <sid0@fb.com>
parents:
26098
diff
changeset
|
475 |
return self.vfs.join(path, *insidef) |
18213
c38a62af000e
vfs: add a read only vfs
Pierre-Yves David <pierre-yves.david@ens-lyon.org>
parents:
18206
diff
changeset
|
476 |
|
29998
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
477 |
class closewrapbase(object): |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
478 |
"""Base class of wrapper, which hooks closing |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
479 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
480 |
Do not instantiate outside of the vfs layer. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
481 |
""" |
29998
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
482 |
def __init__(self, fh): |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31555
diff
changeset
|
483 |
object.__setattr__(self, r'_origfh', fh) |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
484 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
485 |
def __getattr__(self, attr): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
486 |
return getattr(self._origfh, attr) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
487 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
488 |
def __setattr__(self, attr, value): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
489 |
return setattr(self._origfh, attr, value) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
490 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
491 |
def __delattr__(self, attr): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
492 |
return delattr(self._origfh, attr) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
493 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
494 |
def __enter__(self): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
495 |
return self._origfh.__enter__() |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
496 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
497 |
def __exit__(self, exc_type, exc_value, exc_tb): |
29998
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
498 |
raise NotImplementedError('attempted instantiating ' + str(type(self))) |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
499 |
|
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
500 |
def close(self): |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
501 |
raise NotImplementedError('attempted instantiating ' + str(type(self))) |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
502 |
|
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
503 |
class delayclosedfile(closewrapbase): |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
504 |
"""Proxy for a file object whose close is delayed. |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
505 |
|
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
506 |
Do not instantiate outside of the vfs layer. |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
507 |
""" |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
508 |
def __init__(self, fh, closer): |
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
509 |
super(delayclosedfile, self).__init__(fh) |
31649
f80d9ddc40f3
py3: abuse r'' to preserve str-ness of literals passed to __setattr__()
Yuya Nishihara <yuya@tcha.org>
parents:
31555
diff
changeset
|
510 |
object.__setattr__(self, r'_closer', closer) |
29998
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
511 |
|
0c40e64d6154
scmutil: factor out common logic of delayclosedfile to reuse it
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29813
diff
changeset
|
512 |
def __exit__(self, exc_type, exc_value, exc_tb): |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
513 |
self._closer.close(self._origfh) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
514 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
515 |
def close(self): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
516 |
self._closer.close(self._origfh) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
517 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
518 |
class backgroundfilecloser(object): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
519 |
"""Coordinates background closing of file handles on multiple threads.""" |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
520 |
def __init__(self, ui, expectedcount=-1): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
521 |
self._running = False |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
522 |
self._entered = False |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
523 |
self._threads = [] |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
524 |
self._threadexception = None |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
525 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
526 |
# Only Windows/NTFS has slow file closing. So only enable by default |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
527 |
# on that platform. But allow to be enabled elsewhere for testing. |
30644
d524c88511a7
py3: replace os.name with pycompat.osname (part 1 of 2)
Pulkit Goyal <7895pulkit@gmail.com>
parents:
30529
diff
changeset
|
528 |
defaultenabled = pycompat.osname == 'nt' |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
529 |
enabled = ui.configbool('worker', 'backgroundclose', defaultenabled) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
530 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
531 |
if not enabled: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
532 |
return |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
533 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
534 |
# There is overhead to starting and stopping the background threads. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
535 |
# Don't do background processing unless the file count is large enough |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
536 |
# to justify it. |
33231
d19804c16710
configitems: register the 'worker.backgroundcloseminfilecount' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33230
diff
changeset
|
537 |
minfilecount = ui.configint('worker', 'backgroundcloseminfilecount') |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
538 |
# FUTURE dynamically start background threads after minfilecount closes. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
539 |
# (We don't currently have any callers that don't know their file count) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
540 |
if expectedcount > 0 and expectedcount < minfilecount: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
541 |
return |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
542 |
|
33230
5dcbd2045dcb
configitems: register the 'worker.backgroundclosemaxqueue' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
32792
diff
changeset
|
543 |
maxqueue = ui.configint('worker', 'backgroundclosemaxqueue') |
33232
4531a967e7f1
configitems: register the 'worker.backgroundclosethreadcount' config
Pierre-Yves David <pierre-yves.david@octobus.net>
parents:
33231
diff
changeset
|
544 |
threadcount = ui.configint('worker', 'backgroundclosethreadcount') |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
545 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
546 |
ui.debug('starting %d threads for background file closing\n' % |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
547 |
threadcount) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
548 |
|
28819
826d457df138
scmutil: use util.queue/util.empty for py3 compat
timeless <timeless@mozdev.org>
parents:
28197
diff
changeset
|
549 |
self._queue = util.queue(maxsize=maxqueue) |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
550 |
self._running = True |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
551 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
552 |
for i in range(threadcount): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
553 |
t = threading.Thread(target=self._worker, name='backgroundcloser') |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
554 |
self._threads.append(t) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
555 |
t.start() |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
556 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
557 |
def __enter__(self): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
558 |
self._entered = True |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
559 |
return self |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
560 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
561 |
def __exit__(self, exc_type, exc_value, exc_tb): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
562 |
self._running = False |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
563 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
564 |
# Wait for threads to finish closing so open files don't linger for |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
565 |
# longer than lifetime of context manager. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
566 |
for t in self._threads: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
567 |
t.join() |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
568 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
569 |
def _worker(self): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
570 |
"""Main routine for worker thread.""" |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
571 |
while True: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
572 |
try: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
573 |
fh = self._queue.get(block=True, timeout=0.100) |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
574 |
# Need to catch or the thread will terminate and |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
575 |
# we could orphan file descriptors. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
576 |
try: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
577 |
fh.close() |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
578 |
except Exception as e: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
579 |
# Stash so can re-raise from main thread later. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
580 |
self._threadexception = e |
28819
826d457df138
scmutil: use util.queue/util.empty for py3 compat
timeless <timeless@mozdev.org>
parents:
28197
diff
changeset
|
581 |
except util.empty: |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
582 |
if not self._running: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
583 |
break |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
584 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
585 |
def close(self, fh): |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
586 |
"""Schedule a file for closing.""" |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
587 |
if not self._entered: |
29389
98e8313dcd9e
i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents:
29373
diff
changeset
|
588 |
raise error.Abort(_('can only call close() when context manager ' |
98e8313dcd9e
i18n: translate abort messages
liscju <piotr.listkiewicz@gmail.com>
parents:
29373
diff
changeset
|
589 |
'active')) |
27895
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
590 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
591 |
# If a background thread encountered an exception, raise now so we fail |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
592 |
# fast. Otherwise we may potentially go on for minutes until the error |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
593 |
# is acted on. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
594 |
if self._threadexception: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
595 |
e = self._threadexception |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
596 |
self._threadexception = None |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
597 |
raise e |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
598 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
599 |
# If we're not actively running, close synchronously. |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
600 |
if not self._running: |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
601 |
fh.close() |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
602 |
return |
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
603 |
|
2d6a89e79b48
scmutil: support background file closing
Gregory Szorc <gregory.szorc@gmail.com>
parents:
27879
diff
changeset
|
604 |
self._queue.put(fh, block=True, timeout=None) |
29999
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
605 |
|
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
606 |
class checkambigatclosing(closewrapbase): |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
607 |
"""Proxy for a file object, to avoid ambiguity of file stat |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
608 |
|
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
609 |
See also util.filestat for detail about "ambiguity of file stat". |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
610 |
|
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
611 |
This proxy is useful only if the target file is guarded by any |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
612 |
lock (e.g. repo.lock or repo.wlock) |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
613 |
|
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
614 |
Do not instantiate outside of the vfs layer. |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
615 |
""" |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
616 |
def __init__(self, fh): |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
617 |
super(checkambigatclosing, self).__init__(fh) |
32792
7ad95626f6a7
filestat: move __init__ to frompath constructor
Siddharth Agarwal <sid0@fb.com>
parents:
32768
diff
changeset
|
618 |
object.__setattr__(self, r'_oldstat', util.filestat.frompath(fh.name)) |
29999
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
619 |
|
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
620 |
def _checkambig(self): |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
621 |
oldstat = self._oldstat |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
622 |
if oldstat.stat: |
33280
646352291f5b
vfs: copy if EPERM to avoid file stat ambiguity forcibly at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
33259
diff
changeset
|
623 |
_avoidambig(self._origfh.name, oldstat) |
29999
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
624 |
|
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
625 |
def __exit__(self, exc_type, exc_value, exc_tb): |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
626 |
self._origfh.__exit__(exc_type, exc_value, exc_tb) |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
627 |
self._checkambig() |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
628 |
|
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
629 |
def close(self): |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
630 |
self._origfh.close() |
57830bd0e787
scmutil: add file object wrapper class to check ambiguity at closing
FUJIWARA Katsunori <foozy@lares.dti.ne.jp>
parents:
29998
diff
changeset
|
631 |
self._checkambig() |