blob: e8be42ca3868152021d5c7a1f487535457e18319 [file] [log] [blame]
Jiyong Park331d1ea2021-05-10 11:01:23 +09001/*
2 * Copyright (C) 2021 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17//! `zipfuse` is a FUSE filesystem for zip archives. It provides transparent access to the files
18//! in a zip archive. This filesystem does not supporting writing files back to the zip archive.
19//! The filesystem has to be mounted read only.
20
21mod inode;
22
Alan Stokes60f82202022-10-07 16:40:07 +010023use anyhow::{Context as AnyhowContext, Result};
Andrew Walbranaa1efc42022-08-10 13:33:57 +000024use clap::{builder::ValueParser, Arg, ArgAction, Command};
Jiyong Park331d1ea2021-05-10 11:01:23 +090025use fuse::filesystem::*;
26use fuse::mount::*;
Alan Stokes60f82202022-10-07 16:40:07 +010027use rustutils::system_properties;
Jiyong Park331d1ea2021-05-10 11:01:23 +090028use std::collections::HashMap;
29use std::convert::TryFrom;
Jiyong Park851f68a2021-05-11 21:41:25 +090030use std::ffi::{CStr, CString};
Jiyong Park331d1ea2021-05-10 11:01:23 +090031use std::fs::{File, OpenOptions};
32use std::io;
33use std::io::Read;
Andrew Walbranae3350d2023-07-21 19:01:18 +010034use std::mem::{size_of, MaybeUninit};
Jiyong Park331d1ea2021-05-10 11:01:23 +090035use std::os::unix::io::AsRawFd;
36use std::path::Path;
Andrew Walbranaa1efc42022-08-10 13:33:57 +000037use std::path::PathBuf;
Jiyong Park331d1ea2021-05-10 11:01:23 +090038use std::sync::Mutex;
39
40use crate::inode::{DirectoryEntry, Inode, InodeData, InodeKind, InodeTable};
41
42fn main() -> Result<()> {
Andrew Walbranaa1efc42022-08-10 13:33:57 +000043 let matches = clap_command().get_matches();
44
45 let zip_file = matches.get_one::<PathBuf>("ZIPFILE").unwrap();
46 let mount_point = matches.get_one::<PathBuf>("MOUNTPOINT").unwrap();
47 let options = matches.get_one::<String>("options");
48 let noexec = matches.get_flag("noexec");
49 let ready_prop = matches.get_one::<String>("readyprop");
50 let uid: u32 = matches.get_one::<String>("uid").map_or(0, |s| s.parse().unwrap());
51 let gid: u32 = matches.get_one::<String>("gid").map_or(0, |s| s.parse().unwrap());
52 run_fuse(zip_file, mount_point, options, noexec, ready_prop, uid, gid)?;
53
54 Ok(())
55}
56
57fn clap_command() -> Command {
58 Command::new("zipfuse")
Jiyong Park6a762db2021-05-31 14:00:52 +090059 .arg(
Andrew Walbranaa1efc42022-08-10 13:33:57 +000060 Arg::new("options")
Jeff Vander Stoepa8dc2712022-07-29 02:33:45 +020061 .short('o')
Jiyong Park6a762db2021-05-31 14:00:52 +090062 .required(false)
Chris Wailes68c39f82021-07-27 16:03:44 -070063 .help("Comma separated list of mount options"),
Jiyong Park6a762db2021-05-31 14:00:52 +090064 )
Andrew Scull3854e402022-07-04 12:10:20 +000065 .arg(
Andrew Walbranaa1efc42022-08-10 13:33:57 +000066 Arg::new("noexec")
Andrew Scull3854e402022-07-04 12:10:20 +000067 .long("noexec")
Andrew Walbranaa1efc42022-08-10 13:33:57 +000068 .action(ArgAction::SetTrue)
Andrew Scull3854e402022-07-04 12:10:20 +000069 .help("Disallow the execution of binary files"),
70 )
Alan Stokes60f82202022-10-07 16:40:07 +010071 .arg(
Andrew Walbranaa1efc42022-08-10 13:33:57 +000072 Arg::new("readyprop")
Alan Stokes60f82202022-10-07 16:40:07 +010073 .short('p')
Alan Stokes60f82202022-10-07 16:40:07 +010074 .help("Specify a property to be set when mount is ready"),
75 )
Andrew Walbranaa1efc42022-08-10 13:33:57 +000076 .arg(Arg::new("uid").short('u').help("numeric UID who's the owner of the files"))
77 .arg(Arg::new("gid").short('g').help("numeric GID who's the group of the files"))
78 .arg(Arg::new("ZIPFILE").value_parser(ValueParser::path_buf()).required(true))
79 .arg(Arg::new("MOUNTPOINT").value_parser(ValueParser::path_buf()).required(true))
Jiyong Park331d1ea2021-05-10 11:01:23 +090080}
81
82/// Runs a fuse filesystem by mounting `zip_file` on `mount_point`.
Andrew Scull3854e402022-07-04 12:10:20 +000083pub fn run_fuse(
84 zip_file: &Path,
85 mount_point: &Path,
Andrew Walbranaa1efc42022-08-10 13:33:57 +000086 extra_options: Option<&String>,
Andrew Scull3854e402022-07-04 12:10:20 +000087 noexec: bool,
Andrew Walbranaa1efc42022-08-10 13:33:57 +000088 ready_prop: Option<&String>,
Jiyong Park231f9692023-01-09 19:36:21 +090089 uid: u32,
90 gid: u32,
Andrew Scull3854e402022-07-04 12:10:20 +000091) -> Result<()> {
Jiyong Park331d1ea2021-05-10 11:01:23 +090092 const MAX_READ: u32 = 1 << 20; // TODO(jiyong): tune this
93 const MAX_WRITE: u32 = 1 << 13; // This is a read-only filesystem
94
95 let dev_fuse = OpenOptions::new().read(true).write(true).open("/dev/fuse")?;
96
Jiyong Park6a762db2021-05-31 14:00:52 +090097 let mut mount_options = vec![
98 MountOption::FD(dev_fuse.as_raw_fd()),
Jiyong Park98ce68d2023-01-09 17:46:19 +090099 MountOption::DefaultPermissions,
Jiyong Park6a762db2021-05-31 14:00:52 +0900100 MountOption::RootMode(libc::S_IFDIR | libc::S_IXUSR | libc::S_IXGRP | libc::S_IXOTH),
101 MountOption::AllowOther,
102 MountOption::UserId(0),
103 MountOption::GroupId(0),
104 MountOption::MaxRead(MAX_READ),
105 ];
106 if let Some(value) = extra_options {
107 mount_options.push(MountOption::Extra(value));
108 }
109
Andrew Scull3854e402022-07-04 12:10:20 +0000110 let mut mount_flags = libc::MS_NOSUID | libc::MS_NODEV | libc::MS_RDONLY;
111 if noexec {
112 mount_flags |= libc::MS_NOEXEC;
113 }
114
115 fuse::mount(mount_point, "zipfuse", mount_flags, &mount_options)?;
Alan Stokes60f82202022-10-07 16:40:07 +0100116
117 if let Some(property_name) = ready_prop {
118 system_properties::write(property_name, "1").context("Failed to set readyprop")?;
119 }
120
Victor Hsieh58a5e9b2022-03-09 21:57:26 +0000121 let mut config = fuse::FuseConfig::new();
122 config.dev_fuse(dev_fuse).max_write(MAX_WRITE).max_read(MAX_READ);
Jiyong Park231f9692023-01-09 19:36:21 +0900123 Ok(config.enter_message_loop(ZipFuse::new(zip_file, uid, gid)?)?)
Jiyong Park331d1ea2021-05-10 11:01:23 +0900124}
125
126struct ZipFuse {
127 zip_archive: Mutex<zip::ZipArchive<File>>,
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900128 raw_file: Mutex<File>,
Jiyong Park331d1ea2021-05-10 11:01:23 +0900129 inode_table: InodeTable,
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900130 open_files: Mutex<HashMap<Handle, OpenFile>>,
Jiyong Park331d1ea2021-05-10 11:01:23 +0900131 open_dirs: Mutex<HashMap<Handle, OpenDirBuf>>,
Jiyong Park231f9692023-01-09 19:36:21 +0900132 uid: u32,
133 gid: u32,
Jiyong Park331d1ea2021-05-10 11:01:23 +0900134}
135
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900136/// Represents a [`ZipFile`] that is opened.
137struct OpenFile {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900138 open_count: u32, // multiple opens share the buf because this is a read-only filesystem
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900139 content: OpenFileContent,
140}
141
142/// Holds the content of a [`ZipFile`]. Depending on whether it is compressed or not, the
143/// entire content is stored, or only the zip index is stored.
144enum OpenFileContent {
145 Compressed(Box<[u8]>),
146 Uncompressed(usize), // zip index
Jiyong Park331d1ea2021-05-10 11:01:23 +0900147}
148
149/// Holds the directory entries in a directory opened by [`opendir`].
150struct OpenDirBuf {
151 open_count: u32,
152 buf: Box<[(CString, DirectoryEntry)]>,
153}
154
155type Handle = u64;
156
157fn ebadf() -> io::Error {
158 io::Error::from_raw_os_error(libc::EBADF)
159}
160
161fn timeout_max() -> std::time::Duration {
162 std::time::Duration::new(u64::MAX, 1_000_000_000 - 1)
163}
164
165impl ZipFuse {
Jiyong Park231f9692023-01-09 19:36:21 +0900166 fn new(zip_file: &Path, uid: u32, gid: u32) -> Result<ZipFuse> {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900167 // TODO(jiyong): Use O_DIRECT to avoid double caching.
168 // `.custom_flags(nix::fcntl::OFlag::O_DIRECT.bits())` currently doesn't work.
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900169 let f = File::open(zip_file)?;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900170 let mut z = zip::ZipArchive::new(f)?;
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900171 // Open the same file again so that we can directly access it when accessing
172 // uncompressed zip_file entries in it. `ZipFile` doesn't implement `Seek`.
173 let raw_file = File::open(zip_file)?;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900174 let it = InodeTable::from_zip(&mut z)?;
175 Ok(ZipFuse {
176 zip_archive: Mutex::new(z),
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900177 raw_file: Mutex::new(raw_file),
Jiyong Park331d1ea2021-05-10 11:01:23 +0900178 inode_table: it,
179 open_files: Mutex::new(HashMap::new()),
180 open_dirs: Mutex::new(HashMap::new()),
Jiyong Park231f9692023-01-09 19:36:21 +0900181 uid,
182 gid,
Jiyong Park331d1ea2021-05-10 11:01:23 +0900183 })
184 }
185
186 fn find_inode(&self, inode: Inode) -> io::Result<&InodeData> {
187 self.inode_table.get(inode).ok_or_else(ebadf)
188 }
189
Jiyong Parkd5df9562021-05-13 00:50:23 +0900190 // TODO(jiyong) remove this. Right now this is needed to do the nlink_t to u64 conversion below
191 // on aosp_x86_64 target. That however is a useless conversion on other targets.
192 #[allow(clippy::useless_conversion)]
Jiyong Park331d1ea2021-05-10 11:01:23 +0900193 fn stat_from(&self, inode: Inode) -> io::Result<libc::stat64> {
194 let inode_data = self.find_inode(inode)?;
Andrew Walbranae3350d2023-07-21 19:01:18 +0100195 // SAFETY: All fields of stat64 are valid for zero byte patterns.
196 let mut st = unsafe { MaybeUninit::<libc::stat64>::zeroed().assume_init() };
Jiyong Park331d1ea2021-05-10 11:01:23 +0900197 st.st_dev = 0;
Jiyong Parkd5df9562021-05-13 00:50:23 +0900198 st.st_nlink = if let Some(directory) = inode_data.get_directory() {
199 (2 + directory.len() as libc::nlink_t).into()
Jiyong Park331d1ea2021-05-10 11:01:23 +0900200 } else {
201 1
202 };
203 st.st_ino = inode;
204 st.st_mode = if inode_data.is_dir() { libc::S_IFDIR } else { libc::S_IFREG };
205 st.st_mode |= inode_data.mode;
Jiyong Park231f9692023-01-09 19:36:21 +0900206 st.st_uid = self.uid;
207 st.st_gid = self.gid;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900208 st.st_size = i64::try_from(inode_data.size).unwrap_or(i64::MAX);
209 Ok(st)
210 }
211}
212
213impl fuse::filesystem::FileSystem for ZipFuse {
214 type Inode = Inode;
215 type Handle = Handle;
216 type DirIter = DirIter;
217
218 fn init(&self, _capable: FsOptions) -> std::io::Result<FsOptions> {
219 // The default options added by the fuse crate are fine. We don't have additional options.
220 Ok(FsOptions::empty())
221 }
222
223 fn lookup(&self, _ctx: Context, parent: Self::Inode, name: &CStr) -> io::Result<Entry> {
224 let inode = self.find_inode(parent)?;
225 let directory = inode.get_directory().ok_or_else(ebadf)?;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900226 let entry = directory.get(name);
227 match entry {
228 Some(e) => Ok(Entry {
229 inode: e.inode,
230 generation: 0,
231 attr: self.stat_from(e.inode)?,
232 attr_timeout: timeout_max(), // this is a read-only fs
233 entry_timeout: timeout_max(),
234 }),
235 _ => Err(io::Error::from_raw_os_error(libc::ENOENT)),
236 }
237 }
238
239 fn getattr(
240 &self,
241 _ctx: Context,
242 inode: Self::Inode,
243 _handle: Option<Self::Handle>,
244 ) -> io::Result<(libc::stat64, std::time::Duration)> {
245 let st = self.stat_from(inode)?;
246 Ok((st, timeout_max()))
247 }
248
249 fn open(
250 &self,
251 _ctx: Context,
252 inode: Self::Inode,
253 _flags: u32,
254 ) -> io::Result<(Option<Self::Handle>, fuse::filesystem::OpenOptions)> {
255 let mut open_files = self.open_files.lock().unwrap();
256 let handle = inode as Handle;
257
258 // If the file is already opened, just increase the reference counter. If not, read the
259 // entire file content to the buffer. When `read` is called, a portion of the buffer is
260 // copied to the kernel.
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900261 if let Some(file) = open_files.get_mut(&handle) {
262 if file.open_count == 0 {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900263 return Err(ebadf());
264 }
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900265 file.open_count += 1;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900266 } else {
267 let inode_data = self.find_inode(inode)?;
268 let zip_index = inode_data.get_zip_index().ok_or_else(ebadf)?;
269 let mut zip_archive = self.zip_archive.lock().unwrap();
270 let mut zip_file = zip_archive.by_index(zip_index)?;
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900271 let content = match zip_file.compression() {
272 zip::CompressionMethod::Stored => OpenFileContent::Uncompressed(zip_index),
273 _ => {
274 if let Some(mode) = zip_file.unix_mode() {
275 let is_reg_file = zip_file.is_file();
276 let is_executable =
277 mode & (libc::S_IXUSR | libc::S_IXGRP | libc::S_IXOTH) != 0;
278 if is_reg_file && is_executable {
279 log::warn!(
280 "Executable file {:?} is stored compressed. Consider \
281 storing it uncompressed to save memory",
282 zip_file.mangled_name()
283 );
284 }
285 }
286 let mut buf = Vec::with_capacity(inode_data.size as usize);
287 zip_file.read_to_end(&mut buf)?;
288 OpenFileContent::Compressed(buf.into_boxed_slice())
289 }
290 };
291 open_files.insert(handle, OpenFile { open_count: 1, content });
Jiyong Park331d1ea2021-05-10 11:01:23 +0900292 }
293 // Note: we don't return `DIRECT_IO` here, because then applications wouldn't be able to
294 // mmap the files.
295 Ok((Some(handle), fuse::filesystem::OpenOptions::empty()))
296 }
297
298 fn release(
299 &self,
300 _ctx: Context,
301 inode: Self::Inode,
302 _flags: u32,
303 _handle: Self::Handle,
304 _flush: bool,
305 _flock_release: bool,
306 _lock_owner: Option<u64>,
307 ) -> io::Result<()> {
308 // Releases the buffer for the `handle` when it is opened for nobody. While this is good
309 // for saving memory, this has a performance implication because we need to decompress
310 // again when the same file is opened in the future.
311 let mut open_files = self.open_files.lock().unwrap();
312 let handle = inode as Handle;
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900313 if let Some(file) = open_files.get_mut(&handle) {
314 if file.open_count.checked_sub(1).ok_or_else(ebadf)? == 0 {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900315 open_files.remove(&handle);
316 }
317 Ok(())
318 } else {
319 Err(ebadf())
320 }
321 }
322
323 fn read<W: io::Write + ZeroCopyWriter>(
324 &self,
325 _ctx: Context,
326 _inode: Self::Inode,
327 handle: Self::Handle,
328 mut w: W,
329 size: u32,
330 offset: u64,
331 _lock_owner: Option<u64>,
332 _flags: u32,
333 ) -> io::Result<usize> {
334 let open_files = self.open_files.lock().unwrap();
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900335 let file = open_files.get(&handle).ok_or_else(ebadf)?;
336 if file.open_count == 0 {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900337 return Err(ebadf());
338 }
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900339 Ok(match &file.content {
340 OpenFileContent::Uncompressed(zip_index) => {
341 let mut zip_archive = self.zip_archive.lock().unwrap();
342 let zip_file = zip_archive.by_index(*zip_index)?;
343 let start = zip_file.data_start() + offset;
344 let remaining_size = zip_file.size() - offset;
345 let size = std::cmp::min(remaining_size, size.into());
346
347 let mut raw_file = self.raw_file.lock().unwrap();
348 w.write_from(&mut raw_file, size as usize, start)?
349 }
350 OpenFileContent::Compressed(buf) => {
351 let start = offset as usize;
352 let end = start + size as usize;
353 let end = std::cmp::min(end, buf.len());
354 w.write(&buf[start..end])?
355 }
356 })
Jiyong Park331d1ea2021-05-10 11:01:23 +0900357 }
358
359 fn opendir(
360 &self,
361 _ctx: Context,
362 inode: Self::Inode,
363 _flags: u32,
364 ) -> io::Result<(Option<Self::Handle>, fuse::filesystem::OpenOptions)> {
365 let mut open_dirs = self.open_dirs.lock().unwrap();
366 let handle = inode as Handle;
367 if let Some(odb) = open_dirs.get_mut(&handle) {
368 if odb.open_count == 0 {
369 return Err(ebadf());
370 }
371 odb.open_count += 1;
372 } else {
373 let inode_data = self.find_inode(inode)?;
374 let directory = inode_data.get_directory().ok_or_else(ebadf)?;
375 let mut buf: Vec<(CString, DirectoryEntry)> = Vec::with_capacity(directory.len());
376 for (name, dir_entry) in directory.iter() {
377 let name = CString::new(name.as_bytes()).unwrap();
378 buf.push((name, dir_entry.clone()));
379 }
380 open_dirs.insert(handle, OpenDirBuf { open_count: 1, buf: buf.into_boxed_slice() });
381 }
Jiyong Park63a95cf2021-05-13 19:20:30 +0900382 Ok((Some(handle), fuse::filesystem::OpenOptions::CACHE_DIR))
Jiyong Park331d1ea2021-05-10 11:01:23 +0900383 }
384
385 fn releasedir(
386 &self,
387 _ctx: Context,
388 inode: Self::Inode,
389 _flags: u32,
390 _handle: Self::Handle,
391 ) -> io::Result<()> {
392 let mut open_dirs = self.open_dirs.lock().unwrap();
393 let handle = inode as Handle;
394 if let Some(odb) = open_dirs.get_mut(&handle) {
395 if odb.open_count.checked_sub(1).ok_or_else(ebadf)? == 0 {
396 open_dirs.remove(&handle);
397 }
398 Ok(())
399 } else {
400 Err(ebadf())
401 }
402 }
403
404 fn readdir(
405 &self,
406 _ctx: Context,
407 inode: Self::Inode,
408 _handle: Self::Handle,
409 size: u32,
410 offset: u64,
411 ) -> io::Result<Self::DirIter> {
412 let open_dirs = self.open_dirs.lock().unwrap();
413 let handle = inode as Handle;
414 let odb = open_dirs.get(&handle).ok_or_else(ebadf)?;
415 if odb.open_count == 0 {
416 return Err(ebadf());
417 }
418 let buf = &odb.buf;
419 let start = offset as usize;
Jiyong Park63a95cf2021-05-13 19:20:30 +0900420
421 // Estimate the size of each entry will take space in the buffer. See
422 // external/crosvm/fuse/src/server.rs#add_dirent
423 let mut estimate: usize = 0; // estimated number of bytes we will be writing
424 let mut end = start; // index in `buf`
425 while estimate < size as usize && end < buf.len() {
426 let dirent_size = size_of::<fuse::sys::Dirent>();
427 let name_size = buf[end].0.to_bytes().len();
428 estimate += (dirent_size + name_size + 7) & !7; // round to 8 byte boundary
429 end += 1;
430 }
431
Jiyong Park331d1ea2021-05-10 11:01:23 +0900432 let mut new_buf = Vec::with_capacity(end - start);
433 // The portion of `buf` is *copied* to the iterator. This is not ideal, but inevitable
434 // because the `name` field in `fuse::filesystem::DirEntry` is `&CStr` not `CString`.
435 new_buf.extend_from_slice(&buf[start..end]);
436 Ok(DirIter { inner: new_buf, offset, cur: 0 })
437 }
438}
439
440struct DirIter {
441 inner: Vec<(CString, DirectoryEntry)>,
442 offset: u64, // the offset where this iterator begins. `next` doesn't change this.
443 cur: usize, // the current index in `inner`. `next` advances this.
444}
445
446impl fuse::filesystem::DirectoryIterator for DirIter {
447 fn next(&mut self) -> Option<fuse::filesystem::DirEntry> {
448 if self.cur >= self.inner.len() {
449 return None;
450 }
451
452 let (name, entry) = &self.inner[self.cur];
453 self.cur += 1;
454 Some(fuse::filesystem::DirEntry {
455 ino: entry.inode as libc::ino64_t,
456 offset: self.offset + self.cur as u64,
457 type_: match entry.kind {
458 InodeKind::Directory => libc::DT_DIR.into(),
459 InodeKind::File => libc::DT_REG.into(),
460 },
461 name,
462 })
463 }
464}
465
466#[cfg(test)]
467mod tests {
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000468 use super::*;
469 use anyhow::bail;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900470 use nix::sys::statfs::{statfs, FsType};
Jiyong Park63a95cf2021-05-13 19:20:30 +0900471 use std::collections::BTreeSet;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900472 use std::fs;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900473 use std::io::Write;
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900474 use std::os::unix::fs::MetadataExt;
Jiyong Park331d1ea2021-05-10 11:01:23 +0900475 use std::path::{Path, PathBuf};
476 use std::time::{Duration, Instant};
477 use zip::write::FileOptions;
478
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900479 #[derive(Default)]
480 struct Options {
481 noexec: bool,
482 uid: u32,
483 gid: u32,
484 }
485
Jiyong Park331d1ea2021-05-10 11:01:23 +0900486 #[cfg(not(target_os = "android"))]
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900487 fn start_fuse(zip_path: &Path, mnt_path: &Path, opt: Options) {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900488 let zip_path = PathBuf::from(zip_path);
489 let mnt_path = PathBuf::from(mnt_path);
490 std::thread::spawn(move || {
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900491 crate::run_fuse(&zip_path, &mnt_path, None, opt.noexec, opt.uid, opt.gid).unwrap();
Jiyong Park331d1ea2021-05-10 11:01:23 +0900492 });
493 }
494
495 #[cfg(target_os = "android")]
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900496 fn start_fuse(zip_path: &Path, mnt_path: &Path, opt: Options) {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900497 // Note: for some unknown reason, running a thread to serve fuse doesn't work on Android.
498 // Explicitly spawn a zipfuse process instead.
499 // TODO(jiyong): fix this
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900500 let noexec = if opt.noexec { "--noexec" } else { "" };
Jiyong Park331d1ea2021-05-10 11:01:23 +0900501 assert!(std::process::Command::new("sh")
502 .arg("-c")
Andrew Scull3854e402022-07-04 12:10:20 +0000503 .arg(format!(
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900504 "/data/local/tmp/zipfuse {} -u {} -g {} {} {}",
Andrew Scull3854e402022-07-04 12:10:20 +0000505 noexec,
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900506 opt.uid,
507 opt.gid,
Andrew Scull3854e402022-07-04 12:10:20 +0000508 zip_path.display(),
509 mnt_path.display()
510 ))
Jiyong Park331d1ea2021-05-10 11:01:23 +0900511 .spawn()
512 .is_ok());
513 }
514
515 fn wait_for_mount(mount_path: &Path) -> Result<()> {
516 let start_time = Instant::now();
517 const POLL_INTERVAL: Duration = Duration::from_millis(50);
518 const TIMEOUT: Duration = Duration::from_secs(10);
519 const FUSE_SUPER_MAGIC: FsType = FsType(0x65735546);
520 loop {
521 if statfs(mount_path)?.filesystem_type() == FUSE_SUPER_MAGIC {
522 break;
523 }
524
525 if start_time.elapsed() > TIMEOUT {
526 bail!("Time out mounting zipfuse");
527 }
528 std::thread::sleep(POLL_INTERVAL);
529 }
530 Ok(())
531 }
532
533 // Creates a zip file, adds some files to the zip file, mounts it using zipfuse, runs the check
534 // routine, and finally unmounts.
535 fn run_test(add: fn(&mut zip::ZipWriter<File>), check: fn(&std::path::Path)) {
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900536 run_test_with_options(Default::default(), add, check);
Andrew Scull3854e402022-07-04 12:10:20 +0000537 }
538
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900539 fn run_test_with_options(
540 opt: Options,
Andrew Scull3854e402022-07-04 12:10:20 +0000541 add: fn(&mut zip::ZipWriter<File>),
542 check: fn(&std::path::Path),
543 ) {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900544 // Create an empty zip file
545 let test_dir = tempfile::TempDir::new().unwrap();
546 let zip_path = test_dir.path().join("test.zip");
547 let zip = File::create(&zip_path);
548 assert!(zip.is_ok());
549 let mut zip = zip::ZipWriter::new(zip.unwrap());
550
551 // Let test users add files/dirs to the zip file
552 add(&mut zip);
553 assert!(zip.finish().is_ok());
554 drop(zip);
555
556 // Mount the zip file on the "mnt" dir using zipfuse.
557 let mnt_path = test_dir.path().join("mnt");
558 assert!(fs::create_dir(&mnt_path).is_ok());
559
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900560 start_fuse(&zip_path, &mnt_path, opt);
Jiyong Park331d1ea2021-05-10 11:01:23 +0900561
562 let mnt_path = test_dir.path().join("mnt");
563 // Give some time for the fuse to boot up
564 assert!(wait_for_mount(&mnt_path).is_ok());
565 // Run the check routine, and do the clean up.
566 check(&mnt_path);
567 assert!(nix::mount::umount2(&mnt_path, nix::mount::MntFlags::empty()).is_ok());
568 }
569
570 fn check_file(root: &Path, file: &str, content: &[u8]) {
571 let path = root.join(file);
572 assert!(path.exists());
573
574 let metadata = fs::metadata(&path);
575 assert!(metadata.is_ok());
576
577 let metadata = metadata.unwrap();
578 assert!(metadata.is_file());
579 assert_eq!(content.len(), metadata.len() as usize);
580
581 let read_data = fs::read(&path);
582 assert!(read_data.is_ok());
583 assert_eq!(content, read_data.unwrap().as_slice());
584 }
585
Jiyong Park63a95cf2021-05-13 19:20:30 +0900586 fn check_dir<S: AsRef<str>>(root: &Path, dir: &str, files: &[S], dirs: &[S]) {
Jiyong Park331d1ea2021-05-10 11:01:23 +0900587 let dir_path = root.join(dir);
588 assert!(dir_path.exists());
589
590 let metadata = fs::metadata(&dir_path);
591 assert!(metadata.is_ok());
592
593 let metadata = metadata.unwrap();
594 assert!(metadata.is_dir());
595
596 let iter = fs::read_dir(&dir_path);
597 assert!(iter.is_ok());
598
599 let iter = iter.unwrap();
Jiyong Park63a95cf2021-05-13 19:20:30 +0900600 let mut actual_files = BTreeSet::new();
601 let mut actual_dirs = BTreeSet::new();
Jiyong Park331d1ea2021-05-10 11:01:23 +0900602 for de in iter {
603 let entry = de.unwrap();
604 let path = entry.path();
605 if path.is_dir() {
606 actual_dirs.insert(path.strip_prefix(&dir_path).unwrap().to_path_buf());
607 } else {
608 actual_files.insert(path.strip_prefix(&dir_path).unwrap().to_path_buf());
609 }
610 }
Jiyong Park63a95cf2021-05-13 19:20:30 +0900611 let expected_files: BTreeSet<PathBuf> =
612 files.iter().map(|s| PathBuf::from(s.as_ref())).collect();
613 let expected_dirs: BTreeSet<PathBuf> =
614 dirs.iter().map(|s| PathBuf::from(s.as_ref())).collect();
Jiyong Park331d1ea2021-05-10 11:01:23 +0900615
616 assert_eq!(expected_files, actual_files);
617 assert_eq!(expected_dirs, actual_dirs);
618 }
619
620 #[test]
621 fn empty() {
622 run_test(
623 |_| {},
624 |root| {
Jiyong Park63a95cf2021-05-13 19:20:30 +0900625 check_dir::<String>(root, "", &[], &[]);
Jiyong Park331d1ea2021-05-10 11:01:23 +0900626 },
627 );
628 }
629
630 #[test]
631 fn single_file() {
632 run_test(
633 |zip| {
634 zip.start_file("foo", FileOptions::default()).unwrap();
635 zip.write_all(b"0123456789").unwrap();
636 },
637 |root| {
638 check_dir(root, "", &["foo"], &[]);
639 check_file(root, "foo", b"0123456789");
640 },
641 );
642 }
643
644 #[test]
Andrew Scull3854e402022-07-04 12:10:20 +0000645 fn noexec() {
646 fn add_executable(zip: &mut zip::ZipWriter<File>) {
647 zip.start_file("executable", FileOptions::default().unix_permissions(0o755)).unwrap();
648 }
649
650 // Executables can be run when not mounting with noexec.
651 run_test(add_executable, |root| {
652 let res = std::process::Command::new(root.join("executable")).status();
653 res.unwrap();
654 });
655
656 // Mounting with noexec results in permissions denial when running an executable.
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900657 let opt = Options { noexec: true, ..Default::default() };
658 run_test_with_options(opt, add_executable, |root| {
Andrew Scull3854e402022-07-04 12:10:20 +0000659 let res = std::process::Command::new(root.join("executable")).status();
660 assert!(matches!(res.unwrap_err().kind(), std::io::ErrorKind::PermissionDenied));
661 });
662 }
663
664 #[test]
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900665 fn uid_gid() {
666 const UID: u32 = 100;
667 const GID: u32 = 200;
668 run_test_with_options(
669 Options { noexec: true, uid: UID, gid: GID },
670 |zip| {
671 zip.start_file("foo", FileOptions::default()).unwrap();
672 zip.write_all(b"0123456789").unwrap();
673 },
674 |root| {
675 let path = root.join("foo");
676
Charisee96113f32023-01-26 09:00:42 +0000677 let metadata = fs::metadata(path);
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900678 assert!(metadata.is_ok());
679 let metadata = metadata.unwrap();
680
681 assert_eq!(UID, metadata.uid());
682 assert_eq!(GID, metadata.gid());
683 },
684 );
685 }
686
687 #[test]
Jiyong Park331d1ea2021-05-10 11:01:23 +0900688 fn single_dir() {
689 run_test(
690 |zip| {
691 zip.add_directory("dir", FileOptions::default()).unwrap();
692 },
693 |root| {
694 check_dir(root, "", &[], &["dir"]);
Jiyong Park63a95cf2021-05-13 19:20:30 +0900695 check_dir::<String>(root, "dir", &[], &[]);
Jiyong Park331d1ea2021-05-10 11:01:23 +0900696 },
697 );
698 }
699
700 #[test]
701 fn complex_hierarchy() {
702 // root/
703 // a/
704 // b1/
705 // b2/
706 // c1 (file)
707 // c2/
708 // d1 (file)
709 // d2 (file)
710 // d3 (file)
711 // x/
712 // y1 (file)
713 // y2 (file)
714 // y3/
715 //
716 // foo (file)
717 // bar (file)
718 run_test(
719 |zip| {
720 let opt = FileOptions::default();
721 zip.add_directory("a/b1", opt).unwrap();
722
723 zip.start_file("a/b2/c1", opt).unwrap();
724
725 zip.start_file("a/b2/c2/d1", opt).unwrap();
726 zip.start_file("a/b2/c2/d2", opt).unwrap();
727 zip.start_file("a/b2/c2/d3", opt).unwrap();
728
729 zip.start_file("x/y1", opt).unwrap();
730 zip.start_file("x/y2", opt).unwrap();
731 zip.add_directory("x/y3", opt).unwrap();
732
733 zip.start_file("foo", opt).unwrap();
734 zip.start_file("bar", opt).unwrap();
735 },
736 |root| {
737 check_dir(root, "", &["foo", "bar"], &["a", "x"]);
738 check_dir(root, "a", &[], &["b1", "b2"]);
Jiyong Park63a95cf2021-05-13 19:20:30 +0900739 check_dir::<String>(root, "a/b1", &[], &[]);
Jiyong Park331d1ea2021-05-10 11:01:23 +0900740 check_dir(root, "a/b2", &["c1"], &["c2"]);
741 check_dir(root, "a/b2/c2", &["d1", "d2", "d3"], &[]);
742 check_dir(root, "x", &["y1", "y2"], &["y3"]);
Jiyong Park63a95cf2021-05-13 19:20:30 +0900743 check_dir::<String>(root, "x/y3", &[], &[]);
Jiyong Park331d1ea2021-05-10 11:01:23 +0900744 check_file(root, "a/b2/c1", &[]);
745 check_file(root, "a/b2/c2/d1", &[]);
746 check_file(root, "a/b2/c2/d2", &[]);
747 check_file(root, "a/b2/c2/d3", &[]);
748 check_file(root, "x/y1", &[]);
749 check_file(root, "x/y2", &[]);
750 check_file(root, "foo", &[]);
751 check_file(root, "bar", &[]);
752 },
753 );
754 }
755
756 #[test]
757 fn large_file() {
758 run_test(
759 |zip| {
760 let data = vec![10; 2 << 20];
761 zip.start_file("foo", FileOptions::default()).unwrap();
762 zip.write_all(&data).unwrap();
763 },
764 |root| {
765 let data = vec![10; 2 << 20];
766 check_file(root, "foo", &data);
767 },
768 );
769 }
Jiyong Park63a95cf2021-05-13 19:20:30 +0900770
771 #[test]
772 fn large_dir() {
773 const NUM_FILES: usize = 1 << 10;
774 run_test(
775 |zip| {
776 let opt = FileOptions::default();
777 // create 1K files. Each file has a name of length 100. So total size is at least
778 // 100KB, which is bigger than the readdir buffer size of 4K.
779 for i in 0..NUM_FILES {
780 zip.start_file(format!("dir/{:0100}", i), opt).unwrap();
781 }
782 },
783 |root| {
784 let dirs_expected: Vec<_> = (0..NUM_FILES).map(|i| format!("{:0100}", i)).collect();
785 check_dir(
786 root,
787 "dir",
788 dirs_expected.iter().map(|s| s.as_str()).collect::<Vec<&str>>().as_slice(),
789 &[],
790 );
791 },
792 );
793 }
Jiyong Parkd40f7bb2021-05-17 10:55:56 +0900794
Jiyong Parke6587ca2021-05-17 14:42:23 +0900795 fn run_fuse_and_check_test_zip(test_dir: &Path, zip_path: &Path) {
796 let mnt_path = test_dir.join("mnt");
Jiyong Parkd40f7bb2021-05-17 10:55:56 +0900797 assert!(fs::create_dir(&mnt_path).is_ok());
798
Jiyong Park0e74d9f2023-01-10 14:12:20 +0900799 let opt = Options { noexec: false, ..Default::default() };
800 start_fuse(zip_path, &mnt_path, opt);
Jiyong Parkd40f7bb2021-05-17 10:55:56 +0900801
802 // Give some time for the fuse to boot up
803 assert!(wait_for_mount(&mnt_path).is_ok());
804
805 check_dir(&mnt_path, "", &[], &["dir"]);
806 check_dir(&mnt_path, "dir", &["file1", "file2"], &[]);
807 check_file(&mnt_path, "dir/file1", include_bytes!("../testdata/dir/file1"));
808 check_file(&mnt_path, "dir/file2", include_bytes!("../testdata/dir/file2"));
809 assert!(nix::mount::umount2(&mnt_path, nix::mount::MntFlags::empty()).is_ok());
810 }
Jiyong Parke6587ca2021-05-17 14:42:23 +0900811
812 #[test]
813 fn supports_deflate() {
814 let test_dir = tempfile::TempDir::new().unwrap();
815 let zip_path = test_dir.path().join("test.zip");
816 let mut zip_file = File::create(&zip_path).unwrap();
817 zip_file.write_all(include_bytes!("../testdata/test.zip")).unwrap();
818
Chris Wailes68c39f82021-07-27 16:03:44 -0700819 run_fuse_and_check_test_zip(test_dir.path(), &zip_path);
Jiyong Parke6587ca2021-05-17 14:42:23 +0900820 }
821
Jiyong Parkf5ff33c2021-08-30 22:32:19 +0900822 #[test]
823 fn supports_store() {
824 run_test(
825 |zip| {
826 let data = vec![10; 2 << 20];
827 zip.start_file(
828 "foo",
829 FileOptions::default().compression_method(zip::CompressionMethod::Stored),
830 )
831 .unwrap();
832 zip.write_all(&data).unwrap();
833 },
834 |root| {
835 let data = vec![10; 2 << 20];
836 check_file(root, "foo", &data);
837 },
838 );
839 }
840
Jiyong Parke6587ca2021-05-17 14:42:23 +0900841 #[cfg(not(target_os = "android"))] // Android doesn't have the loopdev crate
842 #[test]
843 fn supports_zip_on_block_device() {
844 // Write test.zip to the test directory
845 let test_dir = tempfile::TempDir::new().unwrap();
846 let zip_path = test_dir.path().join("test.zip");
847 let mut zip_file = File::create(&zip_path).unwrap();
848 let data = include_bytes!("../testdata/test.zip");
849 zip_file.write_all(data).unwrap();
850
851 // Pad 0 to test.zip so that its size is multiple of 4096.
852 const BLOCK_SIZE: usize = 4096;
853 let size = (data.len() + BLOCK_SIZE) & !BLOCK_SIZE;
854 let pad_size = size - data.len();
855 assert!(pad_size != 0);
856 let pad = vec![0; pad_size];
857 zip_file.write_all(pad.as_slice()).unwrap();
858 drop(zip_file);
859
860 // Attach test.zip to a loop device
861 let lc = loopdev::LoopControl::open().unwrap();
862 let ld = scopeguard::guard(lc.next_free().unwrap(), |ld| {
863 ld.detach().unwrap();
864 });
865 ld.attach_file(&zip_path).unwrap();
866
867 // Start zipfuse over to the loop device (not the zip file)
868 run_fuse_and_check_test_zip(&test_dir.path(), &ld.path().unwrap());
869 }
Andrew Walbranaa1efc42022-08-10 13:33:57 +0000870
871 #[test]
872 fn verify_command() {
873 // Check that the command parsing has been configured in a valid way.
874 clap_command().debug_assert();
875 }
Jiyong Park331d1ea2021-05-10 11:01:23 +0900876}