Repository: pkolaczk/fclones (language: rust)

Issue:
Can't compile on FreeBSD
Tried to compile from source on a FreeBSD jail, got these errors, tried again with the verbose flag to get more information.

   Compiling fclones v0.17.0

error[E0063]: missing field `l_sysid` in initializer of `flock`
  --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/fclones-0.17.0/src/lock.rs:31:17
   |
31 |         let f = libc::flock {
   |                 ^^^^^^^^^^^ missing `l_sysid`

error[E0063]: missing field `l_sysid` in initializer of `flock`
  --> /root/.cargo/registry/src/github.com-1ecc6299db9ec823/fclones-0.17.0/src/lock.rs:47:17
   |
47 |         let f = libc::flock {
   |                 ^^^^^^^^^^^ missing `l_sysid`

error: aborting due to 2 previous errors



For more information about this error, try `rustc --explain E0063`.
error: failed to compile `fclones v0.17.0`, intermediate artifacts can be foundat `/tmp/cargo-installPdO1Nf`

Caused by:
  could not compile `fclones`

See attached file for full message. 
[FreeBSD fclones Errors.docx](https://github.com/pkolaczk/fclones/files/7425119/FreeBSD.fclones.Errors.docx)


Discussion / hints:
Thank you for the report, I'll take a look.

Fix patch:
diff --git a/src/lock.rs b/src/lock.rs
index 8550bda..0e87ea5 100644
--- a/src/lock.rs
+++ b/src/lock.rs
@@ -23,18 +23,19 @@ impl FileLock {
         }
     }
 
+    /// Creates a libc::flock initialized to zeros.
+    /// Should be safe, because flock contains primitive fields only, no references.
+    fn new_flock() -> libc::flock {
+        unsafe { std::mem::zeroed() }
+    }
+
     #[cfg(unix)]
     fn fcntl_lock(file: &File) -> io::Result<()> {
         use nix::fcntl::*;
         use std::os::unix::io::AsRawFd;
-
-        let f = libc::flock {
-            l_type: libc::F_WRLCK as i16,
-            l_whence: libc::SEEK_SET as i16,
-            l_start: 0,
-            l_len: 0,
-            l_pid: 0,
-        };
+        let mut f = Self::new_flock();
+        f.l_type = libc::F_WRLCK as i16;
+        f.l_whence = libc::SEEK_SET as i16;
         let result = nix::fcntl::fcntl(file.as_raw_fd(), FcntlArg::F_SETLK(&f));
         Self::nix_as_io_error(result).map(|_| {})
     }
@@ -43,14 +44,9 @@ impl FileLock {
     fn fcntl_unlock(file: &File) -> io::Result<()> {
         use nix::fcntl::*;
         use std::os::unix::io::AsRawFd;
-
-        let f = libc::flock {
-            l_type: libc::F_UNLCK as i16,
-            l_whence: libc::SEEK_SET as i16,
-            l_start: 0,
-            l_len: 0,
-            l_pid: 0,
-        };
+        let mut f = Self::new_flock();
+        f.l_type = libc::F_UNLCK as i16;
+        f.l_whence = libc::SEEK_SET as i16;
         let result = nix::fcntl::fcntl(file.as_raw_fd(), FcntlArg::F_SETLK(&f));
         Self::nix_as_io_error(result).map(|_| {})
     }


Test patch: