rust/hg-cpython/src/vfs.rs
changeset 52185 8d35941689af
parent 52184 735bf027dd1d
equal deleted inserted replaced
52184:735bf027dd1d 52185:8d35941689af
    12 };
    12 };
    13 use hg::{
    13 use hg::{
    14     errors::{HgError, IoResultExt},
    14     errors::{HgError, IoResultExt},
    15     exit_codes,
    15     exit_codes,
    16     utils::files::{get_bytes_from_path, get_path_from_bytes},
    16     utils::files::{get_bytes_from_path, get_path_from_bytes},
    17     vfs::Vfs,
    17     vfs::{Vfs, VfsFile},
    18 };
    18 };
    19 
    19 
    20 /// Wrapper around a Python VFS object to call back into Python from `hg-core`.
    20 /// Wrapper around a Python VFS object to call back into Python from `hg-core`.
    21 pub struct PyVfs {
    21 pub struct PyVfs {
    22     inner: PyObject,
    22     inner: PyObject,
   139         Ok(self.number(py).get().to_py_object(py))
   139         Ok(self.number(py).get().to_py_object(py))
   140     }
   140     }
   141 });
   141 });
   142 
   142 
   143 impl Vfs for PyVfs {
   143 impl Vfs for PyVfs {
   144     fn open(&self, filename: &Path) -> Result<File, HgError> {
   144     fn open(&self, filename: &Path) -> Result<VfsFile, HgError> {
   145         self.inner_open(filename, false, false, false, true)
   145         self.inner_open(filename, false, false, false, true)
   146             .map(|(f, _)| f)
   146             .map(|(f, _)| VfsFile::normal(f, filename.to_owned()))
   147     }
   147     }
   148     fn open_read(&self, filename: &Path) -> Result<File, HgError> {
   148     fn open_read(&self, filename: &Path) -> Result<VfsFile, HgError> {
   149         self.inner_open(filename, false, false, false, false)
   149         self.inner_open(filename, false, false, false, false)
   150             .map(|(f, _)| f)
   150             .map(|(f, _)| VfsFile::normal(f, filename.to_owned()))
   151     }
   151     }
   152 
   152 
   153     fn open_check_ambig(
   153     fn open_check_ambig(&self, filename: &Path) -> Result<VfsFile, HgError> {
       
   154         self.inner_open(filename, false, true, false, true)
       
   155             .map(|(f, _)| VfsFile::normal(f, filename.to_owned()))
       
   156     }
       
   157 
       
   158     fn create(
   154         &self,
   159         &self,
   155         filename: &Path,
   160         filename: &Path,
   156     ) -> Result<std::fs::File, HgError> {
   161         check_ambig: bool,
   157         self.inner_open(filename, false, true, false, true)
   162     ) -> Result<VfsFile, HgError> {
   158             .map(|(f, _)| f)
   163         self.inner_open(filename, true, check_ambig, false, true)
   159     }
   164             .map(|(f, _)| VfsFile::normal(f, filename.to_owned()))
   160 
       
   161     fn create(&self, filename: &Path) -> Result<std::fs::File, HgError> {
       
   162         self.inner_open(filename, true, false, false, true)
       
   163             .map(|(f, _)| f)
       
   164     }
   165     }
   165 
   166 
   166     fn create_atomic(
   167     fn create_atomic(
   167         &self,
   168         &self,
   168         filename: &Path,
   169         filename: &Path,
   169         check_ambig: bool,
   170         check_ambig: bool,
   170     ) -> Result<hg::vfs::AtomicFile, HgError> {
   171     ) -> Result<VfsFile, HgError> {
   171         self.inner_open(filename, true, false, true, true).map(
   172         self.inner_open(filename, true, false, true, true).map(
   172             |(fp, temp_name)| {
   173             |(fp, temp_name)| {
   173                 hg::vfs::AtomicFile::from_file(
   174                 VfsFile::Atomic(hg::vfs::AtomicFile::from_file(
   174                     fp,
   175                     fp,
   175                     check_ambig,
   176                     check_ambig,
   176                     temp_name.expect("temp name should exist"),
   177                     temp_name.expect("temp name should exist"),
   177                     filename.to_owned(),
   178                     filename.to_owned(),
   178                 )
   179                 ))
   179             },
   180             },
   180         )
   181         )
   181     }
   182     }
   182 
   183 
   183     fn file_size(&self, file: &File) -> Result<u64, HgError> {
   184     fn file_size(&self, file: &VfsFile) -> Result<u64, HgError> {
   184         let gil = &Python::acquire_gil();
   185         let gil = &Python::acquire_gil();
   185         let py = gil.python();
   186         let py = gil.python();
   186         let raw_fd = file.as_raw_fd();
   187         let raw_fd = file.as_raw_fd();
   187         let py_fd = PyFile::create_instance(py, Cell::new(raw_fd))
   188         let py_fd = PyFile::create_instance(py, Cell::new(raw_fd))
   188             .expect("create_instance cannot fail");
   189             .expect("create_instance cannot fail");