mercurial/win32.py
branchstable
changeset 11992 ccd8e592c3c5
parent 11991 50523b4407f6
child 12387 4f8067c94729
equal deleted inserted replaced
11991:50523b4407f6 11992:ccd8e592c3c5
    32     """Return number of hardlinks for the given file."""
    32     """Return number of hardlinks for the given file."""
    33     try:
    33     try:
    34         fh = win32file.CreateFile(pathname,
    34         fh = win32file.CreateFile(pathname,
    35             win32file.GENERIC_READ, win32file.FILE_SHARE_READ,
    35             win32file.GENERIC_READ, win32file.FILE_SHARE_READ,
    36             None, win32file.OPEN_EXISTING, 0, None)
    36             None, win32file.OPEN_EXISTING, 0, None)
    37         try:
       
    38             return win32file.GetFileInformationByHandle(fh)
       
    39         finally:
       
    40             fh.Close()
       
    41     except pywintypes.error:
    37     except pywintypes.error:
    42         return None
    38         raise OSError(errno.ENOENT, 'The system cannot find the file specified')
       
    39     try:
       
    40         return win32file.GetFileInformationByHandle(fh)
       
    41     finally:
       
    42         fh.Close()
    43 
    43 
    44 def nlinks(pathname):
    44 def nlinks(pathname):
    45     """Return number of hardlinks for the given file."""
    45     """Return number of hardlinks for the given file."""
    46     res = _getfileinfo(pathname)
    46     links = _getfileinfo(pathname)[7]
    47     if res is not None:
       
    48         links = res[7]
       
    49     else:
       
    50         links = os.lstat(pathname).st_nlink
       
    51     if links < 2:
    47     if links < 2:
    52         # Known to be wrong for most network drives
    48         # Known to be wrong for most network drives
    53         dirname = os.path.dirname(pathname)
    49         dirname = os.path.dirname(pathname)
    54         if not dirname:
    50         if not dirname:
    55             dirname = '.'
    51             dirname = '.'
    62 def samefile(fpath1, fpath2):
    58 def samefile(fpath1, fpath2):
    63     """Returns whether fpath1 and fpath2 refer to the same file. This is only
    59     """Returns whether fpath1 and fpath2 refer to the same file. This is only
    64     guaranteed to work for files, not directories."""
    60     guaranteed to work for files, not directories."""
    65     res1 = _getfileinfo(fpath1)
    61     res1 = _getfileinfo(fpath1)
    66     res2 = _getfileinfo(fpath2)
    62     res2 = _getfileinfo(fpath2)
    67     if res1 is not None and res2 is not None:
    63     # Index 4 is the volume serial number, and 8 and 9 contain the file ID
    68         # Index 4 is the volume serial number, and 8 and 9 contain the file ID
    64     return res1[4] == res2[4] and res1[8] == res2[8] and res1[9] == res2[9]
    69         return res1[4] == res2[4] and res1[8] == res2[8] and res1[9] == res2[9]
       
    70     else:
       
    71         return False
       
    72 
    65 
    73 def samedevice(fpath1, fpath2):
    66 def samedevice(fpath1, fpath2):
    74     """Returns whether fpath1 and fpath2 are on the same device. This is only
    67     """Returns whether fpath1 and fpath2 are on the same device. This is only
    75     guaranteed to work for files, not directories."""
    68     guaranteed to work for files, not directories."""
    76     res1 = _getfileinfo(fpath1)
    69     res1 = _getfileinfo(fpath1)
    77     res2 = _getfileinfo(fpath2)
    70     res2 = _getfileinfo(fpath2)
    78     if res1 is not None and res2 is not None:
    71     return res1[4] == res2[4]
    79         return res1[4] == res2[4]
       
    80     else:
       
    81         return False
       
    82 
    72 
    83 def testpid(pid):
    73 def testpid(pid):
    84     '''return True if pid is still running or unable to
    74     '''return True if pid is still running or unable to
    85     determine, False otherwise'''
    75     determine, False otherwise'''
    86     try:
    76     try: