From 447787ab7ceeb087e6854a139fe6bf7507289629 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Thu, 24 Jan 2019 10:43:30 +0100 Subject: [PATCH] tools.rs: implement file_read_firstline --- src/api2/node/time.rs | 10 +--------- src/tools.rs | 17 +++++++++++++++++ 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/api2/node/time.rs b/src/api2/node/time.rs index b5f8a860..7a31fbd2 100644 --- a/src/api2/node/time.rs +++ b/src/api2/node/time.rs @@ -9,15 +9,7 @@ use chrono::prelude::*; fn read_etc_localtime() -> Result { - let file = std::fs::File::open("/etc/timezone")?; - - use std::io::{BufRead, BufReader}; - - let mut reader = BufReader::new(file); - - let mut line = String::new(); - - let _ = reader.read_line(&mut line)?; + let line = tools::file_read_firstline("/etc/timezone")?; Ok(line.trim().to_owned()) } diff --git a/src/tools.rs b/src/tools.rs index 3c3fe2ec..6be03a80 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -59,6 +59,23 @@ pub fn map_struct_mut(buffer: &mut [u8]) -> Result<&mut T, Error> { Ok(unsafe { &mut * (buffer.as_ptr() as *mut T) }) } +pub fn file_read_firstline>(path: P) -> Result { + + let path = path.as_ref(); + + let file = std::fs::File::open(path)?; + + use std::io::{BufRead, BufReader}; + + let mut reader = BufReader::new(file); + + let mut line = String::new(); + + let _ = reader.read_line(&mut line)?; + + Ok(line) +} + /// Atomically write a file. We first create a temporary file, which /// is then renamed. pub fn file_set_contents>(