1515# BASIS, AND THERE IS NO OBLIGATION WHATSOEVER TO PROVIDE MAINTENANCE,
1616# SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
1717#
18- #$Id: blobfiles.py,v 1.12.2.3 2004-11-11 06:09:43 richard Exp $
18+ #$Id: blobfiles.py,v 1.12.2.4 2004-11-22 02:38:24 richard Exp $
1919'''This module exports file storage for roundup backends.
2020Files are stored into a directory hierarchy.
2121'''
@@ -37,7 +37,7 @@ def files_in_dir(dir):
3737
3838class FileStorage :
3939 """Store files in some directory structure"""
40- def filename (self , classname , nodeid , property = None ):
40+ def filename (self , classname , nodeid , property = None , create = False ):
4141 '''Determine what the filename for the given node and optionally
4242 property is.
4343
@@ -55,7 +55,7 @@ def filename(self, classname, nodeid, property=None):
5555 # have a separate subdir for every thousand messages
5656 subdir = str (int (nodeid ) / 1000 )
5757 filename = os .path .join (self .dir , 'files' , classname , subdir , name )
58- if os .path .exists (filename ):
58+ if create or os .path .exists (filename ):
5959 return filename
6060
6161 # try .tmp
@@ -82,7 +82,7 @@ def storefile(self, classname, nodeid, property, content):
8282 is being saved.
8383 '''
8484 # determine the name of the file to write to
85- name = self .filename (classname , nodeid , property )
85+ name = self .filename (classname , nodeid , property , create = True )
8686
8787 # make sure the file storage dir exists
8888 if not os .path .exists (os .path .dirname (name )):
@@ -91,10 +91,11 @@ def storefile(self, classname, nodeid, property, content):
9191 # save to a temp file
9292 name = name + '.tmp'
9393
94- # save off the rename action
95- self .transactions .append ((self .doStoreFile , (classname , nodeid ,
96- property )))
97-
94+ # make sure we don't register the rename action more than once
95+ if not os .path .exists (name ):
96+ # save off the rename action
97+ self .transactions .append ((self .doStoreFile , (classname , nodeid ,
98+ property )))
9899 open (name , 'wb' ).write (content )
99100
100101 def getfile (self , classname , nodeid , property ):
@@ -121,16 +122,21 @@ def doStoreFile(self, classname, nodeid, property, **databases):
121122 # determine the name of the file to write to
122123 name = self .filename (classname , nodeid , property )
123124
124- if not os .path .exists (name + ".tmp" ):
125- return
125+ # the file is currently ".tmp" - move it to its real name to commit
126+ if name .endswith ('.tmp' ):
127+ # creation
128+ dstname = os .path .splitext (name )[0 ]
129+ else :
130+ # edit operation
131+ dstname = name
132+ name = name + '.tmp'
126133
127134 # content is being updated (and some platforms, eg. win32, won't
128135 # let us rename over the top of the old file)
129- if os .path .exists (name ):
130- os .remove (name )
136+ if os .path .exists (dstname ):
137+ os .remove (dstname )
131138
132- # the file is currently ".tmp" - move it to its real name to commit
133- os .rename (name + ".tmp" , name )
139+ os .rename (name , dstname )
134140
135141 # return the classname, nodeid so we reindex this content
136142 return (classname , nodeid )
0 commit comments