Commit Graph

129 Commits

Author SHA1 Message Date
Yuya Nishihara
a71f259bd2 doctest: bulk-replace string literals with b'' for Python 3
Our code transformer can't rewrite string literals in docstrings, and I
don't want to make the transformer more complex.
2017-09-03 14:32:11 +09:00
Augie Fackler
e2774d9258 python3: wrap all uses of <exception>.strerror with strtolocal
Our string literals are bytes, and we mostly want to %-format a
strerror into a one of those literals, so this fixes a ton of issues.
2017-08-22 20:03:07 -04:00
Jun Wu
a0e5a4defb ssh: quote parameters using shellquote (SEC)
This patch uses shellquote to quote ssh parameters more strictly to avoid
shell injection.
2017-08-04 23:54:12 -07:00
Augie Fackler
5f2d0af8a2 ssh: ban any username@host or host that starts with - (SEC)
This paranoia probably isn't required, but it can't hurt either.
2017-08-04 14:00:03 -04:00
Yuya Nishihara
74b8d80dfc windows: do not close stdout on flush() failure
It's been there since 84af5a079c7d (2007-02-19), but seems wrong since any
I/O operations to a closed file would raise ValueError, not IOError. We should
keep the file object open even if the underlying file descriptor is half dead.
2017-06-05 23:36:35 +09:00
Yuya Nishihara
70995f9aa9 osutil: switch to policy importer
"make clean" is recommended to test this change, though C API compatibility
should be preserved.
2016-08-12 11:35:17 +09:00
Martin von Zweigbergk
c3406ac3db cleanup: use set literals
We no longer support Python 2.6, so we can now use set literals.
2017-02-10 16:56:29 -08:00
Yuya Nishihara
cbe21a1cc9 osutil: proxy through util (and platform) modules (API)
See the previous commit for why. Marked as API change since osutil.listdir()
seems widely used in third-party extensions.

The win32mbcs extension is updated to wrap both util. and windows. aliases.
2017-04-26 22:26:28 +09:00
Matt Harbison
38d197a30d windows: add context manager support to mixedfilemodewrapper
I stumbled into this in the next patch.  The difference between getting a
context manager capable object or not from vfs classes was as subtle as adding a
'+' to the file mode.
2017-04-11 21:38:11 -04:00
Yuya Nishihara
2a7deb97d4 py3: abuse r'' to preserve str-ness of literals passed to __setattr__() 2017-03-26 16:33:12 +09:00
Ryan McElroy
4456f7562c util: unify unlinkpath
Previously, there were two slightly different versions of unlinkpath between
windows and posix, but these differences were eliminated in previous patches.
Now we can unify these two code paths inside of the util module.
2017-03-21 06:50:28 -07:00
Yuya Nishihara
dcade16cf7 encoding: factor out unicode variants of from/tolocal()
Unfortunately, these functions will be commonly used on Python 3.
2017-03-13 09:11:08 -07:00
Pulkit Goyal
bb08d44667 py3: replace os.environ with encoding.environ (part 3 of 5) 2016-12-18 01:54:36 +05:30
Pulkit Goyal
5629f71f5b py3: replace os.sep with pycompat.ossep (part 3 of 4) 2016-12-17 20:14:24 +05:30
Pulkit Goyal
851ae37944 py3: replace os.pathsep with pycompat.ospathsep
os.pathsep returns unicode on Python 3. We already have pycompat.ospathsep
which return bytes on Python 3. This patch replaces all the occurrences of
os.pathsep in the codebase (excluding tests) to pycompat.ospathsep.
2016-12-17 19:47:17 +05:30
Yuya Nishihara
ed37e73e5c windows: do not replace sys.stdout by winstdout
Now we use util.stdout everywhere.
2016-10-21 00:03:46 +09:00
Yuya Nishihara
e876fdd309 windows: do not replace sys.__stdout__
Now we don't use sys.__stdout__ except for getting its fileno(), so we no
longer have to wrap it by winstdout.

