Skip to content

Commit

Permalink
fix to work with nix instead of ioctl crate, clean up debugging
Browse files Browse the repository at this point in the history
  • Loading branch information
jamespharaoh committed Jul 4, 2017
1 parent 6126012 commit b3863ba
Show file tree
Hide file tree
Showing 13 changed files with 259 additions and 113 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@

crc = "1.4"
flate2 = "0.2"
ioctl = "0.3"
lazy_static = "0.2"
libc = "0.2"
minilzo = "0.1"
nix = "*"
uuid = "0.3"

# noet ts=4 filetype=toml
12 changes: 0 additions & 12 deletions src/compress/lzo.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
use std::io;
use std::io::Write;

use libc;

use std::mem;
Expand Down Expand Up @@ -106,9 +103,6 @@ pub fn decompress (
output_size: usize,
) -> Result <Vec <u8>, String> {

io::stderr ().write_all (
b"LZO\r\n");

initialise ().unwrap_or_else (
|error|

Expand All @@ -117,9 +111,6 @@ pub fn decompress (

);

io::stderr ().write_all (
b"LZO2\r\n");

let mut output =
Vec::new ();

Expand All @@ -145,9 +136,6 @@ pub fn decompress (
output_len as usize,
0u8);

io::stderr ().write_all (
b"LZO3\r\n");

match result {

LZO_E_OK =>
Expand Down
9 changes: 0 additions & 9 deletions src/diskformat/item/extent_data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
use std::borrow::Cow;
use std::io;
use std::io::Write;
use std::mem;

use flate2;
Expand Down Expand Up @@ -232,17 +230,10 @@ impl <'a> BtrfsExtentData <'a> {
).map (
|uncompressed_data|

{
io::stdout ().write_fmt (
format_args! (
"LZO SUCCESS {} -> {} bytes\r\n",
raw_data.len (),
uncompressed_data.len ()));
Ok (Some (
Cow::Owned (
uncompressed_data)
))
}

).or_else (
|error|
Expand Down
182 changes: 182 additions & 0 deletions src/diskformat/item/root_item.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
use std::fmt;
use std::mem;

use diskformat::*;

#[ derive (Copy, Clone, Eq, Hash, Ord, PartialEq, PartialOrd) ]
pub struct BtrfsRootItem <'a> {
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
}

#[ repr (C, packed) ]
#[ derive (Copy, Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd) ]
pub struct BtrfsRootItemData {
inode_item: BtrfsInodeItemData,
expected_generation: u64,
root_object_id: u64,
root_node_block_number: u64,
byte_limit: u64,
bytes_used: u64,
last_snapshot_generation: u64,
flags: u64,
num_references: u32,
drop_progress: BtrfsKey,
drop_level: u8,
tree_level: u8,
generation_v2: u64,
subvolume_uuid: BtrfsUuid,
parent_uuid: BtrfsUuid,
received_uuid: BtrfsUuid,
changed_transaction_id: u64,
created_transaction_id: u64,
sent_transaction_id: u64,
received_transaction_id: u64,
changed_time: [u8; 0xc],
created_time: [u8; 0xc],
sent_time: [u8; 0xc],
received_time: [u8; 0xc],
reserved: [u64; 0x8],
}

impl <'a> BtrfsRootItem <'a> {

pub fn from_bytes (
header: & 'a BtrfsLeafItemHeader,
data_bytes: & 'a [u8],
) -> Result <BtrfsRootItem <'a>, String> {

// sanity check

if data_bytes.len () != mem::size_of::<BtrfsRootItemData> () {

return Err (
format! (
"Must be at least 0x{:x} bytes",
mem::size_of::<BtrfsRootItemData> ()));

}

// create root item

let root_item = BtrfsRootItem {
header: header,
data_bytes: data_bytes,
};

// return

Ok (root_item)

}

pub fn header (& self) -> & BtrfsLeafItemHeader {
self.header
}

pub fn key (& self) -> BtrfsKey {
self.header.key ()
}

pub fn object_id (& self) -> u64 {
self.header.object_id ()
}

pub fn data (& self) -> & BtrfsRootItemData {

unsafe {
& * (
self.data_bytes.as_ptr ()
as * const BtrfsRootItemData
)
}

}

pub fn inode_item (& self) -> & BtrfsInodeItemData {
& self.data ().inode_item
}

pub fn expected_generation (& self) -> u64 {
self.data ().expected_generation
}

pub fn root_object_id (& self) -> u64 {
self.data ().root_object_id
}

pub fn root_node_block_number (& self) -> u64 {
self.data ().root_node_block_number
}

pub fn byte_limit (& self) -> u64 {
self.data ().byte_limit
}

pub fn bytes_used (& self) -> u64 {
self.data ().bytes_used
}

pub fn last_snapshot_generation (& self) -> u64 {
self.data ().last_snapshot_generation
}

pub fn flags (& self) -> u64 {
self.data ().flags
}

pub fn num_references (& self) -> u32 {
self.data ().num_references
}

pub fn drop_progress (& self) -> BtrfsKey {
self.data ().drop_progress
}

pub fn drop_level (& self) -> u8 {
self.data ().drop_level
}

pub fn tree_level (& self) -> u8 {
self.data ().tree_level
}

}

impl <'a> fmt::Debug for BtrfsRootItem <'a> {

fn fmt (
& self,
formatter: & mut fmt::Formatter,
) -> Result <(), fmt::Error> {

write! (
formatter,
"BtrfsRootItem: {{ header: ",
) ?;

self.header.fmt (
formatter,
) ?;

write! (
formatter,
", ",
) ?;

self.data ().fmt (
formatter,
) ?;

write! (
formatter,
" }}",
) ?;

Ok (())

}

}

// ex: noet ts=4 filetype=rust
4 changes: 2 additions & 2 deletions src/diskformat/superblock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ pub struct BtrfsSuperblock {
}

pub struct BtrfsSuperblockSystemChunks <'a> {
superblock: & 'a BtrfsSuperblock,
_superblock: & 'a BtrfsSuperblock,
address: * const u8,
end_address: * const u8,
}
Expand All @@ -63,7 +63,7 @@ impl BtrfsSuperblock {
};

BtrfsSuperblockSystemChunks {
superblock: self,
_superblock: self,
address: start_address,
end_address: end_address,
}
Expand Down
1 change: 0 additions & 1 deletion src/diskformat/tree/root_tree.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use std::collections::BTreeMap;
use std::collections::HashMap;

use diskformat::*;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@
#![ allow (unused_parens) ]

#[ macro_use ]
extern crate ioctl;
extern crate lazy_static;

#[ macro_use ]
extern crate lazy_static;
extern crate nix;

extern crate crc;
extern crate libc;
Expand Down
2 changes: 0 additions & 2 deletions src/linux/ctypes/ioctl_constants.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use linux::imports::*;

pub const UUID_SIZE: usize = 16;
pub const DEVICE_PATH_NAME_MAX: usize = 1024;

Expand Down
17 changes: 7 additions & 10 deletions src/linux/operations/deduplicate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,23 +99,20 @@ pub fn deduplicate_range (

// perform ioctl

let file_dedupe_range_result = unsafe {
unsafe {

ioctl_file_dedupe_range (
file_descriptor,
c_dedupe_range)

};
}.map_err (
|error|

if file_dedupe_range_result != 0 {
format! (
"Dedupe ioctl returned {}",
error)

return Err (
format! (
"Dedupe ioctl returned {}",
file_dedupe_range_result)
);

}
) ?;

// decode c result

Expand Down
16 changes: 8 additions & 8 deletions src/linux/operations/defragment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,21 +70,21 @@ pub fn defragment_range (

// call ioctl

let ioctl_result = unsafe {
unsafe {

ioctl_defrag_range (
file_descriptor,
& defrag_range_args as * const IoctlDefragRangeArgs,
)
};

if ioctl_result != 0 {
}.map_err (
|error|

return Err (
format! (
"Defragment IOCTL returned {}",
ioctl_result));
format! (
"Defragment IOCTL returned {}",
error)

}
) ?;

// return ok

Expand Down
16 changes: 7 additions & 9 deletions src/linux/operations/fiemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,22 +169,20 @@ fn get_c_file_extent_map (
c_fiemap_info.extent_count = extent_count;
c_fiemap_info.flags = 0; //FIEMAP_FLAG_SYNC;

let fiemap_result =
unsafe {
unsafe {

ioctl_fiemap (
file_descriptor,
c_fiemap_info as * mut IoctlFiemap)

};

if fiemap_result != 0 {
}.map_err (
|error|

return Err (
"Error getting file extent map".to_string ()
);
format! (
"Error getting file extent map: {}",
error)

}
) ?;

// return

Expand Down
Loading

0 comments on commit b3863ba

Please sign in to comment.