mercurial/appendfile.py
changeset 2177 6886bc0b77af
parent 2165 d821918e3bee
parent 2176 9b42304d9896
child 2232 ef3c039e7ab8
equal deleted inserted replaced
2174:3044a3fdae76 2177:6886bc0b77af
     4 #
     4 #
     5 # This software may be used and distributed according to the terms
     5 # This software may be used and distributed according to the terms
     6 # of the GNU General Public License, incorporated herein by reference.
     6 # of the GNU General Public License, incorporated herein by reference.
     7 
     7 
     8 from demandload import *
     8 from demandload import *
     9 demandload(globals(), "cStringIO changelog errno manifest os tempfile")
     9 demandload(globals(), "cStringIO changelog errno manifest os tempfile util")
    10 
    10 
    11 # writes to metadata files are ordered.  reads: changelog, manifest,
    11 # writes to metadata files are ordered.  reads: changelog, manifest,
    12 # normal files.  writes: normal files, manifest, changelog.
    12 # normal files.  writes: normal files, manifest, changelog.
    13 
    13 
    14 # manifest contains pointers to offsets in normal files.  changelog
    14 # manifest contains pointers to offsets in normal files.  changelog
    34     writedata called.'''
    34     writedata called.'''
    35 
    35 
    36     def __init__(self, fp, tmpname):
    36     def __init__(self, fp, tmpname):
    37         if tmpname:
    37         if tmpname:
    38             self.tmpname = tmpname
    38             self.tmpname = tmpname
    39             self.tmpfp = open(self.tmpname, 'ab+')
    39             self.tmpfp = util.posixfile(self.tmpname, 'ab+')
    40         else:
    40         else:
    41             fd, self.tmpname = tempfile.mkstemp(prefix="hg-appendfile-")
    41             fd, self.tmpname = tempfile.mkstemp(prefix="hg-appendfile-")
    42             self.tmpfp = os.fdopen(fd, 'ab+')
    42             os.close(fd)
       
    43             self.tmpfp = util.posixfile(self.tmpname, 'ab+')
    43         self.realfp = fp
    44         self.realfp = fp
    44         self.offset = fp.tell()
    45         self.offset = fp.tell()
    45         # real file is not written by anyone else. cache its size so
    46         # real file is not written by anyone else. cache its size so
    46         # seek and read can be fast.
    47         # seek and read can be fast.
    47         self.realsize = os.fstat(fp.fileno()).st_size
    48         self.realsize = util.fstat(fp).st_size
       
    49         self.name = fp.name
    48 
    50 
    49     def end(self):
    51     def end(self):
    50         self.tmpfp.flush() # make sure the stat is correct
    52         self.tmpfp.flush() # make sure the stat is correct
    51         return self.realsize + os.fstat(self.tmpfp.fileno()).st_size
    53         return self.realsize + util.fstat(self.tmpfp).st_size
    52 
    54 
    53     def tell(self):
    55     def tell(self):
    54         return self.offset
    56         return self.offset
    55 
    57 
    56     def flush(self):
    58     def flush(self):