-
Notifications
You must be signed in to change notification settings - Fork 51
/
lib.rs
452 lines (388 loc) · 15.9 KB
/
lib.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
#![cfg_attr(test, feature(test))]
#![deny(warnings)]
#[cfg(unix)]
mod unix;
#[cfg(unix)]
use unix as sys;
#[cfg(windows)]
mod windows;
#[cfg(windows)]
use windows as sys;
use std::fs::File;
use std::io::{Error, Result};
use std::path::Path;
/// Extension trait for `std::fs::File` which provides allocation, duplication and locking methods.
///
/// ## Notes on File Locks
///
/// This library provides whole-file locks in both shared (read) and exclusive
/// (read-write) varieties.
///
/// File locks are a cross-platform hazard since the file lock APIs exposed by
/// operating system kernels vary in subtle and not-so-subtle ways.
///
/// The API exposed by this library can be safely used across platforms as long
/// as the following rules are followed:
///
/// * Multiple locks should not be created on an individual `File` instance
/// concurrently.
/// * Duplicated files should not be locked without great care.
/// * Files to be locked should be opened with at least read or write
/// permissions.
/// * File locks may only be relied upon to be advisory.
///
/// See the tests in `lib.rs` for cross-platform lock behavior that may be
/// relied upon; see the tests in `unix.rs` and `windows.rs` for examples of
/// platform-specific behavior. File locks are implemented with
/// [`flock(2)`](http://man7.org/linux/man-pages/man2/flock.2.html) on Unix and
/// [`LockFile`](https://msdn.microsoft.com/en-us/library/windows/desktop/aa365202(v=vs.85).aspx)
/// on Windows.
pub trait FileExt {
/// Returns a duplicate instance of the file.
///
/// The returned file will share the same file position as the original
/// file.
///
/// If using rustc version 1.9 or later, prefer using `File::try_clone` to this.
///
/// # Notes
///
/// This is implemented with
/// [`dup(2)`](http://man7.org/linux/man-pages/man2/dup.2.html) on Unix and
/// [`DuplicateHandle`](https://msdn.microsoft.com/en-us/library/windows/desktop/ms724251(v=vs.85).aspx)
/// on Windows.
fn duplicate(&self) -> Result<File>;
/// Returns the amount of physical space allocated for a file.
fn allocated_size(&self) -> Result<u64>;
/// Ensures that at least `len` bytes of disk space are allocated for the
/// file, and the file size is at least `len` bytes. After a successful call
/// to `allocate`, subsequent writes to the file within the specified length
/// are guaranteed not to fail because of lack of disk space.
fn allocate(&self, len: u64) -> Result<()>;
/// Locks the file for shared usage, blocking if the file is currently
/// locked exclusively.
fn lock_shared(&self) -> Result<()>;
/// Locks the file for exclusive usage, blocking if the file is currently
/// locked.
fn lock_exclusive(&self) -> Result<()>;
/// Locks the file for shared usage, or returns a an error if the file is
/// currently locked (see `lock_contended_error`).
fn try_lock_shared(&self) -> Result<()>;
/// Locks the file for shared usage, or returns a an error if the file is
/// currently locked (see `lock_contended_error`).
fn try_lock_exclusive(&self) -> Result<()>;
/// Unlocks the file.
fn unlock(&self) -> Result<()>;
}
impl FileExt for File {
fn duplicate(&self) -> Result<File> {
sys::duplicate(self)
}
fn allocated_size(&self) -> Result<u64> {
sys::allocated_size(self)
}
fn allocate(&self, len: u64) -> Result<()> {
sys::allocate(self, len)
}
fn lock_shared(&self) -> Result<()> {
sys::lock_shared(self)
}
fn lock_exclusive(&self) -> Result<()> {
sys::lock_exclusive(self)
}
fn try_lock_shared(&self) -> Result<()> {
sys::try_lock_shared(self)
}
fn try_lock_exclusive(&self) -> Result<()> {
sys::try_lock_exclusive(self)
}
fn unlock(&self) -> Result<()> {
sys::unlock(self)
}
}
/// Returns the error that a call to a try lock method on a contended file will
/// return.
pub fn lock_contended_error() -> Error {
sys::lock_error()
}
/// `FsStats` contains some common stats about a file system.
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct FsStats {
free_space: u64,
available_space: u64,
total_space: u64,
allocation_granularity: u64,
}
impl FsStats {
/// Returns the number of free bytes in the file system containing the provided
/// path.
pub fn free_space(&self) -> u64 {
self.free_space
}
/// Returns the available space in bytes to non-priveleged users in the file
/// system containing the provided path.
pub fn available_space(&self) -> u64 {
self.available_space
}
/// Returns the total space in bytes in the file system containing the provided
/// path.
pub fn total_space(&self) -> u64 {
self.total_space
}
/// Returns the filesystem's disk space allocation granularity in bytes.
/// The provided path may be for any file in the filesystem.
///
/// On Posix, this is equivalent to the filesystem's block size.
/// On Windows, this is equivalent to the filesystem's cluster size.
pub fn allocation_granularity(&self) -> u64 {
self.allocation_granularity
}
}
/// Get the stats of the file system containing the provided path.
pub fn statvfs<P>(path: P) -> Result<FsStats> where P: AsRef<Path> {
sys::statvfs(path.as_ref())
}
/// Returns the number of free bytes in the file system containing the provided
/// path.
pub fn free_space<P>(path: P) -> Result<u64> where P: AsRef<Path> {
statvfs(path).map(|stat| stat.free_space)
}
/// Returns the available space in bytes to non-priveleged users in the file
/// system containing the provided path.
pub fn available_space<P>(path: P) -> Result<u64> where P: AsRef<Path> {
statvfs(path).map(|stat| stat.available_space)
}
/// Returns the total space in bytes in the file system containing the provided
/// path.
pub fn total_space<P>(path: P) -> Result<u64> where P: AsRef<Path> {
statvfs(path).map(|stat| stat.total_space)
}
/// Returns the filesystem's disk space allocation granularity in bytes.
/// The provided path may be for any file in the filesystem.
///
/// On Posix, this is equivalent to the filesystem's block size.
/// On Windows, this is equivalent to the filesystem's cluster size.
pub fn allocation_granularity<P>(path: P) -> Result<u64> where P: AsRef<Path> {
statvfs(path).map(|stat| stat.allocation_granularity)
}
#[cfg(test)]
mod test {
extern crate tempdir;
extern crate test;
use std::fs;
use super::*;
use std::io::{Read, Seek, SeekFrom, Write};
/// Tests file duplication.
#[test]
fn duplicate() {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("fs2");
let mut file1 =
fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
let mut file2 = file1.duplicate().unwrap();
// Write into the first file and then drop it.
file1.write_all(b"foo").unwrap();
drop(file1);
let mut buf = vec![];
// Read from the second file; since the position is shared it will already be at EOF.
file2.read_to_end(&mut buf).unwrap();
assert_eq!(0, buf.len());
// Rewind and read.
file2.seek(SeekFrom::Start(0)).unwrap();
file2.read_to_end(&mut buf).unwrap();
assert_eq!(&buf, &b"foo");
}
/// Tests shared file lock operations.
#[test]
fn lock_shared() {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("fs2");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
let file3 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
// Concurrent shared access is OK, but not shared and exclusive.
file1.lock_shared().unwrap();
file2.lock_shared().unwrap();
assert_eq!(file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
file1.unlock().unwrap();
assert_eq!(file3.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
// Once all shared file locks are dropped, an exclusive lock may be created;
file2.unlock().unwrap();
file3.lock_exclusive().unwrap();
}
/// Tests exclusive file lock operations.
#[test]
fn lock_exclusive() {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("fs2");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
// No other access is possible once an exclusive lock is created.
file1.lock_exclusive().unwrap();
assert_eq!(file2.try_lock_exclusive().unwrap_err().kind(),
lock_contended_error().kind());
assert_eq!(file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind());
// Once the exclusive lock is dropped, the second file is able to create a lock.
file1.unlock().unwrap();
file2.lock_exclusive().unwrap();
}
/// Tests that a lock is released after the file that owns it is dropped.
#[test]
fn lock_cleanup() {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("fs2");
let file1 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
let file2 = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
file1.lock_exclusive().unwrap();
assert_eq!(file2.try_lock_shared().unwrap_err().kind(),
lock_contended_error().kind());
// Drop file1; the lock should be released.
drop(file1);
file2.lock_shared().unwrap();
}
/// Tests file allocation.
#[test]
fn allocate() {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("fs2");
let file = fs::OpenOptions::new().write(true).create(true).open(&path).unwrap();
let blksize = allocation_granularity(&path).unwrap();
// New files are created with no allocated size.
assert_eq!(0, file.allocated_size().unwrap());
assert_eq!(0, file.metadata().unwrap().len());
// Allocate space for the file, checking that the allocated size steps
// up by block size, and the file length matches the allocated size.
file.allocate(2 * blksize - 1).unwrap();
assert_eq!(2 * blksize, file.allocated_size().unwrap());
assert_eq!(2 * blksize - 1, file.metadata().unwrap().len());
// Truncate the file, checking that the allocated size steps down by
// block size.
file.set_len(blksize + 1).unwrap();
assert_eq!(2 * blksize, file.allocated_size().unwrap());
assert_eq!(blksize + 1, file.metadata().unwrap().len());
}
/// Checks filesystem space methods.
#[test]
fn filesystem_space() {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let total_space = total_space(&tempdir.path()).unwrap();
let free_space = free_space(&tempdir.path()).unwrap();
let available_space = available_space(&tempdir.path()).unwrap();
assert!(total_space > free_space);
assert!(total_space > available_space);
assert!(available_space <= free_space);
}
/// Benchmarks creating and removing a file. This is a baseline benchmark
/// for comparing against the truncate and allocate benchmarks.
#[bench]
fn bench_file_create(b: &mut test::Bencher) {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("file");
b.iter(|| {
fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.unwrap();
fs::remove_file(&path).unwrap();
});
}
/// Benchmarks creating a file, truncating it to 32MiB, and deleting it.
#[bench]
fn bench_file_truncate(b: &mut test::Bencher) {
let size = 32 * 1024 * 1024;
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("file");
b.iter(|| {
let file = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.unwrap();
file.set_len(size).unwrap();
fs::remove_file(&path).unwrap();
});
}
/// Benchmarks creating a file, allocating 32MiB for it, and deleting it.
#[bench]
fn bench_file_allocate(b: &mut test::Bencher) {
let size = 32 * 1024 * 1024;
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("file");
b.iter(|| {
let file = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.unwrap();
file.allocate(size).unwrap();
fs::remove_file(&path).unwrap();
});
}
/// Benchmarks creating a file, allocating 32MiB for it, and deleting it.
#[bench]
fn bench_allocated_size(b: &mut test::Bencher) {
let size = 32 * 1024 * 1024;
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("file");
let file = fs::OpenOptions::new()
.read(true)
.write(true)
.create(true)
.open(&path)
.unwrap();
file.allocate(size).unwrap();
b.iter(|| {
file.allocated_size().unwrap();
});
}
/// Benchmarks duplicating a file descriptor or handle.
#[bench]
fn bench_duplicate(b: &mut test::Bencher) {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("fs2");
let file = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
b.iter(|| test::black_box(file.duplicate().unwrap()));
}
/// Benchmarks locking and unlocking a file lock.
#[bench]
fn bench_lock_unlock(b: &mut test::Bencher) {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
let path = tempdir.path().join("fs2");
let file = fs::OpenOptions::new().read(true).write(true).create(true).open(&path).unwrap();
b.iter(|| {
file.lock_exclusive().unwrap();
file.unlock().unwrap();
});
}
/// Benchmarks the free space method.
#[bench]
fn bench_free_space(b: &mut test::Bencher) {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
b.iter(|| {
test::black_box(free_space(&tempdir.path()).unwrap());
});
}
/// Benchmarks the available space method.
#[bench]
fn bench_available_space(b: &mut test::Bencher) {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
b.iter(|| {
test::black_box(available_space(&tempdir.path()).unwrap());
});
}
/// Benchmarks the total space method.
#[bench]
fn bench_total_space(b: &mut test::Bencher) {
let tempdir = tempdir::TempDir::new("fs2").unwrap();
b.iter(|| {
test::black_box(total_space(&tempdir.path()).unwrap());
});
}
}