From 8ba97b8cff3d8a2443e9bb299a5caca2be62adee Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Sun, 18 Sep 2016 12:54:10 -0600
Subject: [PATCH] Canonicalize paths in open

---
 context/context.rs |  4 ++--
 syscall/fs.rs      | 13 ++++++++++---
 2 files changed, 12 insertions(+), 5 deletions(-)

diff --git a/context/context.rs b/context/context.rs
index 43bdc61c..4b4cc3a7 100644
--- a/context/context.rs
+++ b/context/context.rs
@@ -93,7 +93,7 @@ impl Context {
 
     /// Add a file to the lowest available slot.
     /// Return the file descriptor number or None if no slot was found
-    pub fn add_file(&mut self, file: File) -> Option<usize> {
+    pub fn add_file(&self, file: File) -> Option<usize> {
         let mut files = self.files.lock();
         for (i, mut file_option) in files.iter_mut().enumerate() {
             if file_option.is_none() {
@@ -122,7 +122,7 @@ impl Context {
 
     /// Remove a file
     // TODO: adjust files vector to smaller size if possible
-    pub fn remove_file(&mut self, i: usize) -> Option<File> {
+    pub fn remove_file(&self, i: usize) -> Option<File> {
         let mut files = self.files.lock();
         if i < files.len() {
             files[i].take()
diff --git a/syscall/fs.rs b/syscall/fs.rs
index acf457e2..c992bbaa 100644
--- a/syscall/fs.rs
+++ b/syscall/fs.rs
@@ -61,7 +61,14 @@ pub fn write(fd: usize, buf: &[u8]) -> Result<usize> {
 
 /// Open syscall
 pub fn open(path: &[u8], flags: usize) -> Result<usize> {
-    let mut parts = path.splitn(2, |&b| b == b':');
+    let path_canon = {
+        let contexts = context::contexts();
+        let context_lock = contexts.current().ok_or(Error::NoProcess)?;
+        let context = context_lock.read();
+        context.canonicalize(path)
+    };
+
+    let mut parts = path_canon.splitn(2, |&b| b == b':');
     let namespace_opt = parts.next();
     let reference_opt = parts.next();
 
@@ -75,7 +82,7 @@ pub fn open(path: &[u8], flags: usize) -> Result<usize> {
 
     let contexts = context::contexts();
     let context_lock = contexts.current().ok_or(Error::NoProcess)?;
-    let mut context = context_lock.write();
+    let context = context_lock.read();
     context.add_file(::context::file::File {
         scheme: scheme_id,
         number: file_id
@@ -87,7 +94,7 @@ pub fn close(fd: usize) -> Result<usize> {
     let file = {
         let contexts = context::contexts();
         let context_lock = contexts.current().ok_or(Error::NoProcess)?;
-        let mut context = context_lock.write();
+        let context = context_lock.read();
         let file = context.remove_file(fd).ok_or(Error::BadFile)?;
         file
     };
-- 
GitLab