1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
use std::ffi::{CString, CStr};
use std::str;
use libc::{c_char, c_int};

use err::HdfsErr;
use native::*;
use dfs::HdfsFs;

pub fn str_to_chars(s: &str) -> *const c_char {
  CString::new(s.as_bytes()).unwrap().as_ptr()
}

pub fn chars_to_str<'a>(chars: *const c_char) -> &'a str {
  let slice = unsafe { CStr::from_ptr(chars) }.to_bytes();
  str::from_utf8(slice).unwrap()
}

pub fn bool_to_c_int(val: bool) -> c_int {
  if val { 1 } else { 0 }
}

/// Hdfs Utility
pub struct HdfsUtil;

/// HDFS Utility
impl HdfsUtil {

  /// Copy file from one filesystem to another.
  ///
  /// #### Params
  /// * ```srcFS``` - The handle to source filesystem.
  /// * ```src``` - The path of source file.
  /// * ```dstFS``` - The handle to destination filesystem.
  /// * ```dst``` - The path of destination file.
  pub fn copy(src_fs: &HdfsFs, src: &str, dst_fs: &HdfsFs, dst: &str)
      -> Result<bool, HdfsErr> {

    let res = unsafe {
      hdfsCopy(src_fs.raw(), str_to_chars(src), dst_fs.raw(), str_to_chars(dst))
    };

    if res == 0 {
      Ok(true)
    } else {
      Err(HdfsErr::Unknown)
    }
  }

  /// Move file from one filesystem to another.
  ///
  /// #### Params
  /// * ```srcFS``` - The handle to source filesystem.
  /// * ```src``` - The path of source file.
  /// * ```dstFS``` - The handle to destination filesystem.
  /// * ```dst``` - The path of destination file.
  pub fn mv(src_fs: &HdfsFs, src: &str, dst_fs: &HdfsFs, dst: &str)
      -> Result<bool, HdfsErr> {

    let res = unsafe {
      hdfsMove(src_fs.raw(), str_to_chars(src), dst_fs.raw(), str_to_chars(dst))
    };

    if res == 0 {
      Ok(true)
    } else {
      Err(HdfsErr::Unknown)
    }
  }
}