Repository: LMCache/LMCache
File: lmcache/v1/storage_backend/connector/fs_connector.py (Python)

Code before:
                    memory_obj.get_memory_format(),
                )
                if self.save_chunk_meta
                else None
            )

            size = len(buffer)
            do_use_odirect = self.use_odirect
            if do_use_odirect:
                fblock_aligned = self.os_disk_bs > 0 and size % self.os_disk_bs == 0
                if not fblock_aligned:
                    logger.warning(
                        f"Cannot use O_DIRECT for writing size {size}, "
                        f"which is not aligned to block size {self.os_disk_bs}."
                    )
                    do_use_odirect = False

            if do_use_odirect:
                # Use Direct I/O
                await self.loop.run_in_executor(
                    None, self._put_with_odirect, temp_path, buffer
                )
            else:
                # Use standard async I/O
                # Write to file (metadata + data)
                async with aiofiles.open(temp_path, "wb") as f:
                    if metadata is not None:
                        await f.write(metadata.serialize())
                    await f.write(buffer)

            # Atomically rename temp file to final destination
            await aiofiles.os.replace(temp_path, final_path)

        except Exception as e:
            logger.error(f"Failed to write file {final_path}: {str(e)}")
            if await aiofiles.os.path.exists(temp_path):
                await aiofiles.os.unlink(temp_path)  # Remove corrupted file
            raise

    def remove_sync(self, key: CacheEngineKey) -> bool:
        """
        Remove the file associated with the given key.

        Args:
            key: The key to remove.

        Returns:
            bool: True if the file was successfully removed, False otherwise.
        """
        file_path = self._get_file_path(key)
        try:

Review comment (suggestion):
This is so strict, is there a way to put the head aligned part by `_put_with_odirect` and the tail non-aligned part using `aiofiles.write`.

Or you can fill some padding zero to the tail of the chunk file.

Code after:
                    os.close(fd)
                except OSError:
                    pass

    async def put(self, key: CacheEngineKey, memory_obj: MemoryObj):
        """Store data to file system"""
        final_path, temp_path = self._get_file_and_tmp_path(key)

        try:
            # Prepare metadata
            buffer = memory_obj.byte_array
            metadata = (
                RemoteMetadata(
                    len(buffer),
                    memory_obj.get_shape(),
                    memory_obj.get_dtype(),
                    memory_obj.get_memory_format(),
                )
                if self.save_chunk_meta
                else None
            )

            size = len(buffer)
            do_use_odirect = self.use_odirect
            if do_use_odirect:
                fblock_aligned = self.os_disk_bs > 0 and size % self.os_disk_bs == 0
                if not fblock_aligned:
                    logger.warning(
                        f"Cannot use O_DIRECT for writing size {size}, "
                        f"which is not aligned to block size {self.os_disk_bs}."
                    )
                    do_use_odirect = False

            if do_use_odirect:
                # Use Direct I/O
                await self.loop.run_in_executor(
                    None, self._put_with_odirect, temp_path, buffer
                )
            else:
                # Use standard async I/O
                # Write to file (metadata + data)
                async with aiofiles.open(temp_path, "wb") as f:
                    if metadata is not None:
                        await f.write(metadata.serialize())
                    await f.write(buffer)

            # Atomically rename temp file to final destination
            await aiofiles.os.replace(temp_path, final_path)

        except Exception as e:
            logger.error(f"Failed to write file {final_path}: {str(e)}")