Beispiel #1
0
    def test_read(self):
        self._fillfile(b"0123456789")
        ghost = GhostFile(self._datapath, None)

        with open(self._datapath, 'rb') as f:
            self.assertEqual(ghost.read(10, 0, f.fileno()), b"0123456789")
        with open(self._datapath, 'rb') as f:
            self.assertEqual(ghost.read(5, 5, f.fileno()), b"56789")
        with open(self._datapath, 'rb') as f:
            self.assertEqual(ghost.read(5, 8, f.fileno()), b"89")
        with open(self._datapath, 'rb') as f:
            self.assertEqual(ghost.read(5, 10, f.fileno()), b"")

        with open(self._datapath, 'r+b') as f:
            ghost.apply(f.fileno())
        ghost.release()

        # Make sure the file was not modified
        with open(self._datapath, 'rb') as f:
            self.assertEqual(f.read(), b"0123456789")
Beispiel #2
0
    def test_truncate(self):
        self._fillfile(b"0123456789")
        ghost = GhostFile(self._datapath, None)

        with open(self._datapath, 'r+b') as f:
            ghost.truncate(0)
            self.assertEqual(ghost.size, 0)
            self.assertEqual(ghost.read(100, 0, f.fileno()), b"")

        # Make sure the file was not modified
        with open(self._datapath, 'rb') as f:
            self.assertEqual(f.read(), b"0123456789")

        with open(self._datapath, 'r+b') as f:
            ghost.apply(f.fileno())
        ghost.release()

        # Make sure the file *was* modified
        with open(self._datapath, 'rb') as f:
            self.assertEqual(f.read(), b"")
Beispiel #3
0
    def _add_ghost_file(self, ghost_path: str) -> GhostFile:
        """
        Adds a ghost file for the specified virtual path.
        If a ghost file already exists for that path, it doesn't add another one but keeps track
        of this additional reference (see `SemanticFS._delete_ghost_file`).
        :param ghost_path: the virtual path for the ghost file
        :return: the added GhostFile
        """
        dspath = self._datastore_path(ghost_path)
        normpath = os.path.normcase(os.path.normpath(ghost_path))

        if (dspath, normpath) in self._sem_writing_files:
            assert self._sem_writing_files_count[dspath, normpath] > 0
            self._sem_writing_files_count[dspath, normpath] += 1
        else:
            assert (dspath, normpath) not in self._sem_writing_files_count
            self._sem_writing_files[dspath, normpath] = GhostFile(
                dspath, lambda: self._clear_stat_cache(PathInfo(ghost_path)))
            self._sem_writing_files_count[dspath, normpath] = 1

        assert (dspath, normpath
                ) in self._sem_writing_files and self._sem_writing_files_count[
                    dspath, normpath] > 0
        return self._sem_writing_files[dspath, normpath]
Beispiel #4
0
    def test_write_same(self):
        self._fillfile(b"0123456789")
        ghost = GhostFile(self._datapath, None)

        with open(self._datapath, 'r+b') as f:
            ghost.write(b"01234", 0, f.fileno())
            self.assertEqual(ghost.read(100, 0, f.fileno()), b"0123456789")

        with open(self._datapath, 'r+b') as f:
            ghost.write(b"789", 7, f.fileno())
            self.assertEqual(ghost.read(100, 0, f.fileno()), b"0123456789")

        with open(self._datapath, 'r+b') as f:
            ghost.write(b"345", 3, f.fileno())
            self.assertEqual(ghost.read(100, 0, f.fileno()), b"0123456789")

        with open(self._datapath, 'r+b') as f:
            ghost.apply(f.fileno())
        ghost.release()

        # Make sure the file was not modified
        with open(self._datapath, 'rb') as f:
            self.assertEqual(f.read(), b"0123456789")
Beispiel #5
0
 def test_empty_read(self):
     self._fillfile(b"")
     ghost = GhostFile(self._datapath, None)
     with open(self._datapath, 'rb') as f:
         self.assertEqual(ghost.read(10, 0, f.fileno()), b"")
     ghost.release()
Beispiel #6
0
    def test_write_diff(self):
        self._fillfile(b"0123456789")
        ghost = GhostFile(self._datapath, None)

        with open(self._datapath, 'r+b') as f:
            ghost.write(b"abcd", 0, f.fileno())
            self.assertEqual(ghost.read(100, 0, f.fileno()), b"abcd456789")

        # Make sure the file *was* modified
        with open(self._datapath, 'rb') as f:
            self.assertEqual(f.read(), b"abcd456789")

        with open(self._datapath, 'r+b') as f:
            ghost.write(b"hij", 7, f.fileno())
            self.assertEqual(ghost.read(100, 0, f.fileno()), b"abcd456hij")

        with open(self._datapath, 'r+b') as f:
            ghost.write(b"XXX", 3, f.fileno())
            self.assertEqual(ghost.read(100, 0, f.fileno()), b"abcXXX6hij")

        with open(self._datapath, 'r+b') as f:
            ghost.apply(f.fileno())
        ghost.release()

        # Make sure the file *was* modified
        with open(self._datapath, 'rb') as f:
            self.assertEqual(f.read(), b"abcXXX6hij")