-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #49 from Kijewski/pr-rel-link
"/etc/localtime" may be relative link
- Loading branch information
Showing
2 changed files
with
36 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,25 @@ | ||
use std::fs::read_link; | ||
|
||
const PREFIX: &str = "/usr/share/zoneinfo/"; | ||
|
||
pub(crate) fn get_timezone_inner() -> Result<String, crate::GetTimezoneError> { | ||
// see https://www.cyberciti.biz/faq/openbsd-time-zone-howto/ | ||
|
||
// This is a backport of the Linux implementation. | ||
// NetBSDs is less than thorough how the softlink should be set up. | ||
|
||
const PREFIXES: &[&str] = &[ | ||
"/usr/share/zoneinfo/", // absolute path | ||
"../usr/share/zoneinfo/", // relative path | ||
]; | ||
let mut s = read_link("/etc/localtime")? | ||
.into_os_string() | ||
.into_string() | ||
.map_err(|_| crate::GetTimezoneError::FailedParsingString)?; | ||
if !s.starts_with(PREFIX) { | ||
return Err(crate::GetTimezoneError::FailedParsingString); | ||
for &prefix in PREFIXES { | ||
if s.starts_with(prefix) { | ||
// Trim to the correct length without allocating. | ||
s.replace_range(..prefix.len(), ""); | ||
return Ok(s); | ||
} | ||
} | ||
|
||
// Trim to the correct length without allocating. | ||
s.replace_range(..PREFIX.len(), ""); | ||
Ok(s) | ||
Err(crate::GetTimezoneError::FailedParsingString) | ||
} |