ยปCore Development>Code coverage>Lib/plat-os2emx/_emx_link.py

Python code coverage for Lib/plat-os2emx/_emx_link.py

#countcontent
1n/a# _emx_link.py
2n/a
3n/a# Written by Andrew I MacIntyre, December 2002.
4n/a
5n/a"""_emx_link.py is a simplistic emulation of the Unix link(2) library routine
6n/afor creating so-called hard links. It is intended to be imported into
7n/athe os module in place of the unimplemented (on OS/2) Posix link()
8n/afunction (os.link()).
9n/a
10n/aWe do this on OS/2 by implementing a file copy, with link(2) semantics:-
11n/a - the target cannot already exist;
12n/a - we hope that the actual file open (if successful) is actually
13n/a atomic...
14n/a
15n/aLimitations of this approach/implementation include:-
16n/a - no support for correct link counts (EMX stat(target).st_nlink
17n/a is always 1);
18n/a - thread safety undefined;
19n/a - default file permissions (r+w) used, can't be over-ridden;
20n/a - implemented in Python so comparatively slow, especially for large
21n/a source files;
22n/a - need sufficient free disk space to store the copy.
23n/a
24n/aBehaviour:-
25n/a - any exception should propagate to the caller;
26n/a - want target to be an exact copy of the source, so use binary mode;
27n/a - returns None, same as os.link() which is implemented in posixmodule.c;
28n/a - target removed in the event of a failure where possible;
29n/a - given the motivation to write this emulation came from trying to
30n/a support a Unix resource lock implementation, where minimal overhead
31n/a during creation of the target is desirable and the files are small,
32n/a we read a source block before attempting to create the target so that
33n/a we're ready to immediately write some data into it.
34n/a"""
35n/a
36n/aimport os
37n/aimport errno
38n/a
39n/a__all__ = ['link']
40n/a
41n/adef link(source, target):
42n/a """link(source, target) -> None
43n/a
44n/a Attempt to hard link the source file to the target file name.
45n/a On OS/2, this creates a complete copy of the source file.
46n/a """
47n/a
48n/a s = os.open(source, os.O_RDONLY | os.O_BINARY)
49n/a if os.isatty(s):
50n/a raise OSError(errno.EXDEV, 'Cross-device link')
51n/a data = os.read(s, 1024)
52n/a
53n/a try:
54n/a t = os.open(target, os.O_WRONLY | os.O_BINARY | os.O_CREAT | os.O_EXCL)
55n/a except OSError:
56n/a os.close(s)
57n/a raise
58n/a
59n/a try:
60n/a while data:
61n/a os.write(t, data)
62n/a data = os.read(s, 1024)
63n/a except OSError:
64n/a os.close(s)
65n/a os.close(t)
66n/a os.unlink(target)
67n/a raise
68n/a
69n/a os.close(s)
70n/a os.close(t)
71n/a
72n/aif __name__ == '__main__':
73n/a import sys
74n/a try:
75n/a link(sys.argv[1], sys.argv[2])
76n/a except IndexError:
77n/a print('Usage: emx_link <source> <target>')
78n/a except OSError:
79n/a print('emx_link: %s' % str(sys.exc_info()[1]))