1## @file
2# Override built in function file.open to provide support for long file path
3#
4# Copyright (c) 2014 - 2015, Intel Corporation. All rights reserved.<BR>
5# SPDX-License-Identifier: BSD-2-Clause-Patent
6#
7
8import os
9import platform
10import shutil
11import codecs
12
13##
14# OpenLongPath
15# Convert a file path to a long file path
16#
17def LongFilePath(FileName):
18    FileName = os.path.normpath(FileName)
19    if platform.system() == 'Windows':
20        if FileName.startswith('\\\\?\\'):
21            return FileName
22        if FileName.startswith('\\\\'):
23            return '\\\\?\\UNC\\' + FileName[2:]
24        if os.path.isabs(FileName):
25            return '\\\\?\\' + FileName
26    return FileName
27
28##
29# OpenLongFilePath
30# wrap open to support opening a long file path
31#
32def OpenLongFilePath(FileName, Mode='r', Buffer= -1):
33    return open(LongFilePath(FileName), Mode, Buffer)
34
35def CodecOpenLongFilePath(Filename, Mode='rb', Encoding=None, Errors='strict', Buffering=1):
36    return codecs.open(LongFilePath(Filename), Mode, Encoding, Errors, Buffering)
37
38##
39# CopyLongFilePath
40# wrap copyfile to support copy a long file path
41#
42def CopyLongFilePath(src, dst):
43    with open(LongFilePath(src), 'rb') as fsrc:
44        with open(LongFilePath(dst), 'wb') as fdst:
45            shutil.copyfileobj(fsrc, fdst)
46