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