#![cfg(target_os = "linux")]
use byteorder::{BigEndian, ReadBytesExt};
use error::{RadosError, RadosResult};
use std::io::{Cursor, Read, Write};
use std::net::Shutdown;
use std::os::unix::net::UnixStream;
use std::str;
pub fn admin_socket_command(cmd: &str, socket: &str) -> RadosResult<String> {
let raw_cmd = json!({
"prefix": cmd,
});
admin_socket_raw_command(&raw_cmd.to_string(), socket)
}
#[allow(unused_variables)]
pub fn admin_socket_raw_command(cmd: &str, socket: &str) -> RadosResult<String> {
let mut buffer = vec![0; 4]; let cmd = &format!("{}\0", cmd);
let mut stream = try!(UnixStream::connect(socket));
let wb = try!(stream.write(cmd.as_bytes()));
let ret_val = try!(stream.read(&mut buffer));
if ret_val < 4 {
try!(stream.shutdown(Shutdown::Both));
return Err(RadosError::new("Admin socket: Invalid command or socket did not return any data".to_string()));
}
let mut rdr = Cursor::new(buffer);
let len = rdr.read_u32::<BigEndian>()?;
let mut output_buffer = vec![0; len as usize];
stream.read_exact(&mut output_buffer)?;
stream.shutdown(Shutdown::Both)?;
Ok(String::from_utf8_lossy(&output_buffer).into_owned())
}