From c7aba8fdfdd88a5849f48900960d27bb7ca9f249 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Sat, 13 Feb 2021 12:24:19 -0700
Subject: [PATCH] Switch Context::cwd to using RwLock

---
 src/context/context.rs | 6 +++---
 src/syscall/fs.rs      | 4 ++--
 src/syscall/process.rs | 2 +-
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/src/context/context.rs b/src/context/context.rs
index f2da71ab..2068d399 100644
--- a/src/context/context.rs
+++ b/src/context/context.rs
@@ -237,7 +237,7 @@ pub struct Context {
     /// The name of the context
     pub name: Arc<RwLock<Box<str>>>,
     /// The current working directory
-    pub cwd: Arc<Mutex<Vec<u8>>>,
+    pub cwd: Arc<RwLock<Vec<u8>>>,
     /// The open files in the scheme
     pub files: Arc<Mutex<Vec<Option<FileDescriptor>>>>,
     /// Signal actions
@@ -293,7 +293,7 @@ impl Context {
             tls: None,
             grants: Arc::new(Mutex::new(UserGrants::default())),
             name: Arc::new(RwLock::new(String::new().into_boxed_str())),
-            cwd: Arc::new(Mutex::new(Vec::new())),
+            cwd: Arc::new(RwLock::new(Vec::new())),
             files: Arc::new(Mutex::new(Vec::new())),
             actions: Arc::new(Mutex::new(vec![(
                 SigAction {
@@ -315,7 +315,7 @@ impl Context {
     /// "bar:/foo" will be used directly, as it is already absolute
     pub fn canonicalize(&self, path: &[u8]) -> Vec<u8> {
         let mut canon = if path.iter().position(|&b| b == b':').is_none() {
-            let cwd = self.cwd.lock();
+            let cwd = self.cwd.read();
 
             let mut canon = if !path.starts_with(b"/") {
                 let mut c = cwd.clone();
diff --git a/src/syscall/fs.rs b/src/syscall/fs.rs
index 197442df..780f869c 100644
--- a/src/syscall/fs.rs
+++ b/src/syscall/fs.rs
@@ -66,7 +66,7 @@ pub fn chdir(path: &[u8]) -> Result<usize> {
         let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
         let context = context_lock.read();
         let canonical = context.canonicalize(path);
-        *context.cwd.lock() = canonical;
+        *context.cwd.write() = canonical;
         Ok(0)
     } else {
         Err(Error::new(ENOTDIR))
@@ -78,7 +78,7 @@ pub fn getcwd(buf: &mut [u8]) -> Result<usize> {
     let contexts = context::contexts();
     let context_lock = contexts.current().ok_or(Error::new(ESRCH))?;
     let context = context_lock.read();
-    let cwd = context.cwd.lock();
+    let cwd = context.cwd.read();
     let mut i = 0;
     while i < buf.len() && i < cwd.len() {
         buf[i] = cwd[i];
diff --git a/src/syscall/process.rs b/src/syscall/process.rs
index 6b15779a..4f0cbacf 100644
--- a/src/syscall/process.rs
+++ b/src/syscall/process.rs
@@ -253,7 +253,7 @@ pub fn clone(flags: CloneFlags, stack_base: usize) -> Result<ContextId> {
             if flags.contains(CLONE_FS) {
                 cwd = Arc::clone(&context.cwd);
             } else {
-                cwd = Arc::new(Mutex::new(context.cwd.lock().clone()));
+                cwd = Arc::new(RwLock::new(context.cwd.read().clone()));
             }
 
             if flags.contains(CLONE_FILES) {
-- 
GitLab