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.2 2004-10-15 01:10:22 richard Exp $
18+ #$Id: blobfiles.py,v 1.12.2.3 2004-11-11 06:09:43 richard Exp $
1919'''This module exports file storage for roundup backends.
2020Files are stored into a directory hierarchy.
2121'''
@@ -39,7 +39,11 @@ class FileStorage:
3939 """Store files in some directory structure"""
4040 def filename (self , classname , nodeid , property = None ):
4141 '''Determine what the filename for the given node and optionally
42- property is.
42+ property is.
43+
44+ Try a variety of different filenames - the file could be in the
45+ usual place, or it could be in a temp file pre-commit *or* it
46+ could be in an old-style, backwards-compatible flat directory.
4347 '''
4448 if property :
4549 name = '%s%s.%s' % (classname , nodeid , property )
@@ -50,20 +54,27 @@ def filename(self, classname, nodeid, property=None):
5054
5155 # have a separate subdir for every thousand messages
5256 subdir = str (int (nodeid ) / 1000 )
53- return os .path .join (self .dir , 'files' , classname , subdir , name )
57+ filename = os .path .join (self .dir , 'files' , classname , subdir , name )
58+ if os .path .exists (filename ):
59+ return filename
5460
55- def filename_flat (self , classname , nodeid , property = None ):
56- '''Determine what the filename for the given node and optionally
57- property is.
58- '''
61+ # try .tmp
62+ filename = filename + '.tmp'
63+ if os .path .exists (filename ):
64+ return filename
65+
66+ # ok, try flat (very old-style)
5967 if property :
60- return os .path .join (self .dir , 'files' , '%s%s.%s' % (classname ,
68+ filename = os .path .join (self .dir , 'files' , '%s%s.%s' % (classname ,
6169 nodeid , property ))
6270 else :
63- # roundupdb.FileClass never specified the property name, so don't
64- # include it
65- return os .path .join (self .dir , 'files' , '%s%s' % (classname ,
71+ filename = os .path .join (self .dir , 'files' , '%s%s' % (classname ,
6672 nodeid ))
73+ if os .path .exists (filename ):
74+ return filename
75+
76+ # file just ain't there
77+ raise IOError , 'content file for %s not found' % name
6778
6879 def storefile (self , classname , nodeid , property , content ):
6980 '''Store the content of the file in the database. The property may be
@@ -89,21 +100,14 @@ def storefile(self, classname, nodeid, property, content):
89100 def getfile (self , classname , nodeid , property ):
90101 '''Get the content of the file in the database.
91102 '''
92- # try a variety of different filenames - the file could be in the
93- # usual place, or it could be in a temp file pre-commit *or* it
94- # could be in an old-style, backwards-compatible flat directory
95103 filename = self .filename (classname , nodeid , property )
96- flat_filename = self .filename_flat (classname , nodeid , property )
97- for filename in (filename , filename + '.tmp' , flat_filename ):
98- if os .path .exists (filename ):
99- f = open (filename , 'rb' )
100- break
101- else :
102- raise IOError , 'content file not found'
103- # snarf the contents and make sure we close the file
104- content = f .read ()
105- f .close ()
106- return content
104+
105+ f = open (filename , 'rb' )
106+ try :
107+ # snarf the contents and make sure we close the file
108+ return f .read ()
109+ finally :
110+ f .close ()
107111
108112 def numfiles (self ):
109113 '''Get number of files in storage, even across subdirectories.
0 commit comments