Skip to content

Commit 783bffa

Browse files
authored
Adapt to new jbk SmallVec (#103)
2 parents 9b05e05 + dd0fdca commit 783bffa

File tree

8 files changed

+61
-46
lines changed

8 files changed

+61
-46
lines changed

Cargo.lock

+3-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

arx/src/light_path.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use std::io::Write;
22

33
#[derive(Clone, Debug)]
4-
pub struct LightPath(Vec<Vec<u8>>);
4+
pub struct LightPath(Vec<jbk::SmallBytes>);
55

66
impl LightPath {
77
pub fn new() -> Self {
88
Self(Vec::with_capacity(10))
99
}
1010

11-
pub fn push(&mut self, component: Vec<u8>) {
11+
pub fn push(&mut self, component: jbk::SmallBytes) {
1212
self.0.push(component);
1313
}
1414

@@ -44,8 +44,8 @@ impl Default for LightPath {
4444
}
4545
}
4646

47-
impl From<Vec<u8>> for LightPath {
48-
fn from(s: Vec<u8>) -> Self {
47+
impl From<jbk::SmallBytes> for LightPath {
48+
fn from(s: jbk::SmallBytes) -> Self {
4949
let mut p = Self::new();
5050
p.push(s);
5151
p

arx/src/list.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::path::PathBuf;
1111
use anyhow::{anyhow, Context, Result};
1212
use clap::{Parser, ValueHint};
1313

14-
type Path = Vec<u8>;
14+
type Path = jbk::SmallBytes;
1515

1616
struct PathBuilder {
1717
path_property: jbk::reader::builder::ArrayProperty,
@@ -28,7 +28,7 @@ impl arx::Builder for PathBuilder {
2828

2929
fn create_entry(&self, _idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
3030
let path_prop = self.path_property.create(reader)?;
31-
let mut path = vec![];
31+
let mut path = jbk::SmallBytes::new();
3232
path_prop.resolve_to_vec(&mut path)?;
3333
Ok(path)
3434
}

libarx/src/arx_fs.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl LightLinkBuilder {
9393
}
9494

9595
impl jbk::reader::builder::BuilderTrait for LightLinkBuilder {
96-
type Entry = Vec<u8>;
96+
type Entry = jbk::SmallBytes;
9797
type Error = FsError;
9898

9999
fn create_entry(&self, idx: jbk::EntryIdx) -> Result<Option<Self::Entry>, Self::Error> {
@@ -102,7 +102,7 @@ impl jbk::reader::builder::BuilderTrait for LightLinkBuilder {
102102
.map(|reader| match self.variant_id_property.create(&reader)? {
103103
Some(EntryType::Link) => {
104104
let target = self.link_property.create(&reader)?;
105-
let mut vec = Vec::new();
105+
let mut vec = jbk::SmallBytes::new();
106106
target.resolve_to_vec(&mut vec)?;
107107
Ok(vec)
108108
}
@@ -199,7 +199,7 @@ impl jbk::reader::builder::BuilderTrait for LightDirBuilder {
199199

200200
struct LightCommonPath {
201201
file_type: Option<EntryType>,
202-
path: Vec<u8>,
202+
path: jbk::SmallBytes,
203203
}
204204

205205
struct LightCommonPathBuilder {
@@ -227,7 +227,7 @@ impl jbk::reader::builder::BuilderTrait for LightCommonPathBuilder {
227227
.get_entry_reader(idx)
228228
.map(|reader| {
229229
let path_prop = self.path_property.create(&reader)?;
230-
let mut path = vec![];
230+
let mut path = jbk::SmallBytes::new();
231231
path_prop.resolve_to_vec(&mut path)?;
232232
let file_type = self.variant_id_property.create(&reader)?;
233233
Ok(LightCommonPath { file_type, path })
@@ -326,7 +326,7 @@ impl jbk::reader::builder::BuilderTrait for AttrBuilder {
326326
match link.size() {
327327
Some(s) => s as u64,
328328
None => {
329-
let mut vec = Vec::new();
329+
let mut vec = jbk::SmallBytes::new();
330330
link.resolve_to_vec(&mut vec)?;
331331
vec.len() as u64
332332
}
@@ -779,7 +779,7 @@ impl<'a, S: Stats> fuser::Filesystem for ArxFs<'a, S> {
779779
// We remove "." and ".."
780780
let entry_idx = range.offset() + jbk::EntryIdx::from(i as u32 - 2);
781781
let entry_ino = Ino::from(entry_idx);
782-
let entry_path = OsString::from_vec(entry.path);
782+
let entry_path = OsString::from_vec(entry.path.to_vec());
783783
let should_break = reply.add(
784784
entry_ino.get(),
785785
/* offset =*/ i,

libarx/src/create/entry_store_creator.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,10 @@ impl DirEntry {
234234
)
235235
.into());
236236
}
237-
values.insert(Property::Target, jbk::Value::Array(target.into()));
237+
values.insert(
238+
Property::Target,
239+
jbk::Value::Array(Vec::from(target).into()),
240+
);
238241
let entry = Box::new(jbk::creator::BasicEntry::new_from_schema(
239242
&entry_store.schema,
240243
Some(EntryType::Link),

libarx/src/entry.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use jbk::reader::ByteSlice;
55
#[derive(Clone)]
66
pub struct CommonPart {
77
idx: jbk::EntryIdx,
8-
path: Vec<u8>,
8+
path: jbk::SmallBytes,
99
parent: Option<jbk::EntryIdx>,
1010
owner: u32,
1111
group: u32,
@@ -18,7 +18,7 @@ pub trait CommonEntry {
1818
fn idx(&self) -> jbk::EntryIdx {
1919
self.common().idx
2020
}
21-
fn path(&self) -> &Vec<u8> {
21+
fn path(&self) -> &[u8] {
2222
&self.common().path
2323
}
2424
fn parent(&self) -> Option<jbk::EntryIdx> {
@@ -63,7 +63,7 @@ impl FileEntry {
6363
#[derive(Clone)]
6464
pub struct Link {
6565
common: CommonPart,
66-
target: Vec<u8>,
66+
target: jbk::SmallBytes,
6767
}
6868

6969
impl CommonEntry for Link {
@@ -73,7 +73,7 @@ impl CommonEntry for Link {
7373
}
7474

7575
impl Link {
76-
pub fn target(&self) -> &Vec<u8> {
76+
pub fn target(&self) -> &[u8] {
7777
&self.target
7878
}
7979
}
@@ -121,7 +121,7 @@ mod private {
121121

122122
fn create_entry(&self, idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<CommonPart> {
123123
let path_prop = self.path_property.create(reader)?;
124-
let mut path = vec![];
124+
let mut path = jbk::SmallBytes::new();
125125
path_prop.resolve_to_vec(&mut path)?;
126126
let parent = self.parent_prorperty.create(reader)?;
127127
let parent = if parent == 0 {
@@ -185,7 +185,7 @@ mod private {
185185
fn create_entry(&self, idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
186186
let common = self.common.create_entry(idx, reader)?;
187187
let target_prop = self.link_property.create(reader)?;
188-
let mut target = vec![];
188+
let mut target = jbk::SmallBytes::new();
189189
target_prop.resolve_to_vec(&mut target)?;
190190
Ok(Link { common, target })
191191
}

libarx/src/tools.rs

+30-20
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::convert::TryInto;
12
use std::collections::HashSet;
23
use std::fs::{create_dir_all, OpenOptions};
34
use std::io::{ErrorKind, Write};
@@ -16,14 +17,14 @@ use jbk::reader::MayMissPack;
1617
use std::sync::{Arc, OnceLock};
1718

1819
struct FileEntry {
19-
path: String,
20+
path: jbk::SmallString,
2021
content: jbk::ContentAddress,
2122
mtime: u64,
2223
}
2324

2425
struct Link {
25-
path: String,
26-
target: String,
26+
path: jbk::SmallString,
27+
target: jbk::SmallString,
2728
mtime: u64,
2829
}
2930

@@ -46,12 +47,12 @@ impl Builder for FileBuilder {
4647

4748
fn create_entry(&self, _idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
4849
let path_prop = self.path_property.create(reader)?;
49-
let mut path = vec![];
50+
let mut path = jbk::SmallBytes::new();
5051
path_prop.resolve_to_vec(&mut path)?;
5152
let content = self.content_address_property.create(reader)?;
5253
let mtime = self.mtime_property.create(reader)?;
5354
Ok(FileEntry {
54-
path: String::from_utf8(path)?,
55+
path: path.try_into()?,
5556
content,
5657
mtime,
5758
})
@@ -77,16 +78,16 @@ impl Builder for LinkBuilder {
7778

7879
fn create_entry(&self, _idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
7980
let path_prop = self.path_property.create(reader)?;
80-
let mut path = vec![];
81+
let mut path = jbk::SmallBytes::new();
8182
path_prop.resolve_to_vec(&mut path)?;
8283

8384
let target_prop = self.link_property.create(reader)?;
84-
let mut target = vec![];
85+
let mut target = jbk::SmallBytes::new();
8586
target_prop.resolve_to_vec(&mut target)?;
8687
let mtime = self.mtime_property.create(reader)?;
8788
Ok(Link {
88-
path: String::from_utf8(path)?,
89-
target: String::from_utf8(target)?,
89+
path: path.try_into()?,
90+
target: target.try_into()?,
9091
mtime,
9192
})
9293
}
@@ -97,7 +98,7 @@ struct DirBuilder {
9798
}
9899

99100
impl Builder for DirBuilder {
100-
type Entry = String;
101+
type Entry = jbk::SmallString;
101102

102103
fn new(properties: &AllProperties) -> Self {
103104
Self {
@@ -107,9 +108,9 @@ impl Builder for DirBuilder {
107108

108109
fn create_entry(&self, _idx: jbk::EntryIdx, reader: &ByteSlice) -> jbk::Result<Self::Entry> {
109110
let path_prop = self.path_property.create(reader)?;
110-
let mut path = vec![];
111+
let mut path = jbk::SmallBytes::new();
111112
path_prop.resolve_to_vec(&mut path)?;
112-
Ok(String::from_utf8(path)?)
113+
Ok(path.try_into()?)
113114
}
114115
}
115116

@@ -196,9 +197,9 @@ where
196197
fn on_directory_enter(
197198
&self,
198199
current_path: &mut crate::PathBuf,
199-
path: &String,
200+
path: &jbk::SmallString,
200201
) -> Result<bool, ExtractError> {
201-
current_path.push(path);
202+
current_path.push(path.as_str());
202203
if !self.should_extract(current_path, true) {
203204
return Ok(false);
204205
}
@@ -212,7 +213,7 @@ where
212213
fn on_directory_exit(
213214
&self,
214215
current_path: &mut crate::PathBuf,
215-
_path: &String,
216+
_path: &jbk::SmallString,
216217
) -> Result<(), ExtractError> {
217218
current_path.pop();
218219
Ok(())
@@ -224,7 +225,7 @@ where
224225
entry: &FileEntry,
225226
) -> Result<(), ExtractError> {
226227
let mut current_path = current_path.clone();
227-
current_path.push(&entry.path);
228+
current_path.push(entry.path.as_str());
228229
let entry_content = entry.content;
229230
let abs_path = self.abs_path(&current_path);
230231
let print_progress = self.print_progress;
@@ -323,13 +324,16 @@ where
323324

324325
fn on_link(&self, current_path: &mut crate::PathBuf, link: &Link) -> Result<(), ExtractError> {
325326
let mut current_path = current_path.clone();
326-
current_path.push(&link.path);
327+
current_path.push(link.path.as_str());
327328
if !self.should_extract(&current_path, false) {
328329
current_path.pop();
329330
return Ok(());
330331
}
331332
let abs_path = self.abs_path(&current_path);
332-
if let Err(e) = symlink(PathBuf::from(&link.target), PathBuf::from(&abs_path)) {
333+
if let Err(e) = symlink(
334+
PathBuf::from(link.target.as_str()),
335+
PathBuf::from(&abs_path),
336+
) {
333337
match e.kind() {
334338
ErrorKind::AlreadyExists => match self.overwrite {
335339
Overwrite::Skip => return Ok(()),
@@ -343,14 +347,20 @@ where
343347
let new_time = SystemTime::UNIX_EPOCH + Duration::from_secs(link.mtime);
344348
if new_time >= existing_time {
345349
std::fs::remove_file(&abs_path)?;
346-
symlink(PathBuf::from(&link.target), PathBuf::from(&abs_path))?;
350+
symlink(
351+
PathBuf::from(link.target.as_str()),
352+
PathBuf::from(&abs_path),
353+
)?;
347354
} else {
348355
return Ok(());
349356
}
350357
}
351358
Overwrite::Overwrite => {
352359
std::fs::remove_file(&abs_path)?;
353-
symlink(PathBuf::from(&link.target), PathBuf::from(&abs_path))?;
360+
symlink(
361+
PathBuf::from(link.target.as_str()),
362+
PathBuf::from(&abs_path),
363+
)?;
354364
}
355365
Overwrite::Error => return Err(e.into()),
356366
},

python/src/entry.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,13 @@ impl Entry {
2828
fn __repr__(&self) -> String {
2929
match &self.entry {
3030
arx::Entry::File(e) => {
31-
format!("File({})", String::from_utf8(e.path().clone()).unwrap())
31+
format!("File({})", String::from_utf8(e.path().to_vec()).unwrap())
3232
}
3333
arx::Entry::Link(e) => {
34-
format!("Link({})", String::from_utf8(e.path().clone()).unwrap())
34+
format!("Link({})", String::from_utf8(e.path().to_vec()).unwrap())
3535
}
3636
arx::Entry::Dir(_, e) => {
37-
format!("Dir({})", String::from_utf8(e.path().clone()).unwrap())
37+
format!("Dir({})", String::from_utf8(e.path().to_vec()).unwrap())
3838
}
3939
}
4040
}
@@ -53,9 +53,9 @@ impl Entry {
5353
#[getter]
5454
fn path(&self) -> PyResult<String> {
5555
Ok(match &self.entry {
56-
arx::Entry::File(e) => String::from_utf8(e.path().clone()).unwrap(),
57-
arx::Entry::Link(e) => String::from_utf8(e.path().clone()).unwrap(),
58-
arx::Entry::Dir(_, e) => String::from_utf8(e.path().clone()).unwrap(),
56+
arx::Entry::File(e) => String::from_utf8(e.path().to_vec()).unwrap(),
57+
arx::Entry::Link(e) => String::from_utf8(e.path().to_vec()).unwrap(),
58+
arx::Entry::Dir(_, e) => String::from_utf8(e.path().to_vec()).unwrap(),
5959
})
6060
}
6161

0 commit comments

Comments
 (0)