Commit 1ec32c54 authored by Tigran Mkrtchyan's avatar Tigran Mkrtchyan
Browse files

nfsv3: merge NameUtils and HimeraNfsUtils into single class

Motivation:
There are no reasons to have to utility classes in a single package
(plus Himera vs Chimera)

Modification:
merge NameUtils and HimeraNfsUtils into single class Utils class,
including corresponding tests.

Result:
same code, less classes

Acked-by: Marina Sahakyan
Target: master
parent 235e266d
Pipeline #3440 passed with stage
in 1 minute and 32 seconds
/*
* Copyright (c) 2009 - 2014 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this program (see the file COPYING.LIB for more
* details); if not, write to the Free Software Foundation, Inc.,
* 675 Mass Ave, Cambridge, MA 02139, USA.
*/
package org.dcache.nfs.v3;
import org.dcache.nfs.status.AccessException;
import org.dcache.nfs.status.NameTooLongException;
/**
* Utility class to manipulate filename object.
*/
public class NameUtils {
private NameUtils() {}
/**
* Validate ${code filename} requirements.
* @param filename
* @throws AccessException if filename does not meet expected constrains
* @throws NameTooLongException if filename is longer than negotiated with PATHCONF operation.
*/
public static void checkFilename(String filename) throws AccessException, NameTooLongException {
// FIXME: merge with NFSv4 defaults
if (filename.length() > 256) {
throw new NameTooLongException();
}
if (filename.length() == 0 || filename.indexOf('/') != -1 || filename.indexOf('\0') != -1 ) {
throw new AccessException();
}
}
}
......@@ -141,9 +141,9 @@ import org.dcache.oncrpc4j.rpc.RpcCall;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import static org.dcache.nfs.v3.HimeraNfsUtils.defaultPostOpAttr;
import static org.dcache.nfs.v3.HimeraNfsUtils.defaultWccData;
import static org.dcache.nfs.v3.NameUtils.checkFilename;
import static org.dcache.nfs.v3.Utils.defaultPostOpAttr;
import static org.dcache.nfs.v3.Utils.defaultWccData;
import static org.dcache.nfs.v3.Utils.checkFilename;
import org.dcache.nfs.vfs.FsStat;
import org.dcache.nfs.vfs.Inode;
......@@ -208,7 +208,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
Inode inode = new Inode(arg1.object.data);
Stat objStat = fs.getattr(inode);
HimeraNfsUtils.fill_attributes(objStat, res.resok.obj_attributes.attributes);
Utils.fill_attributes(objStat, res.resok.obj_attributes.attributes);
int realAccess = fs.access(inode, arg1.access.value);
......@@ -248,7 +248,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.file_wcc.after.attributes_follow = true;
res.resok.file_wcc.after.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.file_wcc.after.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.file_wcc.after.attributes);
res.resok.file_wcc.before = new pre_op_attr();
res.resok.file_wcc.before.attributes_follow = false;
res.resok.verf = writeVerifier;
......@@ -324,7 +324,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.obj_attributes.attributes_follow = true;
res.resok.obj_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(inodeStat, res.resok.obj_attributes.attributes);
Utils.fill_attributes(inodeStat, res.resok.obj_attributes.attributes);
res.resok.obj = new post_op_fh3();
res.resok.obj.handle_follows = true;
res.resok.obj.handle = new nfs_fh3();
......@@ -339,7 +339,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
parentStat.setNlink(parentStat.getNlink() + 1);
parentStat.setMTime(now);
HimeraNfsUtils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
Utils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
res.resok.dir_wcc.before = new pre_op_attr();
res.resok.dir_wcc.before.attributes_follow = false;
......@@ -400,7 +400,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.obj_attributes.attributes_follow = true;
res.resok.obj_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
res.resok.properties = new uint32(nfs3_prot.FSF3_CANSETTIME |
nfs3_prot.FSF3_HOMOGENEOUS |
......@@ -445,7 +445,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
Inode inode = new Inode(arg1.fsroot.data);
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
} catch (Exception e) {
_log.error("FSSTAT", e);
......@@ -474,7 +474,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok = new GETATTR3resok();
res.resok.obj_attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes);
} catch (ChimeraNFSException e) {
res.status = e.getStatus();
......@@ -509,7 +509,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.file_attributes.attributes_follow = true;
res.resok.file_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(hlink), res.resok.file_attributes.attributes);
Utils.fill_attributes(fs.getattr(hlink), res.resok.file_attributes.attributes);
res.resok.linkdir_wcc = new wcc_data();
res.resok.linkdir_wcc.after = new post_op_attr();
......@@ -519,7 +519,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
// fake answer
parentStat.setNlink(parentStat.getNlink() + 1);
parentStat.setMTime(System.currentTimeMillis());
HimeraNfsUtils.fill_attributes(parentStat, res.resok.linkdir_wcc.after.attributes);
Utils.fill_attributes(parentStat, res.resok.linkdir_wcc.after.attributes);
res.resok.linkdir_wcc.before = new pre_op_attr();
res.resok.linkdir_wcc.before.attributes_follow = false;
......@@ -566,13 +566,13 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.obj_attributes.attributes_follow = true;
res.resok.obj_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
res.resok.dir_attributes = new post_op_attr();
res.resok.dir_attributes.attributes_follow = true;
res.resok.dir_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(parent), res.resok.dir_attributes.attributes);
Utils.fill_attributes(fs.getattr(parent), res.resok.dir_attributes.attributes);
} catch (ChimeraNFSException hne) {
_log.debug("lookup {}", hne.toString());
......@@ -627,7 +627,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.obj_attributes.attributes_follow = true;
res.resok.obj_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
res.resok.dir_wcc = new wcc_data();
res.resok.dir_wcc.after = new post_op_attr();
......@@ -637,7 +637,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
// fake answer
parentStat.setNlink(parentStat.getNlink() + 1);
parentStat.setMTime(System.currentTimeMillis());
HimeraNfsUtils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
Utils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
res.resok.dir_wcc.before = new pre_op_attr();
res.resok.dir_wcc.before.attributes_follow = false;
......@@ -736,7 +736,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.dir_attributes.attributes = new fattr3();
res.resok.cookieverf = new cookieverf3(directoryStream.getVerifier());
HimeraNfsUtils.fill_attributes(dirStat, res.resok.dir_attributes.attributes);
Utils.fill_attributes(dirStat, res.resok.dir_attributes.attributes);
int currcount = READDIRPLUS3RESOK_SIZE;
......@@ -761,7 +761,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
currentEntry.name_attributes = new post_op_attr();
currentEntry.name_attributes.attributes_follow = true;
currentEntry.name_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(le.getStat(), currentEntry.name_attributes.attributes);
Utils.fill_attributes(le.getStat(), currentEntry.name_attributes.attributes);
// check if writing this entry exceeds the count limit
int newSize = ENTRYPLUS3_SIZE + name.length() + currentEntry.name_handle.handle.data.length;
......@@ -842,7 +842,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.dir_attributes = new post_op_attr();
res.resok.dir_attributes.attributes_follow = true;
res.resok.dir_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(dirStat, res.resok.dir_attributes.attributes);
Utils.fill_attributes(dirStat, res.resok.dir_attributes.attributes);
res.resok.cookieverf = new cookieverf3(directoryStream.getVerifier());
......@@ -919,7 +919,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.symlink_attributes.attributes_follow = true;
res.resok.symlink_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.symlink_attributes.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.symlink_attributes.attributes);
res.status = nfsstat.NFS_OK;
......@@ -977,7 +977,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.file_attributes = new post_op_attr();
res.resok.file_attributes.attributes_follow = true;
res.resok.file_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(inodeStat, res.resok.file_attributes.attributes);
Utils.fill_attributes(inodeStat, res.resok.file_attributes.attributes);
} catch (ChimeraNFSException hne) {
res.status = hne.getStatus();
res.resfail = new READ3resfail();
......@@ -1019,7 +1019,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.dir_wcc.before = new pre_op_attr();
res.resok.dir_wcc.before.attributes_follow = true;
res.resok.dir_wcc.before.attributes = new wcc_attr();
HimeraNfsUtils.fill_attributes(parentStat, res.resok.dir_wcc.before.attributes);
Utils.fill_attributes(parentStat, res.resok.dir_wcc.before.attributes);
// correct parent modification time and nlink counter
......@@ -1029,7 +1029,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.dir_wcc.after = new post_op_attr();
res.resok.dir_wcc.after.attributes_follow = true;
res.resok.dir_wcc.after.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
Utils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
} catch (ChimeraNFSException hne) {
......@@ -1072,7 +1072,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.fromdir_wcc.after = new post_op_attr();
res.resok.fromdir_wcc.after.attributes_follow = true;
res.resok.fromdir_wcc.after.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(from), res.resok.fromdir_wcc.after.attributes);
Utils.fill_attributes(fs.getattr(from), res.resok.fromdir_wcc.after.attributes);
res.resok.fromdir_wcc.before = new pre_op_attr();
res.resok.fromdir_wcc.before.attributes_follow = false;
......@@ -1081,7 +1081,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.todir_wcc.after = new post_op_attr();
res.resok.todir_wcc.after.attributes_follow = true;
res.resok.todir_wcc.after.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(to), res.resok.todir_wcc.after.attributes);
Utils.fill_attributes(fs.getattr(to), res.resok.todir_wcc.after.attributes);
res.resok.todir_wcc.before = new pre_op_attr();
res.resok.todir_wcc.before.attributes_follow = false;
......@@ -1131,7 +1131,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.dir_wcc.before = new pre_op_attr();
res.resok.dir_wcc.before.attributes_follow = true;
res.resok.dir_wcc.before.attributes = new wcc_attr();
HimeraNfsUtils.fill_attributes(parentStat, res.resok.dir_wcc.before.attributes);
Utils.fill_attributes(parentStat, res.resok.dir_wcc.before.attributes);
res.resok.dir_wcc.after.attributes_follow = true;
res.resok.dir_wcc.after.attributes = new fattr3();
......@@ -1139,7 +1139,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
parentStat.setMTime(System.currentTimeMillis());
parentStat.setNlink(parentStat.getNlink() - 1);
HimeraNfsUtils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
Utils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
} catch (ChimeraNFSException hne) {
res.resfail = new RMDIR3resfail();
......@@ -1167,13 +1167,13 @@ public class NfsServerV3 extends nfs3_protServerStub {
Inode inode = new Inode(arg1.object.data);
sattr3 newAttr = arg1.new_attributes;
HimeraNfsUtils.set_sattr(inode, fs, newAttr);
Utils.set_sattr(inode, fs, newAttr);
res.resok = new SETATTR3resok();
res.resok.obj_wcc = new wcc_data();
res.resok.obj_wcc.after = new post_op_attr();
res.resok.obj_wcc.after.attributes_follow = true;
res.resok.obj_wcc.after.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.obj_wcc.after.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.obj_wcc.after.attributes);
res.resok.obj_wcc.before = new pre_op_attr();
res.resok.obj_wcc.before.attributes_follow = false;
......@@ -1214,7 +1214,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
Stat parentStat = fs.getattr(parent);
Inode inode = fs.symlink(parent, file, link, call$.getCredential().getSubject(), 777);
HimeraNfsUtils.set_sattr(inode, fs, linkAttr);
Utils.set_sattr(inode, fs, linkAttr);
res.resok = new SYMLINK3resok();
......@@ -1222,7 +1222,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.obj_attributes.attributes_follow = true;
res.resok.obj_attributes.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.obj_attributes.attributes);
res.resok.obj = new post_op_fh3();
res.resok.obj.handle_follows = true;
res.resok.obj.handle = new nfs_fh3();
......@@ -1237,7 +1237,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
parentStat.setNlink(parentStat.getNlink() + 1);
parentStat.setMTime(System.currentTimeMillis());
HimeraNfsUtils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
Utils.fill_attributes(parentStat, res.resok.dir_wcc.after.attributes);
res.resok.dir_wcc.before = new pre_op_attr();
res.resok.dir_wcc.before.attributes_follow = false;
......@@ -1285,7 +1285,7 @@ public class NfsServerV3 extends nfs3_protServerStub {
res.resok.file_wcc.after.attributes_follow = true;
res.resok.file_wcc.after.attributes = new fattr3();
HimeraNfsUtils.fill_attributes(fs.getattr(inode), res.resok.file_wcc.after.attributes);
Utils.fill_attributes(fs.getattr(inode), res.resok.file_wcc.after.attributes);
res.resok.file_wcc.before = new pre_op_attr();
res.resok.file_wcc.before.attributes_follow = false;
res.resok.committed = ret.getStabilityLevel().toStableHow();
......
......@@ -19,6 +19,8 @@
*/
package org.dcache.nfs.v3;
import org.dcache.nfs.status.AccessException;
import org.dcache.nfs.status.NameTooLongException;
import org.dcache.nfs.v3.xdr.fattr3;
import org.dcache.nfs.v3.xdr.fileid3;
import org.dcache.nfs.v3.xdr.ftype3;
......@@ -46,14 +48,14 @@ import java.io.IOException;
import java.util.concurrent.TimeUnit;
public class HimeraNfsUtils {
public class Utils {
private static final int MODE_MASK = 0770000;
private static final Logger _log = LoggerFactory.getLogger(HimeraNfsUtils.class);
private static final Logger _log = LoggerFactory.getLogger(Utils.class);
private HimeraNfsUtils() {
private Utils() {
// no instance allowed
}
......@@ -233,4 +235,23 @@ public class HimeraNfsUtils {
wccData.before = defaultPreOpAttr();
return wccData;
}
/**
* Validate ${code filename} requirements.
* @param filename
* @throws AccessException if filename does not meet expected constrains
* @throws NameTooLongException if filename is longer than negotiated with PATHCONF operation.
*/
public static void checkFilename(String filename) throws AccessException, NameTooLongException {
// FIXME: merge with NFSv4 defaults
if (filename.length() > 256) {
throw new NameTooLongException();
}
if (filename.length() == 0 || filename.indexOf('/') != -1 || filename.indexOf('\0') != -1 ) {
throw new AccessException();
}
}
}
package org.dcache.nfs.v3;
import org.dcache.nfs.v3.xdr.nfstime3;
import org.junit.Assert;
import org.junit.Test;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
public class HimeraNfsUtilsTest {
@Test
public void testConvertTimestamp() throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
long timestamp = dateFormat.parse("01/02/2003 04:06:06.789").getTime();
nfstime3 converted = HimeraNfsUtils.convertTimestamp(timestamp);
Assert.assertEquals(timestamp/1000, converted.seconds.value);
Assert.assertEquals(1000000 * (timestamp%1000), converted.nseconds.value);
long decoded = HimeraNfsUtils.convertTimestamp(converted);
Assert.assertEquals(timestamp, decoded);
}
}
/*
* Copyright (c) 2009 - 2014 Deutsches Elektronen-Synchroton,
* Copyright (c) 2009 - 2021 Deutsches Elektronen-Synchroton,
* Member of the Helmholtz Association, (DESY), HAMBURG, GERMANY
*
* This library is free software; you can redistribute it and/or modify
......@@ -21,15 +21,16 @@ package org.dcache.nfs.v3;
import org.dcache.nfs.status.AccessException;
import org.dcache.nfs.status.NameTooLongException;
import org.dcache.nfs.v3.xdr.nfstime3;
import org.junit.Assert;
import org.junit.Test;
import static org.dcache.nfs.v3.NameUtils.checkFilename;
import java.text.SimpleDateFormat;
import java.util.TimeZone;
/**
*
*/
public class NameUtilsTest {
import static org.dcache.nfs.v3.Utils.checkFilename;
public class UtilsTest {
private final static String GOOD_NAME = "someGoodName";
private final static String EMPTY_NAME = "";
......@@ -38,6 +39,17 @@ public class NameUtilsTest {
private final static String GOOD_UTF8_ARM = "Երեվան";
private final static String GOOD_UTF8_HBR = "יְרוּשָׁלַיִם";
@Test
public void testConvertTimestamp() throws Exception {
SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss.SSS");
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
long timestamp = dateFormat.parse("01/02/2003 04:06:06.789").getTime();
nfstime3 converted = Utils.convertTimestamp(timestamp);
Assert.assertEquals(timestamp/1000, converted.seconds.value);
Assert.assertEquals(1000000 * (timestamp%1000), converted.nseconds.value);
long decoded = Utils.convertTimestamp(converted);
Assert.assertEquals(timestamp, decoded);
}
@Test
public void testWithGoodName() throws AccessException, NameTooLongException {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment