/* This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at https://mozilla.org/MPL/2.0/. */ use std::path::PathBuf; use std::sync::Mutex; use std::{env, fs}; use servo::resources::{self, Resource}; static CMD_RESOURCE_DIR: Mutex> = Mutex::new(None); struct ResourceReader; pub fn init() { resources::set(Box::new(ResourceReader)); } pub(crate) fn resources_dir_path() -> PathBuf { // This needs to be called before the process is sandboxed // as we only give permission to read inside the resources directory, // not the permissions the "search" for the resources directory. let mut dir = CMD_RESOURCE_DIR.lock().unwrap(); if let Some(ref path) = *dir { return PathBuf::from(path); } // Try ./resources and ./Resources relative to the directory containing the // canonicalised executable path, then each of its ancestors. let mut path = env::current_exe().unwrap().canonicalize().unwrap(); while path.pop() { path.push("resources"); if path.is_dir() { *dir = Some(path); return dir.clone().unwrap(); } path.pop(); // Check for Resources on mac when using a case sensitive filesystem. path.push("Resources"); if path.is_dir() { *dir = Some(path); return dir.clone().unwrap(); } path.pop(); } panic!("Could not find resources directory"); } impl resources::ResourceReaderMethods for ResourceReader { fn read(&self, file: Resource) -> Vec { let mut path = resources_dir_path(); path.push(file.filename()); fs::read(path).expect("Can't read file") } fn sandbox_access_files_dirs(&self) -> Vec { vec![resources_dir_path()] } fn sandbox_access_files(&self) -> Vec { vec![] } }