Skip to content

Commit

Permalink
Merge pull request #21 from nastevens/improve-from-file-path
Browse files Browse the repository at this point in the history
Improve from_file path handling
  • Loading branch information
posborne authored Aug 5, 2016
2 parents 14f2015 + 8b9f799 commit f1316d6
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: rust
sudo: false
rust:
- 1.4.0
- 1.5.0
- stable
- beta
- nightly
Expand Down
13 changes: 4 additions & 9 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ use std::os::unix::prelude::*;
use std::io::{self, SeekFrom};
use std::fs;
use std::fs::File;
use std::path::{Path, PathBuf};
use std::path::Path;

mod error;
pub use error::Error;
Expand Down Expand Up @@ -149,21 +149,16 @@ impl Pin {
/// a path starting with /sys/class/gpioXXX), then this function
/// will return an error.
pub fn from_path<T: AsRef<Path>>(path: T) -> Result<Pin> {
// TODO: use something like realpath once available.
// See https://github.com/rust-lang/rfcs/issues/939 and
// https://github.com/rust-lang/rust/issues/11857
let mut pb = PathBuf::from(path.as_ref());
while try!(fs::metadata(&pb)).file_type().is_symlink() {
pb = try!(fs::read_link(pb));
}
// Resolve all symbolic links in the provided path
let pb = try!(fs::canonicalize(path.as_ref()));

// determine if this is valid and figure out the pin_num
if !try!(fs::metadata(&pb)).is_dir() {
return Err(Error::Unexpected(format!("Provided path not a directory or symlink to \
a directory")));
}

let re = regex::Regex::new(r"^/sys/class/gpio/gpio(\d+)$").unwrap();
let re = regex::Regex::new(r"^/sys/.*?/gpio/gpio(\d+)$").unwrap();
let caps = match re.captures(pb.to_str().unwrap_or("")) {
Some(cap) => cap,
None => return Err(Error::InvalidPath(format!("{:?}", pb))),
Expand Down

0 comments on commit f1316d6

Please sign in to comment.