1

I'm using the tool ac2git to convert my Accurev depot to git repository. I'm facing a problem when the os.walk() function in the python file runs. Since my project has a pretty complicated build path I have nested files that have path length exceeding the 260 limitation on Windows 7.I tried using the work-arounds provided by microsoft support but it is not resolving the error. I still get the error [Winerror 3]: File not found , when in fact it is present but cannot be accessed due to the length limitation.

This is a part of the code in the ac2git.py script:

def PreserveEmptyDirs(self):
    preservedDirs = []
    for root, dirs, files in os.walk(self.gitRepo.path, topdown=True):
        for name in dirs:

            path ="\\\\?\\"+ToUnixPath(os.path.join(root, name))


            # Preserve empty directories that are not under the .git/ directory.
            if git.GetGitDirPrefix(path) is None and len(os.listdir(path)) == 0:
                filename = os.path.join(path, '.gitignore')
                with codecs.open(filename, 'w', 'utf-8') as file:
                    #file.write('# accurev2git.py preserve empty dirs\n')
                    preservedDirs.append(filename)
                if not os.path.exists(filename):
                    logger.error("Failed to preserve directory. Couldn't create '{0}'.".format(filename))
    return preservedDirs


def ToUnixPath(path):
rv = SplitPath(path)
if rv is not None:
    if rv[0] == '/':
        rv = '/' + '/'.join(rv[1:])
    else:
        rv = '/'.join(rv)
return rv

def SplitPath(path):
rv = None
if path is not None:
    path = str(path)
    rv = []
    drive, path = os.path.splitdrive(path)
    head, tail = os.path.split(path)
    while len(head) > 0 and head != '/' and head != '\\': # For an absolute path the starting slash isn't removed from head.
        rv.append(tail)
        head, tail = os.path.split(head)
    if len(tail) > 0:
        rv.append(tail)
    if len(head) > 0: # For absolute paths.
        rv.append(head)
    if len(drive) > 0:
        rv.append(drive)
    rv.reverse()
return rv

I have appended the "\\?\" in order to allow for longer path lengths but now I get this error:

FileNotFoundError: [WinError 3] The system cannot find the path specified: '\\\\?\\C:///s/cms'

I'm new to Python and I'm not very sure what is the right approach to tackle it. I have to continue using Windows 7 only. Any suggestions if this problem can be fixed another way?

  • Please fix your indentation. What happens when you update the beginning of path to \\?\ instead of \\\\?\\. – rfkortekaas Sep 01 '16 at 15:45
  • Same error occurs. – Shruti Srivastava Sep 01 '16 at 16:15
  • Why do you also have 3 slashes after c:? It should be one. – rfkortekaas Sep 01 '16 at 16:39
  • 1
    The document you linked to says: _File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, **except when using the "\\?\" prefix** as detailed in the following sections._ So you can't use forward slashes as path separator in this case. And you should use a raw string (`r"\\?\\"`) so you don't need to escape all those backslashes. – mata Sep 01 '16 at 16:42
  • @rfkortekaas the three slashes are getting appended onlt in case of these longer path names. I have pasted the code for the PreserveEmptyDir. – Shruti Srivastava Sep 01 '16 at 16:59
  • @mata I have tried that as well. I was just trying the backslash out as well. – Shruti Srivastava Sep 01 '16 at 17:00
  • 1
    Which filmsystem are you using? According to some other online resources the prefix is \?\ and you may only use backward slashes. So for example: \?\D:\test\folder\file.csv – rfkortekaas Sep 01 '16 at 19:19
  • NTFS. Yea according to what I read, it must be used only with backslash, I think thats the problem. Im trying to figure out how to change the splitpath function in the code so that the path is created with backslashes. Since python is new to me getting this done will probably take time. – Shruti Srivastava Sep 01 '16 at 19:37

1 Answers1

2

So after much ado, I made changes in the python code,

Apparently this information is very important " File I/O functions in the Windows API convert "/" to "\" as part of converting the name to an NT-style name, except when using the "\?\" prefix as detailed in the following sections."

So I just added this code to the function:

def ToUnixPath(path):
rv = SplitPath(path)
rv[:] = [item for item in rv if item != '/']
     rv = '\\'.join(rv)
     return r"\\?"+"\\"+rv

And it worked!

  • Would you mind submitting a patch (pull request) that fixes this issue back to the original repository? It would be nice to have this fixed on Windows, but you would need to make sure not to change the way Linux behaves. – nonsensickle Sep 04 '16 at 10:57
  • @nonsensickle sure . I need to put a check for linux, after taking care of that I'll submit a patch. – Shruti Srivastava Sep 06 '16 at 18:04