[Python-bugs-list] dircache not kept consistent (PR#188)

tage@acm.org tage@acm.org
Mon, 24 Jan 2000 11:56:47 -0500 (EST)


Full_Name: Tage Stabell-Kulø
Version: 1.5.2
OS: FreeBSD
Submission from: (NULL) (129.242.16.231)


The documentation for the dircache module states:
"Return a directory listing of path, as gotten from 
os.listdir(). Note that unless path changes, further call 
to listdir() will not re-read the directory structure. "

The following program demonstrates that this is misleading (at best):
#! /usr/bin/env python

import whrandom
import os
import dircache
import string

generator = whrandom.whrandom()

while 1:
    dirlist = dircache.listdir(".")
    files = []
    while 1:
        file = ""
        for i in range(10):
            file = file + string.hexdigits[generator.randint(0,15)]
        files.append(file);
        fd = open(file, "w")
        fd.close()
        if file in dircache.listdir("."):
            break
    print "Created %d files" % len(files)
    for i in files:
        os.unlink(i)

The output (on my FreeBSD machine) is
Created 28 files
Created 69 files
Created 48 files
Created 58 files
Created 53 files

In my view, this is an outright bug.  I suggest that either the documentation
is changed to say:
	The cache is updated only once a second.

The reason is, of cource, that the st_mtime field is a time_t and holds 
seconds.

Or, better, change posixmodule.c with this patch (more text after the patch)
======
--- posixmodule.c.org   Mon Jan 24 17:08:06 2000
+++ posixmodule.c       Mon Jan 24 17:31:39 2000
@@ -559,7 +559,7 @@
                             (long)st.st_mtime,
                             (long)st.st_ctime);
 #else
-       return Py_BuildValue("(lLllllLlll)",
+       return Py_BuildValue("(lLllllLllll)",
                             (long)st.st_mode,
                             (LONG_LONG)st.st_ino,
                             (long)st.st_dev,
@@ -569,7 +569,8 @@
                             (LONG_LONG)st.st_size,
                             (long)st.st_atime,
                             (long)st.st_mtime,
-                            (long)st.st_ctime);
+                            (long)st.st_ctime,
+                            (long)st.st_mtimespec.tv_nsec);
 #endif
 }
 
@@ -2637,7 +2638,7 @@
                             (long)st.st_mtime,
                             (long)st.st_ctime);
 #else
-       return Py_BuildValue("(lLllllLlll)",
+       return Py_BuildValue("(lLllllLllll)",
                             (long)st.st_mode,
                             (LONG_LONG)st.st_ino,
                             (long)st.st_dev,
@@ -2647,7 +2648,8 @@
                             (LONG_LONG)st.st_size,
                             (long)st.st_atime,
                             (long)st.st_mtime,
-                            (long)st.st_ctime);
+                            (long)st.st_ctime,
+                            (long)st.st_mtimespec.tv_nsec);
 #endif
 }


======

and apply the following patch to the dircache module:

 +++ dircache.py Mon Jan 24 17:48:53 2000
@@ -15,7 +15,11 @@
        except KeyError:
                cached_mtime, list = -1, []
        try:
-               mtime = os.stat(path)[8]
+               st = os.stat(path)
+               if len(st) == 11:
+                       mtime = st[10]
+               else:
+                       mtime = st[8]
        except os.error:
                return []
        if mtime <> cached_mtime:


I appreciate your effort to maintain Python; I hope you find my small
contribution useful.

=====================================================================


I confirm that, to the best of my knowledge and belief, this
contribution is free of any claims of third parties under
copyright, patent or other rights or interests ("claims").  To
the extent that I have any such claims, I hereby grant to CNRI a
nonexclusive, irrevocable, royalty-free, worldwide license to
reproduce, distribute, perform and/or display publicly, prepare
derivative versions, and otherwise use this contribution as part
of the Python software and its related documentation, or any
derivative versions thereof, at no cost to CNRI or its licensed
users, and to authorize others to do so.

I acknowledge that CNRI may, at its sole discretion, decide
whether or not to incorporate this contribution in the Python
software and its related documentation.  I further grant CNRI
permission to use my name and other identifying information
provided to CNRI by me for use in connection with the Python
software and its related documentation.