This helps adding pycompat.stdin/out/err.
2016-10-20 23:27:09 +09:00
Yuya Nishihara
52e59de51c scmutil: move util.termwidth()
I'm going to get rid of sys.stderr|out|in references from posix.termwidth().
In order to do that, termwidth() needs to take a ui, but functions in util.py
shouldn't depend on a ui object. So moves termwidth() to scmutil.py.
2016-10-20 21:38:44 +09:00
Pulkit Goyal
d280d7dc4a py3: conditionalize _winreg import
_winreg module is renamed to winreg in python 3. Added the conditionalize
statements in the respective file because adding this in pycompat will result
in pycompat throwing error as this is a windows registry module and we have
buildbots and most of the contributors on linux.
2016-08-10 04:35:44 +05:30
Yuya Nishihara
9531323bdb chgserver: extract utility to bind unix domain socket to long path
This is common problem of using sockaddr_un.
2016-05-21 16:52:04 +09:00
Matt Harbison
3d1b5ddf9f windows: correct the import of win32
This module is relative, and was overlooked when converting to absolute_import
in 8474b52419b8.
2015-12-16 13:33:43 -05:00
Gregory Szorc
c66a27807d windows: use absolute_import 2015-12-12 23:19:38 -08:00
Gregory Szorc
93ad4e38a3 windows: insert file positioning call between reads and writes
fopen() and fdopen() have a unique-to-Windows requirement that
transitions between read and write operations in files opened
in modes r+, w+, and a+ perform a file positioning call
(fsetpos, fseek, or rewind) in between. While the MSDN docs don't
say what will happen if this is not done, observations reveal
that Python raises an IOError with errno 0. Furthermore, I
/think/ this behavior isn't deterministic. But I can reproduce
it reliably with subsequent patches applied that open revlogs
in a+ mode and perform both reads and writes.

This patch introduces a proxy class for file handles opened
in r+, w+, and a+ mode on Windows. The class intercepts calls
and audits whether a file positioning function has been called
between read and write operations. If not, a dummy, no-op seek
to the current file position is performed. This appears to be
sufficient to "trick" Windows into allowing transitions between
read and writes without raising errors.
2015-09-27 18:46:53 -07:00
Gregory Szorc
5380dea2a7 global: mass rewrite to use modern exception syntax
Python 2.6 introduced the "except type as instance" syntax, replacing
the "except type, instance" syntax that came before. Python 3 dropped
support for the latter syntax. Since we no longer support Python 2.4 or
2.5, we have no need to continue supporting the "except type, instance".

This patch mass rewrites the exception syntax to be Python 2.6+ and
Python 3 compatible.

This patch was produced by running `2to3 -f except -w -n .`.
2015-06-23 22:20:08 -07:00
Gregory Szorc
3aa1c73868 global: mass rewrite to use modern octal syntax
Python 2.6 introduced a new octal syntax: "0oXXX", replacing "0XXX". The
old syntax is not recognized in Python 3 and will result in a parse
error.

Mass rewrite all instances of the old octal syntax to the new syntax.

This patch was generated by `2to3 -f numliterals -w -n .` and the diff
was selectively recorded to exclude changes to "<N>l" syntax conversion,
which will be handled separately.
2015-06-23 22:30:33 -07:00
Adrian Buehlmann
b55308ade0 windows: use os.SEEK_END 2015-06-06 14:03:55 +02:00
Pierre-Yves David
6fec33b555 util: add a simple poll utility
We'll use it to detect when a sshpeer have server output to be displayed.

The implementation is super basic because all case support is not the focus of
this series.
2015-05-20 18:00:05 -05:00
Pierre-Yves David
f3db59f287 windows: drop Python2.4 specific hack for directory not found handling
A good Python 2.4 hack is a removed Python 2.4 hack.
2015-05-18 16:33:57 -05:00
Adrian Buehlmann
e448d01f77 windows: add comment in normcase()
see also 91785bb51738
2015-05-14 11:37:36 +02:00
Matt Harbison
fbfbea7b97 windows: add doctest for shellquote()
This is actual test coverage for issue4629.  The test changes in 6723e40c7c37
were simply the addition of quotes to the output, not ensuring that strings with
backslashes are quoted.
2015-05-01 20:17:00 -04:00
Matt Harbison
9d40fb3218 windows: make shellquote() quote any path containing '\' (issue4629)
The '~' in the bug report is being expanded to a path with Windows style slashes
before being passed to shellquote() via util.shellquote().  But shlex.split()
strips '\' out of the string, leaving an invalid path in dispatch.aliasargs().

This regressed in 72640182118e.

For now, the tests need to be conditionalized for Windows (because those paths
are quoted).  In the future, a more complex regex could probably skip the quotes
if all component separators are double '\'.  I opted to glob away the quotes in
test-rename-merge2.t and test-up-local-change.t (which only exist on Windows),
because they are in very large blocks of output and there are way too many diffs
to conditionalize with #if directives.  Maybe the entire path should be globbed
away like the following paths in each changed line.  Or, letting #if directives
sit in the middle of the output as was mentioned a few months back would work
too.

Unfortunately, I couldn't figure out how to test the specific bug.  All of the
'hg serve' tests have a #require serve declaration, causing them to be skipped
on Windows.  Adding an alias for 'expandtest = outgoing ~/bogusrepo' prints the
repo as '$TESTTMP/bogusrepo', so the test runner must be changing the
environment somehow.
2015-04-29 21:14:59 -04:00
FUJIWARA Katsunori
d376c990a1 util: add removedirs as platform depending function
According to 6b1369445b7b introducing "windows._removedirs()":

    If a hg repository including working directory is a reparse point
    (directory symlinked or a junction point), then using
    os.removedirs will remove the reparse point erroneously.

"windows._removedirs()" should be used instead of "os.removedirs()" on
Windows.

This patch adds "removedirs" as platform depending function to replace
"os.removedirs()" invocations for portability and safety
2015-04-11 00:47:09 +09:00
Matt Harbison
b7f0673ec5 windows: allow readpipe() to actually read data out of the pipe
It appears that the read() in readpipe() never actually ran before (in
test-ssh.t anyway).  A print of the size returned from os.fstat() is 0 for every
single print output in test-ssh.t, so the data in the pipe ends up being read
later instead of when it is available.  This is the same problem as Linux, as
mentioned in e20a5309b88d.

There are several places in the Windows SSH tests where the order of local
output vs remote output differ from the other platforms.  This only fixes one of
those cases (and interstingly, not the one added in order to test e20a5309b88d),
so there is more investigation needed.  However, without this patch, test-ssh.t
also has this diff:

    --- c:/Users/Matt/Projects/hg/tests/test-ssh.t
    +++ c:/Users/Matt/Projects/hg/tests/test-ssh.t.err
    @@ -397,11 +397,11 @@
       $ hg push --ssh "sh ../ssh.sh"
       pushing to ssh://user@dummy/*/remote (glob)
       searching for changes
    -  remote: Permission denied
    -  remote: abort: prechangegroup.hg-ssh hook failed
    -  remote: Permission denied
    -  remote: pushkey-abort: prepushkey.hg-ssh hook failed
       updating 6c0482d977a3 to public failed!
    +  remote: Permission denied
    +  remote: abort: prechangegroup.hg-ssh hook failed
    +  remote: Permission denied
    +  remote: pushkey-abort: prepushkey.hg-ssh hook failed
       [1]

       $ cd ..

Output with this change was stable over 600+ runs of test-ssh.t.  I initially
tried a background thread to read the pipe[1], but this was simpler and the test
results were exactly the same.  I also tried SetNamedPipeHandleState(), but the
PIPE_NOWAIT is for compatibility with LANMAN 2.0, not for async I/O (the results
were identical though).

[1] http://eyalarubas.com/python-subproc-nonblock.html
2015-04-07 22:31:36 -04:00
Siddharth Agarwal
047970182b windows: define normcase spec and fallback
These will be used in upcoming patches to efficiently create a dirstate
foldmap.
2015-04-01 00:31:41 -07:00
Matt Mackall
6decf56e3e merge with stable 2015-02-27 17:46:03 -06:00
Yuya Nishihara
28b8cb187f shellquote: fix missing quotes for empty string
"hg kdiff3 -rREV" did not work because 72640182118e and 295ba4ee1d13 failed
to handle empty argument.
2015-02-11 19:57:07 +09:00
Adrian Buehlmann
4bd6346ab6 windows: adjust doc string and comments of posixfile()
The doc string of osutil.posixfile includes (line 611):

  "On error, this function may raise either a WindowsError or an IOError."

which is most likely correct, but does not fit for this function here anymore,
as we do fold WindowsError to IOError here specifically.

And this function is now a bit more than just an exception-wrapper, as it has
been expanded to additionally sanitize the unloved seek/tell behavior
of Windows.

(Self-disclosure: This patch is entirely untested at the time of its
publication, as I'm currently not using this version myself. I send it
in hopes that it will reduce potential future confusion. CC-ing Matt Harbison)
2015-02-06 23:40:46 +01:00
Matt Harbison
22f64ebc5f windows: seek to the end of posixfile when opening in append mode
The position is implementation defined when opening in append mode,
and it seems like Linux sets it to EOF while Windows keeps it at zero.
This has caused problems in the past when a file is opened and tell()
is immediately called, such as 5274228efcdc and 03f077311ea1.

Since the only caller of osutil.posixfile is this windows module, this seems
like a better place to fix the issue than in osutil.c and pure.osutil.
2015-01-31 12:39:44 -05:00
FUJIWARA Katsunori
1623722bb9 windows: quote the specified string only when it has to be quoted
Before this patch, "windows.shellquote" (as used as "util.shellquote")
always quotes specified strings with double quotation marks, for
external process invocation.

But some problematic applications can't work correctly, when command
line arguments are quoted: see issue4463 for detail.

On the other hand, quoting itself is needed to specify arguments
containing whitespaces and/or some special characters exactly.

This patch makes "windows.shellquote" examine the specified string and
quote it only when it may have to be quoted for safety.
2014-12-25 23:33:26 +09:00
Gregory Szorc
db57d5e9d6 platform: implement readpipe()
Reading all available data from a pipe has a platform-dependent
implementation.

This patch establishes platform.readpipe() by copying the
inline implementation in sshpeer.readerr(). The implementations
for POSIX and Windows are currently identical. The POSIX
implementation will be changed in a subsequent patch.
2014-08-15 20:02:18 -07:00
Christian Ebert
a9aa17b61c util: remove unused realpath (issue4063)
util.realpath was in use for only 5 days from 17bc9a6bb165
until it was backed out in e60acde24a62 because it caused
issue3077 and issue3071.
2013-12-29 13:54:04 +00:00
Bryan O'Sullivan
f023d03282 util: add functions to check symlink/exec bits
These are not yet used.
2013-04-03 11:35:27 -07:00
Mads Kiilerich
275333d6c9 util: fold ENOENT check into unlinkpath, controlled by new ignoremissing flag
Refactor a common pattern.
2012-12-28 11:55:57 +01:00
Matt Mackall
2e6f376926 windows: correctly pass a mode to S_IFMT in statfiles 2012-12-06 16:56:44 -06:00
Bryan O'Sullivan
a288702cfd dirstate: move file type filtering to its source
This prepares us to move to a much faster statfiles implementation on Unix.
2012-11-30 15:55:07 -08:00
Bryan O'Sullivan
e3555667b8 util: implement a faster os.path.split for posix systems
This is not yet used.
2012-09-14 12:08:17 -07:00
Mads Kiilerich
2f4504e446 fix trivial spelling errors 2012-08-15 22:38:42 +02:00
Ross Lagerwall
661779d660 util: replace util.nulldev with os.devnull
Python since 2.4 has supported os.devnull so having util.nulldev
is unnecessary.
2012-08-04 07:14:40 +02:00
Martin Geisler
c1e716912d merge with stable 2012-07-19 00:54:33 +02:00
Martin Geisler
56abbb58cc windows: removed duplicate termwidth definition
Changeset 8c1e21a3407c caused this when the "from win32 import *" line
was replaced with explicit import statements: the wildcard import was
at the bottom of the file and so windows.termwidth was overwritten by
win32.termwidth as indented, but the new explicit import statements
were at the top and so win32.termwidth got lost.

With the switch to ctypes, win32 can always be imported and so the
fallback termwidth in windows is no longer needed.
2012-07-19 00:53:27 +02:00