Skip to content

Commit c227f28

Browse files
jyn514Joshua Nelson
authored and
Joshua Nelson
committed
Remove magic dependency on Linux
This uses the Rust tree_magic package instead. Most work was already done in #600. This just sets tree_magic to build unconditionally instead of always on Windows. This improves the memory usage of docs.rs by over a factor of 5 when uploading files in some cases. This can be tested locally like so: Run minio in the background: ``` docker run -p 9000:9000 -e MINIO_ACCESS_KEY=password -e MINIO_SECRET_KEY=password minio/minio server /data ``` Navigate to localhost:9000/ and set both the access key and secret key to 'password'. Add a new bucket called `rust-docs-rs`. Now in another terminal, run docs.rs. This assumes you already have the CRATESFYI_DATABASE_URL set up locally as described in [developing without docker-compose](https://forge.rust-lang.org/docs-rs/no-docker-compose.html): ``` # set up a fairly large file mkdir -p ignored/tmp yes | head -n $((1024 * 1024 * 50)) > ignored/tmp/100MB git checkout master cargo build --release RUST_LOG=cratesfyi,info S3_ENDPOINT=http://localhost:9000 AWS_ACCESS_KEY_ID=password AWS_SECRET_ACCESS_KEY=password valgrind --tool=massif --massif-out-file=libmagic.massif.out target/release/cratesfyi database add-directory ignored/tmp/ # this will show ~1.5 GB used, depending on your system ms_print libmagic.massif.out | less # now try without libmagic git checkout druid cargo build --release RUST_LOG=cratesfyi,info S3_ENDPOINT=http://localhost:9000 AWS_ACCESS_KEY_ID=password AWS_SECRET_ACCESS_KEY=password valgrind --tool=massif --massif-out-file=no_libmagic.massif.out target/release/cratesfyi database add-directory ignored/tmp/ # this will show ~250 MB used, depending on your system ms_print no_libmagic.massif.out | less ```
1 parent 794c8db commit c227f28

File tree

4 files changed

+5
-61
lines changed

4 files changed

+5
-61
lines changed

Cargo.lock

-27
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ prometheus = { version = "0.7.0", default-features = false }
3838
lazy_static = "1.0.0"
3939
rustwide = "0.5.0"
4040
tempdir = "0.3"
41+
mime_guess = "2"
4142

4243
# iron dependencies
4344
iron = "0.5"
@@ -48,11 +49,9 @@ staticfile = { version = "0.4", features = [ "cache" ] }
4849

4950
[target.'cfg(not(windows))'.dependencies]
5051
libc = "0.2"
51-
magic = "0.12"
5252

5353
[target.'cfg(windows)'.dependencies]
5454
path-slash = "0.1.1"
55-
mime_guess = "2.0.1"
5655

5756
[dependencies.postgres]
5857
version = "0.15"

src/db/file.rs

+3-28
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ use rusoto_s3::{S3, PutObjectRequest, GetObjectRequest, S3Client};
1717
use rusoto_core::region::Region;
1818
use rusoto_credential::DefaultCredentialsProvider;
1919
use std::ffi::OsStr;
20-
#[cfg(not(windows))]
21-
use magic::{Cookie, flags};
2220

2321
const MAX_CONCURRENT_UPLOADS: usize = 1000;
2422

@@ -157,13 +155,12 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
157155
prefix: &str,
158156
path: P)
159157
-> Result<Json> {
160-
let trans = conn.transaction()?;
161-
#[cfg(not(windows))]
162-
let mime_data = load_mime_data()?;
163158
use std::collections::HashMap;
164-
let mut file_paths_and_mimes: HashMap<PathBuf, String> = HashMap::new();
165159
use futures::future::Future;
166160

161+
let trans = conn.transaction()?;
162+
let mut file_paths_and_mimes: HashMap<PathBuf, String> = HashMap::new();
163+
167164
let mut rt = ::tokio::runtime::Runtime::new().unwrap();
168165

169166
let mut to_upload = get_file_list(&path)?;
@@ -194,10 +191,7 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
194191
#[cfg(not(windows))]
195192
let bucket_path = bucket_path.into_os_string().into_string().unwrap();
196193

197-
#[cfg(windows)]
198194
let mime = detect_mime(&content, &file_path)?;
199-
#[cfg(not(windows))]
200-
let mime = detect_mime(&content, &file_path, &mime_data)?;
201195

202196
if let Some(client) = &client {
203197
futures.push(client.put_object(PutObjectRequest {
@@ -260,20 +254,6 @@ pub fn add_path_into_database<P: AsRef<Path>>(conn: &Connection,
260254
file_list_to_json(file_list_with_mimes)
261255
}
262256

263-
#[cfg(not(windows))]
264-
fn load_mime_data() -> Result<Cookie> {
265-
let cookie = Cookie::open(flags::MIME_TYPE)?;
266-
cookie.load::<&str>(&[])?;
267-
Ok(cookie)
268-
}
269-
270-
#[cfg(not(windows))]
271-
fn detect_mime(content: &Vec<u8>, file_path: &Path, cookie: &Cookie) -> Result<String> {
272-
let mime = cookie.buffer(&content)?;
273-
correct_mime(&mime, &file_path)
274-
}
275-
276-
#[cfg(windows)]
277257
fn detect_mime(_content: &Vec<u8>, file_path: &Path) -> Result<String> {
278258
let mime = mime_guess::from_path(file_path).first_raw().map(|m| m).unwrap_or("text/plain");
279259
correct_mime(&mime, &file_path)
@@ -393,12 +373,7 @@ mod test {
393373
}
394374

395375
fn check_mime(content: &str, path: &str, expected_mime: &str) {
396-
#[cfg(not(windows))]
397-
let mime_data = load_mime_data().unwrap();
398-
#[cfg(windows)]
399376
let detected_mime = detect_mime(&content.as_bytes().to_vec(), Path::new(&path));
400-
#[cfg(not(windows))]
401-
let detected_mime = detect_mime(&content.as_bytes().to_vec(), Path::new(&path), &mime_data);
402377
let detected_mime = detected_mime.expect("no mime was given");
403378
assert_eq!(detected_mime, expected_mime);
404379
}

src/lib.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,13 @@ extern crate tokio;
3939
extern crate systemstat;
4040
extern crate rustwide;
4141
extern crate tempdir;
42+
extern crate mime_guess;
4243

43-
#[cfg(not(windows))]
44-
extern crate magic;
4544
#[cfg(not(windows))]
4645
extern crate libc;
4746

4847
#[cfg(windows)]
4948
extern crate path_slash;
50-
#[cfg(windows)]
51-
extern crate mime_guess;
5249

5350
#[cfg(test)]
5451
extern crate once_cell;

0 commit comments

Comments
 (0)