~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/store.py

  • Committer: Martin Pool
  • Date: 2005-06-20 04:33:23 UTC
  • Revision ID: mbp@sourcefrog.net-20050620043323-1f681c2b5b15d026
- write into store using AtomicFile

Show diffs side-by-side

added added

removed removed

Lines of Context:
78
78
        """Add contents of a file into the store.
79
79
 
80
80
        f -- An open file, or file-like object."""
81
 
        # FIXME: Only works on smallish files
82
 
        # TODO: Can be optimized by copying at the same time as
83
 
        # computing the sum.
 
81
        # FIXME: Only works on files that will fit in memory
 
82
        
 
83
        from bzrlib.atomicfile import AtomicFile
 
84
        
84
85
        mutter("add store entry %r" % (fileid))
85
86
        if isinstance(f, types.StringTypes):
86
87
            content = f
87
88
        else:
88
89
            content = f.read()
89
 
 
 
90
            
90
91
        p = self._path(fileid)
91
92
        if os.access(p, os.F_OK) or os.access(p + '.gz', os.F_OK):
92
93
            raise BzrError("store %r already contains id %r" % (self._basedir, fileid))
93
94
 
 
95
        fn = p
94
96
        if compressed:
95
 
            f = gzip.GzipFile(p + '.gz', 'wb')
96
 
        else:
97
 
            f = file(p, 'wb')
 
97
            fn = fn + '.gz'
98
98
            
99
 
        f.write(content)
100
 
        f.close()
 
99
        af = AtomicFile(fn, 'wb')
 
100
        try:
 
101
            if compressed:
 
102
                gf = gzip.GzipFile(mode='wb', fileobj=af)
 
103
                gf.write(content)
 
104
                gf.close()
 
105
            else:
 
106
                af.write(content)
 
107
            af.commit()
 
108
        finally:
 
109
            af.close()
101
110
 
102
111
 
103
112
    def copy_multi(self, other, ids):