fn new<P: AsRef<Path>>(path: P) -> Result<PidFile, PidFileError> {
        // First we try to open the pidfile, without disturbing it.
        let mut pidfile = fs::OpenOptions::new()
            .truncate(false)
            .create(true)
            .read(true)
            .write(true)
            .open(path.as_ref())
            .map_err(PidFileError::CouldNotOpen)?;

        // Now try to acquire an exclusive lock. This will fail if another process or another
        // instance of `PidFile` is holding a lock onto the same pidfile.
        pidfile
            .try_lock_exclusive()
            .map_err(PidFileError::LockFailed)?;

        // At this point, we're the exclusive users of the file and can read its contents.
        let mut raw_contents = String::new();
        pidfile
            .read_to_string(&mut raw_contents)
            .map_err(PidFileError::ReadFailed)?;

        // Note: We cannot distinguish an empty file from a non-existing file, unfortunately.
        let previous = if raw_contents.is_empty() {
            None
        } else {
            Some(raw_contents.parse().map_err(PidFileError::Corrupted)?)
        };

        let pid = process::id();

        // Truncate and rewind.
        pidfile.set_len(0).map_err(PidFileError::WriteFailed)?;
        pidfile
            .seek(SeekFrom::Start(0))
            .map_err(PidFileError::WriteFailed)?;

        // Do our best to ensure that we always have some contents in the file immediately.
        pidfile
            .write_all(pid.to_string().as_bytes())
            .map_err(PidFileError::WriteFailed)?;

        pidfile.flush().map_err(PidFileError::WriteFailed)?;

        Ok(PidFile {
            _pidfile: pidfile,
            path: path.as_ref().to_owned(),
            previous,
        })
    }
/// Creates a new pidfile.
///
/// The error-behavior of this function is important and can be used to distinguish between
/// different conditions described in [`PidFileError`]. If the `PidFile` is instantiated before
/// the resource it is supposed to protect, the following actions are recommended:
Creates a new pidfile.
The error-behavior of this function is important and can be used to distinguish between
different conditions described in [`PidFileError`]. If the `PidFile` is instantiated before
the resource it is supposed to protect, the following actions are recommended.
Creates a new pidfile . The error - behavior of this function is important and can be used to distinguish between different conditions described in [ ` PidFileError ` ] . If the ` PidFile ` is instantiated before the resource it is supposed to protect the following actions are recommended .
fn new<P: AsRef<Path>>(path: P) -> Result<PidFile, PidFileError> {
        // First we try to open the pidfile, without disturbing it.
        let mut pidfile = fs::OpenOptions::new()
            .truncate(false)
            .create(true)
            .read(true)
            .write(true)
            .open(path.as_ref())
            .map_err(PidFileError::CouldNotOpen)?;
        // Now try to acquire an exclusive lock. This will fail if another process or another
        // instance of `PidFile` is holding a lock onto the same pidfile.
        pidfile
            .try_lock_exclusive()
            .map_err(PidFileError::LockFailed)?;
        // At this point, we're the exclusive users of the file and can read its contents.
        let mut raw_contents = String::new();
        pidfile
            .read_to_string(&mut raw_contents)
            .map_err(PidFileError::ReadFailed)?;
        // Note: We cannot distinguish an empty file from a non-existing file, unfortunately.
        let previous = if raw_contents.is_empty() {
            None
        } else {
            Some(raw_contents.parse().map_err(PidFileError::Corrupted)?)
        };
        let pid = process::id();
        // Truncate and rewind.
        pidfile.set_len(0).map_err(PidFileError::WriteFailed)?;
        pidfile
            .seek(SeekFrom::Start(0))
            .map_err(PidFileError::WriteFailed)?;
        // Do our best to ensure that we always have some contents in the file immediately.
        pidfile
            .write_all(pid.to_string().as_bytes())
            .map_err(PidFileError::WriteFailed)?;
        pidfile.flush().map_err(PidFileError::WriteFailed)?;
        Ok(PidFile {
            _pidfile: pidfile,
            path: path.as_ref().to_owned(),
            previous,
        })
    }
fn new < P : AsRef < Path > > ( path : P ) -> Result < PidFile , PidFileError > { // First we try to open the pidfile, without disturbing it. let mut pidfile = fs :: OpenOptions :: new ( ) . truncate ( false ) . create ( true ) . read ( true ) . write ( true ) . open ( path . as_ref ( ) ) . map_err ( PidFileError :: CouldNotOpen ) ? ; // Now try to acquire an exclusive lock. This will fail if another process or another // instance of `PidFile` is holding a lock onto the same pidfile. pidfile . try_lock_exclusive ( ) . map_err ( PidFileError :: LockFailed ) ? ; // At this point, we're the exclusive users of the file and can read its contents. let mut raw_contents = String :: new ( ) ; pidfile . read_to_string ( & mut raw_contents ) . map_err ( PidFileError :: ReadFailed ) ? ; // Note: We cannot distinguish an empty file from a non-existing file, unfortunately. let previous = if raw_contents . is_empty ( ) { None } else { Some ( raw_contents . parse ( ) . map_err ( PidFileError :: Corrupted ) ? ) } ; let pid = process :: id ( ) ; // Truncate and rewind. pidfile . set_len ( 0 ) . map_err ( PidFileError :: WriteFailed ) ? ; pidfile . seek ( SeekFrom :: Start ( 0 ) ) . map_err ( PidFileError :: WriteFailed ) ? ; // Do our best to ensure that we always have some contents in the file immediately. pidfile . write_all ( pid . to_string ( ) . as_bytes ( ) ) . map_err ( PidFileError :: WriteFailed ) ? ; pidfile . flush ( ) . map_err ( PidFileError :: WriteFailed ) ? ; Ok ( PidFile { _pidfile : pidfile , path : path . as_ref ( ) . to_owned ( ) , previous , } ) }
Creates a new pidfile.
Creates a new pidfile .
path
P
path
P