Commit Graph

75 Commits

Author SHA1 Message Date
Yuya Nishihara
91f8f53f3a statfs: make getfstype() raise OSError
It's better for getfstype() function to not suppress an error. Callers can
handle it as necessary. Now "hg debugfsinfo" will report OSError.
2017-03-25 17:25:23 +09:00
Yuya Nishihara
91e4f106c6 statfs: rename pygetfstype to getfstype
There's no ambiguity now.
2017-03-25 17:24:11 +09:00
Yuya Nishihara
413bbbf814 statfs: refactor inner function as a mapping from statfs to string
The major difference between BSD and Linux is how to get a fstype string.
Let's split the longest part of getfstype() as a pure function.
2017-03-25 17:23:21 +09:00
Yuya Nishihara
6b9644999c statfs: simplify handling of return value
Py_BuildValue() can translate NULL pointer to None.
2017-03-25 17:13:12 +09:00
Jun Wu
dc9779bcfc statfs: detect more filesystems on Linux
Previously, the code only has what manpager says. In <linux/magic.h>, there
are more defined. This patch adds filesystems that appear in the current
Arch Linux's /proc/filesystems (autofs, overlay, securityfs) and f2fs, which
was seen in news.
2017-03-25 12:58:55 -07:00
Jun Wu
43184506ff statfs: avoid static allocation
Previously we have "static struct statfs" to return a string. That is not
multiple-thread safe. This patch moves the allocation to the caller to
address the problem.
2017-03-24 15:05:42 -07:00
Jun Wu
3633e80713 statfs: change Linux feature detection
Previously we check three things: "statfs" function, "linux/magic.h" and
"sys/vfs.h" headers. But we didn't check "struct statfs" or the "f_type"
field. That means if a system has "statfs" but "struct statfs" is not
defined in the two header files we check, or defined without the "f_type"
field, the compilation will fail.

This patch combines the checks (2 headers + 1 function + 1 field) together
and sets "HAVE_LINUX_STATFS". It makes setup.py faster (less checks), and
more reliable (immutable to the issue above).
2017-03-24 14:59:19 -07:00
Jun Wu
b84e57666e osutil: report fstype for BSD and OSX 2017-03-23 22:13:02 -07:00
Jun Wu
ef355a6e06 osutil: export a "getfstype" method
This patch exports the "getfstype" method. So we can use it to enable
hardlinks for known safe filesystems.

The patch was tested manually via debugshell on a Linux system.
"mercurial.osutil.getfstype" works as expected. It's hard to mount
filesystem on user-space easily. I will add a test for real hardlink support
to indirectly test this patch, after turning on real hardlinks support for
certain whitelisted filesystems.
2017-03-20 16:34:12 -07:00
Jun Wu
57405f31c1 osutil: add a C function getting filesystem type
Currently it only has Linux filesystems, according to my Linux manpage,
built at 2016-03-15.

The code uses "if" instead of "switch" because there could be some
duplicated values.
2017-03-20 16:24:59 -07:00
Jun Wu
0c6a9d19a1 osutil: fix potential wrong fd close
According to POSIX closedir [1]:

  If a file descriptor is used to implement type DIR, that file descriptor
  shall be closed.

According to POSIX fdopendir [2]:

  Upon calling closedir() the file descriptor shall be closed.

So we should avoid "close(dfd)" after "closedir(dir)". With threads, there
could be a race where an innocent fd gets closed. But Python GIL seems to
help hiding the issue well.

[1]: http://pubs.opengroup.org/onlinepubs/009695399/functions/closedir.html
[2]: http://pubs.opengroup.org/onlinepubs/9699919799/functions/fdopendir.html
2017-03-15 20:43:12 -07:00
Gregory Szorc
5deb9bd0a7 osutil: use Python memory allocator in _listdir
The Python memory allocator has performance advantages
for small allocations.
2017-03-09 11:56:47 -08:00
Jun Wu
11296c3f98 osutil: implement setprocname to set process title for some platforms
This patch adds a simple setprocname method to osutil. The operation is not
defined by any standard and is platform-specific, the current implementation
tries to cover some major platforms (ex. Linux, OS X, FreeBSD) that is
relatively easy to support. Other platforms (Windows [4], other BSDs, ...)
can be added in the future.

The current implementation supports two methods to change process title:
  a. setproctitle if available (works in FreeBSD).
  b. rewrite argv in place (works in Linux [1] and Mac OS X). [2] [3]

[1]: Linux has "prctl(PR_SET_NAME, ...)" but 1) it has 16-byte limit, which
is too small; 2) it is not quite equivalent to what we want - it changes
"/proc/self/comm", not "/proc/self/cmdline" - "comm" change won't show up
in "ps" output unless "-o comm" is used.

[2]: The implementation does not rewrite the **environ buffer like some
other implementations do, just to make the code simpler and safer. However,
this also means the buffer size we can rewrite is significantly shorter. If
we are really greedy and want the "environ" space, we can change the
implementation later.

[3]: It requires a CPython private API: Py_GetArgcArgv to get the original
argv. Unfortunately Python 3 makes a copy of argv and returns the wchar_t
version, so it is not supported for now. (if we really want to, we could
count backwards from "char **environ", given known argc and argv, not sure
if that's a good idea - probably not)

[4]: The feature is aimed to make it easier for forked command server
processes to show what they are doing. Since Windows does not support
fork(), despite it's a major platform, its support is not added in this
patch.
2016-11-11 21:11:17 +00:00
Gregory Szorc
ac2a04b10d osutil: use PyLongObject on Python 3 for listdir_slot
This code looks performance sensitive. So let's retain PyIntObject on
Python 2 and use PyLongObject explicitly on Python 3.
2016-10-09 13:47:46 +02:00
Gregory Szorc
809ce99930 osutil: use PyLongObject in recvfds
PyIntObject doesn't exist in Python 3. While PyIntObject is preferred
on Python 2 because it is a fixed capacity and faster, the difference
between PyIntObject and PyLongObject for scenarios where performance
isn't critical or the caller isn't performing type checking shouldn't
be relevant.

So change recvfds to return a list of longs instead of ints on Python
2.
2016-10-09 13:41:18 +02:00
Gregory Szorc
fb1f96e995 osutil: convert PyString* to PyBytes*
Continuing the conversion from PyString* to PyBytes*.
2016-10-08 21:58:55 +02:00
Augie Fackler
f47ca49ac7 osutil: stop using strcpy
strcpy is a security vulnerability masquerading as a utility
function. Replace it with memcpy since we know how much to copy
anyway.
2016-03-19 20:02:19 -04:00
Yuya Nishihara
ce2c3d9c16 osutil: disable compilation of recvfds() on unsupported platforms
It appears that Solaris doesn't provide CMSG_LEN(), msg_control, etc. As
recvfds() is only necessary for chg, this patch just drops it if CMSG_LEN
isn't defined, which is the same workaround as Python 3.x.

https://hg.python.org/cpython/rev/c64216addd7f#l7.33
2016-02-02 20:56:48 +09:00
Matt Mackall
4343a7194d mac: ignore resource fork when checking file sizes
Some evil evil awful tool adds resource forks to files it's comparing.
Our Mac-specific code to do bulk stats was accidentally using "total
size" which includes those forks in the file size, causing them to be
reported as modified. This changes it to only care about the normal
data size and thus agree with what Mercurial's expecting.
2016-01-14 12:37:15 -06:00
Yuya Nishihara
1172aa7cd7 osutil: implement recvmsg() of SCM_RIGHTS for chg command server
It will be used to attach client's stdio files to a background chg command
server.

The socket module of Python 2.x doesn't provide recvmsg(). This could be
implemented by using ctypes, but it would be less portable than the C version
because the handling of socket ancillary data heavily depends on preprocessor.
Also, some length fields are wrongly typed in the Linux kernel.
2015-12-17 23:41:46 +09:00
Bryan O'Sullivan
f5b0cc2242 osutil: make statfiles check for interrupts periodically
This is a simpler and faster fix for issue4878 than the contortions
performed in dd36f84bd784.
2015-11-17 13:47:14 -08:00
Bryan O'Sullivan
d04584822f osutil: don't leak on statfiles error
Found using the power of the mighty eyeball.
2015-11-17 13:43:09 -08:00
Siddharth Agarwal
45755aa70f osutil: mark end of string with null char, not 0
Noticed this while working on other stuff in the area.
2015-03-25 16:21:58 -07:00
Siddharth Agarwal
886bf50396 osutil: use getdirentriesattr on OS X if possible
This is a significant win for large repositories on OS X, especially with a
cold cache. Unfortunately we need to keep the lstat-based implementation around
for two reasons:

- Not all filesystems support this call.
- There's an edge case in which it's best to fall back to avoid a retry loop.
  More about this in the comments.

The below tests are all performed on a Mac with an SSD running OS X 10.9, on a
repository with over 200k files. The results are best of 5 with simulated
best-effort conditions.

The gains with a hot cache are pretty impressive: 'hg status' goes from 5.18
seconds to 3.79 seconds.

However, a repository that large will probably already be using something like
hgwatchman [1], which helps much more (for this repo, 'hg status' with
hgwatchman is approximately 1 second). Where this really helps is when the
cache is cold [2]: hg status goes from 31.0 seconds to 9.66.

See http://lists.apple.com/archives/filesystem-dev/2014/Dec/msg00002.html for
some more discussion about this function.

This is based on a patch by Sean Farley <sean@farley.io>.

[1] https://bitbucket.org/facebook/hgwatchman

[2] There appears to be no easy way to clear the file cache (aka "vnodes") on
OS X short of rebooting. purge(8) purportedly does that but in my testing had
little effect. The workaround I came up with was to assume that vnode eviction
was LRU, make sure the kern.maxvnodes sysctl is smaller than the size of the
repository, then make sure we'd always miss the cache by running 'hg status' in
another clone of the repository before running it in the test repository.
2015-03-25 15:55:31 -07:00
Siddharth Agarwal
2af765a245 osutil._listdir: rename to _listdir_stat
In upcoming patches we'll add another implementation of listdir on OS X. That
implementation will have to fall back to this one under some circumstances,
though. We'll make _listdir be able to detect those circumstances and use the
right function as appropriate.
2015-03-25 16:43:29 -07:00
Augie Fackler
4912d4ca49 osutil: fix memory leak of PyBytes of path in statfiles
Spotted with cpychecker.
2015-01-27 10:17:16 -05:00
Augie Fackler
b15ba92295 osutil: fix leak of stat in makestat when Py_BuildValue fails
Spotted with cpychecker.
2015-01-27 10:14:23 -05:00
Augie Fackler
7d0895a2f0 osutil.c: clean up space before a tab 2015-01-27 10:12:55 -05:00
Bryan O'Sullivan
48cad1b73b osutil: tab damage, how i hate thee 2012-12-03 13:17:01 -08:00
Bryan O'Sullivan
58c82f12c9 osutil: write a C implementation of statfiles for unix
This makes a big difference to performance.

In a clean working directory containing 170,000 files, performance of
"hg --time diff" improves from 2.38 seconds to 1.69.
2012-12-03 12:40:24 -08:00
Bryan O'Sullivan
7be204f3cf osutil: fix tab damage 2012-11-30 17:40:11 -08:00
Bryan O'Sullivan
6b3dd5248e osutil: factor out creation and init of listdir_stat 2012-11-30 15:55:09 -08:00
Matt Mackall
1af38adeb7 osutil: handle deletion race with readdir/stat (issue3463) 2012-05-18 14:34:33 -05:00
Matt Mackall
1071a1b580 merge with stable 2011-09-14 14:37:10 -05:00
Steve Streeting
6a5c8744b6 osutil: avoid accidentally destroying the True object in isgui (issue2937)
Needed to use 'Py_RETURN_TRUE' instead of 'return Py_True' to avoid
reference count errors which would randomly crash the Python
executable during merge. This only happened when you had something
configured in merge-tools and the merge was large enough.
2011-09-08 11:34:59 +01:00
Matt Mackall
21b00826e8 osutil: emulate os.listdir's OSError for long names (issue2898) 2011-07-13 16:58:51 -05:00
Dan Villiom Podlaski Christiansen
ecb9e72af1 osutil: replace #import with #include, and add a check for it 2011-03-23 23:05:32 +01:00
Matt Mackall
b156bda724 osutil: fix up check-code issues 2011-03-23 09:41:58 -05:00
Dan Villiom Podlaski Christiansen
9dfabab3ba util: add Mac-specific check whether we're in a GUI session (issue2553)
The previous test assumed that 'os.name' was "mac" on Mac OS X. This
is not the case; 'mac' was classic Mac OS, whereas Mac OS X has 'os.name'
be 'posix'.

Please note that this change will break Mercurial on hypothetical
non-Mac OS X deployments of Darwin.

Credit to Brodie Rao for thinking of CGSessionCopyCurrentDictionary()
and Kevin Bullock for testing.
2011-03-23 09:43:34 +01:00
Martin Geisler
a76e121863 backout of e4cb9628354c
Matt and a majority of crew did not like this approach.
2011-01-27 11:15:08 +01:00
Martin Geisler
d23e1973c2 specify C indention style using Emacs file local variables 2011-01-26 12:05:01 +01:00
Adrian Buehlmann
2226216a7c osutil: treat open modes 'w' and 'a' as 'w+' and 'a+' in posixfile
to work around http://support.microsoft.com/kb/899149.

Also, Microsoft's documentation of the CreateFile Windows API says (quote):

  When an application creates a file across a network, it is better to use
  GENERIC_READ | GENERIC_WRITE for dwDesiredAccess than to use
  GENERIC_WRITE alone. The resulting code is faster, because the
  redirector can use the cache manager and send fewer SMBs with more data.
  This combination also avoids an issue where writing to a file across a
  network can occasionally return ERROR_ACCESS_DENIED.
2011-01-15 23:54:01 +01:00
Renato Cunha
29e7db1052 osutil.c: Support for py3k added.
This patch adds support for py3k in osutil.c. This is accomplished by including
a header file responsible for abstracting the API differences between python 2
and python 3.

listdir_stat_type is also changed in the following way: A previous call to
PyObject_HEAD_INIT is substituted to a call to PyVarObject_HEAD_INIT, which
makes the object buildable in both python 2.x and 3.x without weird warnings.

After testing on windows, some modifications were also made in the posixfile
function, as it calls PyFile_FromFile and PyFile_SetBufSize, which are gone in
py3k. In py3k the PyFile_* API is, actually a wrapper over the io module, and
code has been adapted accordingly to fit py3k.
2010-06-15 19:49:56 -03:00
Matt Mackall
8d99be19f0 many, many trivial check-code fixups 2010-01-25 00:05:27 -06:00
Sebastien Binet
44975f5f1e osutil: fix compilation with -ansi 2009-08-14 11:18:23 +02:00
Brendan Cully
9c33729b44 Merge with crew-stable 2009-06-06 13:37:41 -07:00
Arne Babenhauserheide
5ded475334 Some platforms lack the PATH_MAX definition (eg. GNU/Hurd).
Thanks to ronny for making it cleaner.
2009-06-05 15:08:45 +02:00
Patrick Mezard
c51f73163e osutil: silence uninitialized variable warning 2009-05-24 16:27:50 +02:00
Bryan O'Sullivan
3054118009 Windows: improve performance via buffered I/O
The posixfile_nt code hits the win32 file API directly, which
essentially amounts to performing a system call for every read and
write. This is slow.

We add a C extension that lets us use a Python file object instead,
but preserve our desired POSIX-like semantics (the ability to rename
or delete a file that is being accessed).

If the C extension is not available (e.g. in a VPS environment
without a compiler), we fall back to the posixfile_nt code.
2009-05-08 15:52:26 -07:00
Thomas Arendsen Hein
8999e196bc Some additional space/tab cleanups 2008-10-20 15:19:05 +02:00