2 # Copyright 2009, Google Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
9 # * Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # * Redistributions in binary form must reproduce the above
12 # copyright notice, this list of conditions and the following disclaimer
13 # in the documentation and/or other materials provided with the
15 # * Neither the name of Google Inc. nor the names of its
16 # contributors may be used to endorse or promote products derived from
17 # this software without specific prior written permission.
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 """Hard link support for Windows.
33 This module is a SCons tool which should be include in the topmost windows
34 environment. It is usually included by the target_platform_windows tool.
43 if sys
.platform
== 'win32':
44 # Only attempt to load pywin32 on Windows systems
48 print ('Warning: Unable to load win32file module; using copy instead of'
49 ' hard linking for env.Install(). Is pywin32 present?')
51 #------------------------------------------------------------------------------
52 # Python 2.4 and 2.5's os module doesn't support os.link on Windows, even
53 # though Windows does have hard-link capability on NTFS filesystems. So by
54 # default, SCons will insist on copying files instead of linking them as it
55 # does on other (linux,mac) OS's.
57 # Use the CreateHardLink() functionality from pywin32 to provide hard link
58 # capability on Windows also.
61 def _HardLink(fs
, src
, dst
):
62 """Hard link function for hooking into SCons.Node.FS.
65 fs: Filesystem class to use.
66 src: Source filename to link to.
67 dst: Destination link name to create.
70 OSError: The link could not be created.
72 # A hard link shares file permissions from the source. On Windows, the write
73 # access of the file itself determines whether the file can be deleted
74 # (unlike Linux/Mac, where it's the write access of the containing
75 # directory). So if we made a link from a read-only file, the only way to
76 # delete it would be to make the link writable, which would have the
77 # unintended effect of making the source writable too.
79 # So if the source is read-only, we can't hard link from it.
80 if not stat
.S_IMODE(fs
.stat(src
)[stat
.ST_MODE
]) & stat
.S_IWRITE
:
81 raise OSError('Unsafe to hard-link read-only file: %s' % src
)
83 # If the file is writable, only hard-link from it if it was build by SCons.
84 # Those files shouldn't later become read-only. We don't hard-link from
85 # writable files which SCons didn't create, because those could become
86 # read-only (for example, following a 'p4 submit'), which as indicated above
87 # would make our link read-only too.
88 if not fs
.File(src
).has_builder():
89 raise OSError('Unsafe to hard-link file not built by SCons: %s' % src
)
92 win32file
.CreateHardLink(dst
, src
)
93 except win32file
.error
, msg
:
94 # Translate errors into standard OSError which SCons expects.
98 #------------------------------------------------------------------------------
102 # NOTE: SCons requires the use of this name, which fails gpylint.
103 """SCons entry point for this tool."""
104 env
= env
# Silence gpylint
106 # Patch in our hard link function, if we were able to load pywin32
107 if 'win32file' in globals():
108 SCons
.Node
.FS
._hardlink
_func
= _HardLink