Skip to content

Commit 77ca96e

Browse files
committed
mkdir support
1 parent 407c42d commit 77ca96e

6 files changed

Lines changed: 74 additions & 10 deletions

File tree

src/catfs/dir.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
extern crate libc;
22

3+
use std::fs;
4+
use std::io;
35
use std::path::Path;
46

57
use catfs::error;
@@ -71,4 +73,17 @@ impl Handle {
7173
}
7274
}
7375
}
76+
77+
pub fn mkdir(path: &AsRef<Path>, mode: u32) -> io::Result<()> {
78+
rlibc::mkdir(path, mode)
79+
}
80+
81+
pub fn rmdir(src_path: &AsRef<Path>, cache_path: &AsRef<Path>) -> io::Result<()> {
82+
if let Err(e) = fs::remove_dir(cache_path) {
83+
if !error::is_enoent(&e) {
84+
return Err(e);
85+
}
86+
}
87+
return fs::remove_dir(src_path);
88+
}
7489
}

src/catfs/file.rs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -130,15 +130,6 @@ impl Handle {
130130
return fs::remove_file(src_path);
131131
}
132132

133-
pub fn rmdir(src_path: &AsRef<Path>, cache_path: &AsRef<Path>) -> io::Result<()> {
134-
if let Err(e) = fs::remove_dir(cache_path) {
135-
if !error::is_enoent(&e) {
136-
return Err(e);
137-
}
138-
}
139-
return fs::remove_dir(src_path);
140-
}
141-
142133
fn is_pristine(cache_path: &AsRef<Path>) -> error::Result<bool> {
143134
if let Some(v) = xattr::get(cache_path, "user.catfs.pristine")? {
144135
return Ok(v == PRISTINE);

src/catfs/inode.rs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,25 @@ impl<'a> Inode<'a> {
205205
);
206206
}
207207

208+
pub fn mkdir(&self, name: &OsStr, mode: u32) -> error::Result<(Inode<'a>)> {
209+
let path = self.get_child_name(name);
210+
let src_path = self.to_src_path().join(name);
211+
dir::Handle::mkdir(&src_path, mode)?;
212+
213+
let attr = Inode::lookup_path(&src_path)?;
214+
let inode = Inode::new(
215+
self.src_dir,
216+
self.cache_dir,
217+
name.to_os_string(),
218+
path,
219+
attr,
220+
);
221+
222+
return Ok(inode);
223+
}
224+
208225
pub fn rmdir(&self, name: &OsStr) -> io::Result<()> {
209-
return file::Handle::rmdir(
226+
return dir::Handle::rmdir(
210227
&self.to_src_path().join(name),
211228
&self.to_cache_path().join(name),
212229
);

src/catfs/mod.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -534,4 +534,30 @@ impl<'a> Filesystem for CatFS<'a> {
534534
self.remove_path(&parent_inode.get_path().join(name));
535535
}
536536
}
537+
538+
fn mkdir(&mut self, _req: &Request, parent: u64, name: &OsStr, mode: u32, reply: ReplyEntry) {
539+
let parent_inode: Arc<Mutex<Inode<'a>>>;
540+
{
541+
let store = self.store.lock().unwrap();
542+
parent_inode = store.get(parent);
543+
}
544+
545+
let parent_inode = parent_inode.lock().unwrap();
546+
match parent_inode.mkdir(name, mode) {
547+
Ok(inode) => {
548+
reply.entry(&self.ttl, inode.get_attr(), 0);
549+
debug!("<-- mkdir {:?}/{:?}", parent_inode.get_path(), name);
550+
self.insert_inode(inode);
551+
}
552+
Err(e) => {
553+
debug!(
554+
"<-- !mkdir {:?}/{:?} = {}",
555+
parent_inode.get_path(),
556+
name,
557+
e
558+
);
559+
reply.error(e.raw_os_error().unwrap());
560+
}
561+
}
562+
}
537563
}

src/catfs/rlibc.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,16 @@ pub fn readdir(dir: *mut libc::DIR) -> io::Result<Option<Dirent>> {
122122
}
123123
}
124124

125+
pub fn mkdir(path: &AsRef<Path>, mode: u32) -> io::Result<()> {
126+
let s = to_cstring(path);
127+
let res = unsafe { libc::mkdir(s.as_ptr(), mode) };
128+
if res < 0 {
129+
return Err(io::Error::last_os_error());
130+
} else {
131+
return Ok(());
132+
}
133+
}
134+
125135
pub struct File {
126136
fd: libc::c_int,
127137
}

tests/integration_tests.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,6 +248,11 @@ unit_tests!{
248248
diff(&f.get_from(), &f.mnt);
249249
}
250250

251+
fn mkdir(f: &CatFSTests) {
252+
let foo = f.mnt.join("foo");
253+
fs::create_dir(&foo).unwrap();
254+
}
255+
251256
fn rmdir(f: &CatFSTests) {
252257
let dir2 = f.mnt.join("dir2");
253258
fs::remove_dir(&dir2).unwrap();

0 commit comments

Comments
 (0)