Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

follow_path() on windows just loops over the file #2

Open
vasakt opened this issue Sep 13, 2016 · 6 comments
Open

follow_path() on windows just loops over the file #2

vasakt opened this issue Sep 13, 2016 · 6 comments

Comments

@vasakt
Copy link

vasakt commented Sep 13, 2016

Hi

I'm finding that follow_path() on windows just loops over the same file, over and over without waiting at the end of file.

for line in tailhead.follow_path(filepath):
    if line is not None:
        print(line)
    else:
        time.sleep(5)

on a file with lines 1 2 3 .. 11 gives line 1, 2, 3, .., 11, 1, 2, ... over and over.

Thanks

@vasakt
Copy link
Author

vasakt commented Sep 13, 2016

Looks like os.stat(...).st_ino doesn't work on windows on python 2.7. Always returns 0. So everything breaks.

@Kentzo
Copy link
Member

Kentzo commented Sep 13, 2016

@vasakt Interesting. What solution would you propose in that case? We could resort to some WinAPI call here through ctypes.

@vasakt
Copy link
Author

vasakt commented Sep 14, 2016

Not sure of the best solution but I believe that you could use the win32file package. Something like:

def getReadHandle(filename):
  if os.path.isdir(filename):
    dwFlagsAndAttributes = win32file.FILE_FLAG_BACKUP_SEMANTICS
  else:
    dwFlagsAndAttributes = 0
  return win32file.CreateFile(
    filename,
    win32file.GENERIC_READ,
    win32file.FILE_SHARE_READ,
    None,
    win32file.OPEN_EXISTING,
    dwFlagsAndAttributes,
    None
  )
def getUniqueID(hFile):
  (
    attributes,
    created_at, accessed_at, written_at,
    volume,
    file_hi, file_lo,
    n_links,
    index_hi, index_lo
  ) = win32file.GetFileInformationByHandle(hFile)
  return volume, index_hi, index_lo
def filesIdentical(filename1, filename2):
  hFile1 = getReadHandle(filename1)
  hFile2 = getReadHandle(filename2)
  areIdentical= getUniqueID(hFile1) == getUniqueID(hFile2)
  hFile2.Close()
  hFile1.Close()
  return areIdentical

might work.

@Kentzo
Copy link
Member

Kentzo commented Sep 14, 2016

@vasakt I want to find out what was modified in CPython to get it to work. Probably I will be able to just port this approach.

@pankajp
Copy link

pankajp commented Mar 27, 2017

I think on windows you could just compare the creation times of the files to check if the file has been recreated.

Replacing os.stat(file_path).st_ino != os.fstat(self.following_file.fileno()).st_ino with os.stat(file_path).st_ctime != os.fstat(self.following_file.fileno()).st_ctime ( https://github.com/GreatFruitOmsk/tailhead/blob/master/tailhead/__init__.py#L442 ) on windows should work.

@Kentzo
Copy link
Member

Kentzo commented Mar 27, 2017

Within a certain interval. Probably good enough as a first approach to the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants