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