Skip to content

Commit e8927aa

Browse files
Minor improvements from testing against NFSv4
1 parent 9c805cb commit e8927aa

File tree

6 files changed

+35
-9
lines changed

6 files changed

+35
-9
lines changed

__test__/index.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,17 @@ ava_1.default.serial('should succeed when seeking and writing string object via
10621062
t.is(text, 'hello world');
10631063
await rootHandle.removeEntry(fileHandle.name);
10641064
});
1065+
ava_1.default.serial('should succeed when seeking beyond file size and writing string via write', async (t) => {
1066+
const rootHandle = getRootHandle();
1067+
const fileHandle = await rootHandle.getFileHandle('writable-seek-and-write-string-via-write', { create: true });
1068+
const writable = await fileHandle.createWritable();
1069+
await t.notThrowsAsync(writable.write({ type: 'write', position: 4, data: 'abc' }));
1070+
const file = await fileHandle.getFile();
1071+
t.is(file.size, 7);
1072+
const text = await file.text();
1073+
t.is(text, '\0\0\0\0abc');
1074+
await rootHandle.removeEntry(fileHandle.name);
1075+
});
10651076
ava_1.default.serial('should succeed when truncating size', async (t) => {
10661077
const rootHandle = getRootHandle();
10671078
const fileHandle = await rootHandle.getFileHandle('writable-truncate', { create: true });

__test__/index.spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1160,6 +1160,18 @@ test.serial('should succeed when seeking and writing string object via write', a
11601160
await rootHandle.removeEntry(fileHandle.name);
11611161
})
11621162

1163+
test.serial('should succeed when seeking beyond file size and writing string via write', async (t) => {
1164+
const rootHandle = getRootHandle();
1165+
const fileHandle = await rootHandle.getFileHandle('writable-seek-and-write-string-via-write', {create: true}) as NfsFileHandle;
1166+
const writable = await fileHandle.createWritable();
1167+
await t.notThrowsAsync(writable.write({type: 'write', position: 4, data: 'abc'}));
1168+
const file = await fileHandle.getFile();
1169+
t.is(file.size, 7);
1170+
const text = await file.text();
1171+
t.is(text, '\0\0\0\0abc');
1172+
await rootHandle.removeEntry(fileHandle.name);
1173+
})
1174+
11631175
test.serial('should succeed when truncating size', async (t) => {
11641176
const rootHandle = getRootHandle();
11651177
const fileHandle = await rootHandle.getFileHandle('writable-truncate', {create: true}) as NfsFileHandle;

setup-nfs-testdir.sh

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ if [ -z $NFS_GID ]; then
2828
NFS_GID=nogroup
2929
fi
3030

31+
rm -rf ${NFS_TEST_DIR}/*
3132
mkdir -p ${NFS_TEST_DIR}/first ${NFS_TEST_DIR}/quatre
3233
echo -n "In order to make sure that this file is exactly 123 bytes in size, I have written this text while watching its chars count." > ${NFS_TEST_DIR}/annar
3334
touch ${NFS_TEST_DIR}/3 ${NFS_TEST_DIR}/first/comment ${NFS_TEST_DIR}/quatre/points

src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ pub struct JsNfsHandle {
339339
impl JsNfsHandle {
340340

341341
pub fn open(url: String) -> Self {
342-
Self{nfs: Some(Arc::new(RwLock::new(nfs::connect(url)))), path: DIR_ROOT.into(), kind: KIND_DIRECTORY.into(), name: DIR_ROOT.into()}
342+
let my_nfs = nfs::connect(url).unwrap_or_else(|e| panic!("error opening connection to NFS server: {:?}", e));
343+
Self{nfs: Some(Arc::new(RwLock::new(my_nfs))), path: DIR_ROOT.into(), kind: KIND_DIRECTORY.into(), name: DIR_ROOT.into()}
343344
}
344345

345346
fn is_same(&self, other: &JsNfsHandle) -> bool {
@@ -652,7 +653,8 @@ impl JsNfsFileHandle {
652653
let nfs = &self.handle.nfs;
653654
let my_nfs = nfs.as_ref().unwrap().write().unwrap();
654655
let nfs_stat = my_nfs.stat64(self.handle.path.as_str())?;
655-
Ok(JsNfsFile{handle: self.handle.clone(), size: nfs_stat.size as i64, type_, last_modified: (nfs_stat.mtime * 1000) as i64, name: self.name.clone()})
656+
let last_modified = (nfs_stat.mtime as i64).checked_mul(1000).unwrap_or(nfs_stat.mtime as i64);
657+
Ok(JsNfsFile{handle: self.handle.clone(), size: nfs_stat.size as i64, type_, last_modified, name: self.name.clone()})
656658
}
657659

658660
#[napi]

src/nfs/libnfs.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@ pub(super) struct NFS3 {
2828
}
2929

3030
impl NFS3 {
31-
pub(super) fn connect(url: String) -> Box<dyn NFS> {
32-
let mut nfs = Nfs::new().unwrap();
33-
let _ = nfs.parse_url_mount(url.as_str()).unwrap();
34-
Box::new(NFS3{nfs: Arc::new(RwLock::new(nfs))})
31+
pub(super) fn connect(url: String) -> Result<Box<dyn NFS>> {
32+
let mut nfs = Nfs::new()?;
33+
let _ = nfs.parse_url_mount(url.as_str())?;
34+
Ok(Box::new(NFS3{nfs: Arc::new(RwLock::new(nfs))}))
3535
}
3636
}
3737

src/nfs/mod.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,11 @@ pub struct NFSStat64 {
122122
pub ctime_nsec: u64,
123123
}
124124

125-
pub(crate) fn connect(url: String) -> Box<dyn NFS> {
125+
pub(crate) fn connect(url: String) -> Result<Box<dyn NFS>> {
126126
if std::env::var("TEST_USING_MOCKS").is_ok() {
127-
mock::NFS3::connect(url)
127+
Ok(mock::NFS3::connect(url))
128128
} else if std::env::var("TEST_USING_PURE_RUST").is_ok() {
129-
nfs_rs::NFS3::connect(url)
129+
Ok(nfs_rs::NFS3::connect(url))
130130
} else {
131131
libnfs::NFS3::connect(url)
132132
}

0 commit comments

Comments
 (0)