servoshell/
resources.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use std::path::PathBuf;
6use std::sync::Mutex;
7use std::{env, fs};
8
9use cfg_if::cfg_if;
10use servo::resources::{self, Resource};
11
12static CMD_RESOURCE_DIR: Mutex<Option<PathBuf>> = Mutex::new(None);
13
14struct ResourceReader;
15
16pub fn init() {
17    resources::set(Box::new(ResourceReader));
18}
19
20pub(crate) fn resources_dir_path() -> PathBuf {
21    // This needs to be called before the process is sandboxed
22    // as we only give permission to read inside the resources directory,
23    // not the permissions the "search" for the resources directory.
24    let mut dir = CMD_RESOURCE_DIR.lock().unwrap();
25    if let Some(ref path) = *dir {
26        return PathBuf::from(path);
27    }
28
29    // Try ./resources and ./Resources relative to the directory containing the
30    // canonicalised executable path, then each of its ancestors.
31    let mut path = env::current_exe().unwrap().canonicalize().unwrap();
32    while path.pop() {
33        path.push("resources");
34        if path.is_dir() {
35            *dir = Some(path);
36            return dir.clone().unwrap();
37        }
38        path.pop();
39
40        // Check for Resources on mac when using a case sensitive filesystem.
41        path.push("Resources");
42        if path.is_dir() {
43            *dir = Some(path);
44            return dir.clone().unwrap();
45        }
46        path.pop();
47    }
48
49    cfg_if! {
50        if #[cfg(servo_production)] {
51            panic!("Can't find resources directory")
52        } else {
53            // Static assert that this is really a non-production build, rather
54            // than a failure of the build script’s production check.
55            const _: () = assert!(cfg!(servo_do_not_use_in_production));
56
57            // Try ./resources in the current directory, then each of its ancestors.
58            // Not to be used in production builds without considering the security implications!
59            let mut path = std::env::current_dir().unwrap();
60            loop {
61                path.push("resources");
62                if path.is_dir() {
63                    *dir = Some(path);
64                    return dir.clone().unwrap();
65                }
66                path.pop();
67
68                if !path.pop() {
69                    panic!("Can't find resources directory")
70                }
71            }
72        }
73    }
74}
75
76impl resources::ResourceReaderMethods for ResourceReader {
77    fn read(&self, file: Resource) -> Vec<u8> {
78        let mut path = resources_dir_path();
79        path.push(file.filename());
80        fs::read(path).expect("Can't read file")
81    }
82    fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> {
83        vec![resources_dir_path()]
84    }
85    fn sandbox_access_files(&self) -> Vec<PathBuf> {
86        vec![]
87    }
88}