Skip to content

Commit 56489f2

Browse files
committed
Open README with write permissions
Evidently, some file systems require write permissions in order to lock a file or locking will fail. Open the README file, which we use for locking purposes, with write permissions in an attempt to make locking work on all file systems that the crate may be built on. Closes: libbpf#116 Signed-off-by: Daniel Müller <[email protected]>
1 parent 6a0ecb4 commit 56489f2

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

build.rs

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ use std::env;
44
use std::ffi;
55
use std::fs;
66
use std::fs::read_dir;
7+
use std::fs::File;
8+
use std::io;
79
use std::path;
810
use std::path::Path;
911
use std::process;
@@ -208,12 +210,31 @@ fn main() {
208210
}
209211
}
210212

213+
fn open_lockable(path: &Path) -> io::Result<File> {
214+
let result = File::options()
215+
.read(true)
216+
// Open with write permissions because flock(2) may require them
217+
// on some platforms.
218+
.write(true)
219+
.open(path);
220+
match result {
221+
Ok(file) => Ok(file),
222+
Err(err) if err.kind() == io::ErrorKind::PermissionDenied => {
223+
// On a read-only file system we may not be able to open
224+
// with write permissions. So just open for reading and hope
225+
// for the best.
226+
File::open(path)
227+
},
228+
e @ Err(..) => e,
229+
}
230+
}
231+
211232
fn make_zlib(compiler: &cc::Tool, src_dir: &path::Path, out_dir: &path::Path) {
212233
let src_dir = src_dir.join("zlib");
213234
// lock README such that if two crates are trying to compile
214235
// this at the same time (eg libbpf-rs libbpf-cargo)
215236
// they wont trample each other
216-
let file = std::fs::File::open(src_dir.join("README")).unwrap();
237+
let file = open_lockable(&src_dir.join("README")).unwrap();
217238
let _lock = fcntl::Flock::lock(file, fcntl::FlockArg::LockExclusive).unwrap();
218239

219240
let status = process::Command::new("./configure")
@@ -254,7 +275,7 @@ fn make_elfutils(compiler: &cc::Tool, src_dir: &path::Path, out_dir: &path::Path
254275
// lock README such that if two crates are trying to compile
255276
// this at the same time (eg libbpf-rs libbpf-cargo)
256277
// they wont trample each other
257-
let file = std::fs::File::open(src_dir.join("elfutils/README")).unwrap();
278+
let file = open_lockable(&src_dir.join("elfutils/README")).unwrap();
258279
let _lock = fcntl::Flock::lock(file, fcntl::FlockArg::LockExclusive).unwrap();
259280

260281
let flags = compiler

0 commit comments

Comments
 (0)