Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 90 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,14 @@ exclude = [ "**/*.conf", "**/*.json",
# We get the workspace's crates from the `path` definitions.

[features]
default = ["authentication", "zwave", "philips_hue", "thinkerbell", "ip_camera", "webpush"]
default = ["authentication", "zwave", "philips_hue", "thinkerbell", "ip_camera", "webpush", "file_storage"]
authentication = []
zwave = ["openzwave-adapter"]
philips_hue = []
thinkerbell = ["foxbox_thinkerbell"]
ip_camera = []
webpush = []
webpush = ["rust-crypto"]
file_storage = ["walkdir", "mime_guess", "rust-crypto", "memmap", "notify"]

[build-dependencies]
pkg-config = "0.3"
Expand All @@ -47,15 +48,18 @@ hyper = "0.9"
lazy_static = "^0.2"
libc = "0.2.7"
log = "0.3"
memmap = { version = "0.5", optional = true }
mime_guess = { version = "1.8", optional = true }
mio = "0.6"
mount = "0.2"
notify = { version = "3.0", optional = true }
nix = "0.7"
openssl = "0.7.6"
openssl-sys = "0.7.6"
pagekite = { git = "https://github.com/fabricedesre/pagekite-rs.git" }
rand = "0.3"
router = "0.4"
rust-crypto = "0.2.34"
rust-crypto = { version = "0.2", optional = true }
rustc-serialize = "0.3"
rusqlite = "0.7"
serde = "0.8"
Expand All @@ -67,6 +71,7 @@ unicase = "1.3.0"
time = "0.1"
timer = "0.1.6"
url = "1.2"
walkdir = { version = "1", optional = true }
ws = { version = "0.5", features = ["ssl"] }

[dependencies.iron]
Expand Down
123 changes: 123 additions & 0 deletions src/adapters/file_storage/metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
// 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 http://mozilla.org/MPL/2.0/.

extern crate crypto;
extern crate memmap;
extern crate mime_guess;

use self::crypto::digest::Digest;
use self::crypto::sha1::Sha1;
use self::memmap::{Mmap, Protection};
use self::mime_guess::guess_mime_type;
use std;
use std::fs::File;
use std::path::{Path, PathBuf};

#[derive(Clone, Debug, PartialEq, Serialize)]
pub enum FileKind {
File,
Directory,
}

#[derive(Clone, Debug, Serialize)]
pub struct FileMetadata {
pub path: PathBuf,
pub mime: String,
pub size: u64,
pub kind: FileKind,
pub hash: String, // TODO: switch to a byte array?
}

// Unused for now, but may allow finer tracking of file changes.
#[allow(dead_code)]
fn get_file_hash(path: &Path) -> Result<String, std::io::Error> {
let mut algo = Sha1::new();
let file = Mmap::open_path(&path, Protection::Read)?;
let bytes: &[u8] = unsafe { file.as_slice() };
algo.reset();
algo.input(bytes);

Ok(algo.result_str())
}

pub fn get_path_hash(path: &Path) -> String {
let mut algo = Sha1::new();
let s = format!("{}", path.display());
algo.reset();
algo.input(s.as_bytes());

algo.result_str()
}

pub fn get_file_content(path: &Path) -> Result<memmap::MmapView, std::io::Error> {
let file = Mmap::open_path(&path, Protection::Read)?;

Ok(file.into_view())
}

pub fn get_file_metadata(path: &Path) -> Result<FileMetadata, std::io::Error> {
let file = File::open(&path)?;
let fmeta = file.metadata()?;

let kind = if fmeta.is_dir() {
FileKind::Directory
} else {
FileKind::File
};

let hash = get_path_hash(path);

let mut mpath = PathBuf::new();
mpath.push(path);

Ok(FileMetadata {
path: mpath,
mime: format!("{}", guess_mime_type(path)),
size: fmeta.len(),
kind: kind,
hash: hash,
})
}

#[test]
fn test_unknown_file_hash() {
let hash = get_file_hash(Path::new("./unknown.file"));
assert!(hash.is_err());
}

#[test]
fn test_valid_file_hash() {
let hash = get_file_hash(Path::new("./package.json")).unwrap();
assert_eq!(hash, "103bfbff447830258032deedfc53d73ec87543c4");
}

#[test]
fn test_unknown_file_metadata() {
let meta = get_file_metadata(Path::new("./unknown.file"));
assert!(meta.is_err());
}

#[test]
fn test_valid_file_metadata() {
let path = Path::new("./package.json");
let meta = get_file_metadata(path).unwrap();
assert_eq!(meta.path, path);
assert_eq!(meta.mime, "application/json");
assert_eq!(meta.size, 1557);
assert_eq!(meta.kind, FileKind::File);
assert_eq!(meta.hash,
"9d106f6bdf655116e339da03a9b5609276c92191".to_owned());
}

#[test]
fn test_valid_directory_metadata() {
let path = Path::new("./src");
let meta = get_file_metadata(path).unwrap();
assert_eq!(meta.path, path);
assert_eq!(meta.mime, "application/octet-stream");
assert_eq!(meta.size, 4096);
assert_eq!(meta.kind, FileKind::Directory);
assert_eq!(meta.hash,
"07901fccf6a039e8ea7ac1a1fecb3a710125e149".to_owned());
}
Loading