def write(self, digest, content): assert content is not None with self._lock: self._protected = self._protected or digest path = self._path(digest) # A stale broken file may remain. It is possible for the file to have write # access bit removed which would cause the file_write() call to fail to open # in write mode. Take no chance here. file_path.try_remove(path) try: size = file_write(path, content) except: # There are two possible places were an exception can occur: # 1) Inside |content| generator in case of network or unzipping errors. # 2) Inside file_write itself in case of disk IO errors. # In any case delete an incomplete file and propagate the exception to # caller, it will be logged there. file_path.try_remove(path) raise # Make the file read-only in the cache. This has a few side-effects since # the file node is modified, so every directory entries to this file becomes # read-only. It's fine here because it is a new file. file_path.set_read_only(path, True) with self._lock: self._add(digest, size) return digest
def tearDown(self): for dirpath, dirnames, filenames in os.walk(self.tempdir, topdown=True): for filename in filenames: file_path.set_read_only(os.path.join(dirpath, filename), False) for dirname in dirnames: file_path.set_read_only(os.path.join(dirpath, dirname), False) file_path.rmtree(self.tempdir) super(RunIsolatedTestBase, self).tearDown()
def tearDown(self): for dirpath, dirnames, filenames in os.walk(self.tempdir, topdown=True): for filename in filenames: file_path.set_read_only(os.path.join(dirpath, filename), False) for dirname in dirnames: file_path.set_read_only(os.path.join(dirpath, dirname), False) shutil.rmtree(self.tempdir) super(RunIsolatedTestBase, self).tearDown()
def tearDown(self): if self._tempdir: for dirpath, dirnames, filenames in os.walk(self._tempdir, topdown=True): for filename in filenames: file_path.set_read_only(os.path.join(dirpath, filename), False) for dirname in dirnames: file_path.set_read_only(os.path.join(dirpath, dirname), False) shutil.rmtree(self._tempdir) super(FilePathTest, self).tearDown()
def _save(self): """Saves the LRU ordering.""" self._lock.assert_locked() if sys.platform != 'win32': d = os.path.dirname(self.state_file) if fs.isdir(d): # Necessary otherwise the file can't be created. file_path.set_read_only(d, False) if fs.isfile(self.state_file): file_path.set_read_only(self.state_file, False) self._lru.save(self.state_file)
def tearDown(self): try: if self._tempdir: for dirpath, dirnames, filenames in fs.walk( self._tempdir, topdown=True): for filename in filenames: file_path.set_read_only(os.path.join(dirpath, filename), False) for dirname in dirnames: file_path.set_read_only(os.path.join(dirpath, dirname), False) file_path.rmtree(self._tempdir) finally: super(FilePathTest, self).tearDown()
def test_hard_link_mode(self): # Creates a hard link, see if the file mode changed on the node or the # directory entry. dir_foo = os.path.join(self.tempdir, 'foo') file_bar = os.path.join(dir_foo, 'bar') file_link = os.path.join(dir_foo, 'link') fs.mkdir(dir_foo, 0777) write_content(file_bar, 'bar') file_path.hardlink(file_bar, file_link) self.assertFileMode(file_bar, 0100666) self.assertFileMode(file_link, 0100666) file_path.set_read_only(file_bar, True) self.assertMaskedFileMode(file_bar, 0100444) self.assertMaskedFileMode(file_link, 0100444)
def test_delete_rd_rf(self): # Confirms that a RO file in a RO directory can't be deleted. dir_foo = os.path.join(self.tempdir, 'foo') file_bar = os.path.join(dir_foo, 'bar') fs.mkdir(dir_foo, 0777) write_content(file_bar, 'bar') file_path.set_read_only(dir_foo, True) file_path.set_read_only(file_bar, True) self.assertMaskedFileMode(dir_foo, 040555) self.assertMaskedFileMode(file_bar, 0100444) with self.assertRaises(OSError): # It fails for different reason depending on the OS. See the test cases # above. fs.remove(file_bar)
def test_delete_wd_rf(self): # Confirms that a RO file in a RW directory can be deleted on non-Windows. dir_foo = os.path.join(self.tempdir, 'foo') file_bar = os.path.join(dir_foo, 'bar') fs.mkdir(dir_foo, 0777) write_content(file_bar, 'bar') file_path.set_read_only(dir_foo, False) file_path.set_read_only(file_bar, True) self.assertFileMode(dir_foo, 040777) self.assertMaskedFileMode(file_bar, 0100444) if sys.platform == 'win32': # On Windows, a read-only file can't be deleted. with self.assertRaises(OSError): fs.remove(file_bar) else: fs.remove(file_bar)
def test_delete_rd_wf(self): # Confirms that a Rw file in a RO directory can be deleted on Windows only. dir_foo = os.path.join(self.tempdir, 'foo') file_bar = os.path.join(dir_foo, 'bar') fs.mkdir(dir_foo, 0777) write_content(file_bar, 'bar') file_path.set_read_only(dir_foo, True) file_path.set_read_only(file_bar, False) self.assertMaskedFileMode(dir_foo, 040555) self.assertFileMode(file_bar, 0100666) if sys.platform == 'win32': # A read-only directory has a convoluted meaning on Windows, it means that # the directory is "personalized". This is used as a signal by Windows # Explorer to tell it to look into the directory for desktop.ini. # See http://support.microsoft.com/kb/326549 for more details. # As such, it is important to not try to set the read-only bit on # directories on Windows since it has no effect other than trigger # Windows Explorer to look for desktop.ini, which is unnecessary. fs.remove(file_bar) else: with self.assertRaises(OSError): fs.remove(file_bar)