Skip to content

Commit a37126b

Browse files
committed
Auto merge of #47042 - redox-os:redox, r=estebank
Redox - Implement rename using new system call This does the following: - Update syscall module to match upstream - Implement rename using new system call - Make readlink and symlink utilize O_CLOEXEC - Make readlink and symlink not leave dangling file handles on failure
2 parents 5873a74 + 5919243 commit a37126b

File tree

3 files changed

+34
-7
lines changed

3 files changed

+34
-7
lines changed

src/libstd/sys/redox/fs.rs

+13-6
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,11 @@ pub fn unlink(p: &Path) -> io::Result<()> {
384384
}
385385

386386
pub fn rename(old: &Path, new: &Path) -> io::Result<()> {
387-
copy(old, new)?;
388-
unlink(old)?;
387+
let fd = cvt(syscall::open(old.to_str().unwrap(),
388+
syscall::O_CLOEXEC | syscall::O_STAT | syscall::O_NOFOLLOW))?;
389+
let res = cvt(syscall::frename(fd, new.to_str().unwrap()));
390+
cvt(syscall::close(fd))?;
391+
res?;
389392
Ok(())
390393
}
391394

@@ -421,18 +424,22 @@ fn remove_dir_all_recursive(path: &Path) -> io::Result<()> {
421424
}
422425

423426
pub fn readlink(p: &Path) -> io::Result<PathBuf> {
424-
let fd = cvt(syscall::open(p.to_str().unwrap(), syscall::O_SYMLINK | syscall::O_RDONLY))?;
427+
let fd = cvt(syscall::open(p.to_str().unwrap(),
428+
syscall::O_CLOEXEC | syscall::O_SYMLINK | syscall::O_RDONLY))?;
425429
let mut buf: [u8; 4096] = [0; 4096];
426-
let count = cvt(syscall::read(fd, &mut buf))?;
430+
let res = cvt(syscall::read(fd, &mut buf));
427431
cvt(syscall::close(fd))?;
432+
let count = res?;
428433
Ok(PathBuf::from(unsafe { String::from_utf8_unchecked(Vec::from(&buf[..count])) }))
429434
}
430435

431436
pub fn symlink(src: &Path, dst: &Path) -> io::Result<()> {
432437
let fd = cvt(syscall::open(dst.to_str().unwrap(),
433-
syscall::O_SYMLINK | syscall::O_CREAT | syscall::O_WRONLY | 0o777))?;
434-
cvt(syscall::write(fd, src.to_str().unwrap().as_bytes()))?;
438+
syscall::O_CLOEXEC | syscall::O_SYMLINK |
439+
syscall::O_CREAT | syscall::O_WRONLY | 0o777))?;
440+
let res = cvt(syscall::write(fd, src.to_str().unwrap().as_bytes()));
435441
cvt(syscall::close(fd))?;
442+
res?;
436443
Ok(())
437444
}
438445

src/libstd/sys/redox/syscall/call.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,19 @@ pub fn exit(status: usize) -> Result<usize> {
9393
unsafe { syscall1(SYS_EXIT, status) }
9494
}
9595

96-
/// Register a file for event-based I/O
96+
/// Change file permissions
97+
pub fn fchmod(fd: usize, mode: u16) -> Result<usize> {
98+
unsafe { syscall2(SYS_FCHMOD, fd, mode as usize) }
99+
100+
}
101+
102+
/// Change file ownership
103+
pub fn fchown(fd: usize, uid: u32, gid: u32) -> Result<usize> {
104+
unsafe { syscall3(SYS_FCHOWN, fd, uid as usize, gid as usize) }
105+
106+
}
107+
108+
/// Change file descriptor flags
97109
pub fn fcntl(fd: usize, cmd: usize, arg: usize) -> Result<usize> {
98110
unsafe { syscall3(SYS_FCNTL, fd, cmd, arg) }
99111
}
@@ -118,6 +130,11 @@ pub fn fpath(fd: usize, buf: &mut [u8]) -> Result<usize> {
118130
unsafe { syscall3(SYS_FPATH, fd, buf.as_mut_ptr() as usize, buf.len()) }
119131
}
120132

133+
/// Rename a file
134+
pub fn frename<T: AsRef<[u8]>>(fd: usize, path: T) -> Result<usize> {
135+
unsafe { syscall3(SYS_FRENAME, fd, path.as_ref().as_ptr() as usize, path.as_ref().len()) }
136+
}
137+
121138
/// Get metadata about a file
122139
pub fn fstat(fd: usize, stat: &mut Stat) -> Result<usize> {
123140
unsafe { syscall3(SYS_FSTAT, fd, stat as *mut Stat as usize, mem::size_of::<Stat>()) }

src/libstd/sys/redox/syscall/number.rs

+3
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,14 @@ pub const SYS_DUP2: usize = SYS_CLASS_FILE | SYS_RET_FILE | 63;
3232
pub const SYS_READ: usize = SYS_CLASS_FILE | SYS_ARG_MSLICE | 3;
3333
pub const SYS_WRITE: usize = SYS_CLASS_FILE | SYS_ARG_SLICE | 4;
3434
pub const SYS_LSEEK: usize = SYS_CLASS_FILE | 19;
35+
pub const SYS_FCHMOD: usize = SYS_CLASS_FILE | 94;
36+
pub const SYS_FCHOWN: usize = SYS_CLASS_FILE | 207;
3537
pub const SYS_FCNTL: usize = SYS_CLASS_FILE | 55;
3638
pub const SYS_FEVENT: usize = SYS_CLASS_FILE | 927;
3739
pub const SYS_FMAP: usize = SYS_CLASS_FILE | 90;
3840
pub const SYS_FUNMAP: usize = SYS_CLASS_FILE | 91;
3941
pub const SYS_FPATH: usize = SYS_CLASS_FILE | SYS_ARG_MSLICE | 928;
42+
pub const SYS_FRENAME: usize = SYS_CLASS_FILE | SYS_ARG_PATH | 38;
4043
pub const SYS_FSTAT: usize = SYS_CLASS_FILE | SYS_ARG_MSLICE | 28;
4144
pub const SYS_FSTATVFS: usize = SYS_CLASS_FILE | SYS_ARG_MSLICE | 100;
4245
pub const SYS_FSYNC: usize = SYS_CLASS_FILE | 118;

0 commit comments

Comments
 (0)