Skip to content

Commit 96d8786

Browse files
committed
core: Make Inode a subclass of FileHandle; deprecate constructors
In an effort to simplify file handle/inode call, make Inode no longer encapsulate FileHandle, but extend from it. While the ultimate goal is to deprecate FileHandle, and allow Inode to be used directly in more places, this is the first step in a series of changes. Deprecate the direct use of Inode constructors in favor of static helper methods. Adjust some places in the code that assumed encapsulation / called constructors directly. Related: #149 Signed-off-by: Christian Kohlschütter <[email protected]>
1 parent e2c2f03 commit 96d8786

File tree

9 files changed

+59
-31
lines changed

9 files changed

+59
-31
lines changed

core/src/main/java/org/dcache/nfs/vfs/FileHandle.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,10 @@ public FileHandle build(byte[] opaque) {
201201
return new FileHandle(generation, export_idx, type, opaque);
202202
}
203203

204+
public Inode buildInode(byte[] opaque) {
205+
return new Inode(generation, export_idx, type, opaque);
206+
}
207+
204208
public FileHandle build() {
205209
return build(fs_opaque);
206210
}

core/src/main/java/org/dcache/nfs/vfs/Inode.java

Lines changed: 33 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,52 @@
2121

2222
import java.util.Arrays;
2323

24-
public class Inode {
24+
public class Inode extends FileHandle {
25+
/**
26+
* This constructor will become marked {@code protected} in a future version.
27+
*
28+
* @param bytes The VFS-specific bytes.
29+
*/
30+
@Deprecated
31+
public Inode(byte[] bytes) {
32+
super(bytes);
33+
}
2534

26-
private final FileHandle fh;
35+
@Deprecated(forRemoval = true)
36+
public Inode(FileHandle fh) {
37+
this(fh.bytes());
38+
}
2739

28-
public Inode(byte[] bytes) {
29-
this(new FileHandle(bytes));
40+
Inode(int generation, int exportIdx, int type, byte[] fs_opaque) {
41+
super(generation, exportIdx, type, fs_opaque);
42+
}
43+
44+
public static Inode forFileHandle(FileHandle fh) {
45+
if (fh instanceof Inode) {
46+
return ((Inode) fh);
47+
}
48+
return new Inode(fh.bytes());
3049
}
3150

32-
public Inode(FileHandle h) {
33-
fh = h;
51+
public static Inode forNfsHandle(byte[] bytes) {
52+
return new Inode(bytes);
3453
}
3554

3655
public static Inode forFile(byte[] bytes) {
37-
return new Inode(new FileHandle.FileHandleBuilder().build(bytes));
56+
return new FileHandle.FileHandleBuilder().buildInode(bytes);
3857
}
3958

4059
public byte[] getFileId() {
41-
return fh.getFsOpaque();
60+
return getFsOpaque();
4261
}
4362

4463
public byte[] toNfsHandle() {
45-
return fh.bytes();
64+
return bytes();
4665
}
4766

4867
@Override
4968
public int hashCode() {
50-
return Arrays.hashCode(fh.bytes());
69+
return Arrays.hashCode(bytes());
5170
}
5271

5372
@Override
@@ -59,23 +78,18 @@ public boolean equals(Object obj) {
5978
return false;
6079
}
6180
final Inode other = (Inode) obj;
62-
return Arrays.equals(fh.bytes(), other.fh.bytes());
81+
return Arrays.equals(bytes(), other.bytes());
6382
}
6483

6584
public boolean isPseudoInode() {
66-
return fh.getType() == 1;
85+
return getType() == 1;
6786
}
6887

6988
public int exportIndex() {
70-
return fh.getExportIdx();
89+
return getExportIdx();
7190
}
7291

7392
public int handleVersion() {
74-
return fh.getVersion();
75-
}
76-
77-
@Override
78-
public String toString() {
79-
return fh.toString();
93+
return getVersion();
8094
}
8195
}

core/src/main/java/org/dcache/nfs/vfs/PseudoFs.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,7 @@ public static Inode pseudoIdToReal(Inode inode, int index) {
547547
.setExportIdx(index)
548548
.setType(0)
549549
.build(inode.getFileId());
550-
return new Inode(fh);
550+
return Inode.forFileHandle(fh);
551551
}
552552

553553
private int getIndexId(PseudoFsNode node) {
@@ -618,7 +618,7 @@ private Inode pushExportIndex(Inode inode, int index) {
618618
.setExportIdx(index)
619619
.setType(0)
620620
.build(inode.getFileId());
621-
return new Inode(fh);
621+
return Inode.forFileHandle(fh);
622622
}
623623

624624
private Inode pushExportIndex(Inode parent, Inode inode) {
@@ -648,7 +648,7 @@ private Inode realToPseudo(Inode inode, int idx) {
648648
.setExportIdx(idx)
649649
.setType(1)
650650
.build(inode.getFileId());
651-
return new Inode(fh);
651+
return Inode.forFileHandle(fh);
652652
}
653653

654654
private void pathToPseudoFs(final PseudoFsNode root, Set<PseudoFsNode> all, FsExport e) {

core/src/main/java/org/dcache/nfs/vfs/VirtualFileSystem.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,4 +520,15 @@ default void removeXattr(Inode inode, String attr) throws IOException {
520520
default CompletableFuture<Long> copyFileRange(Inode src, long srcPos, Inode dst, long dstPos, long len) {
521521
return CompletableFuture.failedFuture(new NotSuppException());
522522
}
523+
524+
/**
525+
* Converts an NFS file handle to an {@link Inode}.
526+
*
527+
* @param bytes The NFS file handle.
528+
* @return The {@link Inode}; must not be {@code null}.
529+
* @throws IOException on error
530+
*/
531+
default Inode inodeForNfsHandle(byte[] bytes) throws IOException {
532+
return Inode.forNfsHandle(bytes);
533+
}
523534
}

core/src/test/java/org/dcache/nfs/v3/NfsServerV3READDIRPLUS_3Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class NfsServerV3READDIRPLUS_3Test {
4242
@Before
4343
public void setup() throws Exception {
4444
dirHandle = new FileHandle(0, 1, 0, new byte[] {0, 0, 0, 1}); // the dir we want to read
45-
dirInode = new Inode(dirHandle);
45+
dirInode = Inode.forFileHandle(dirHandle);
4646
dirStat = new Stat(); // the stat marking it as a dir
4747
// noinspection OctalInteger
4848
dirStat.setMode(Stat.S_IFDIR | 0755);

core/src/test/java/org/dcache/nfs/v3/NfsServerV3READDIR_3Test.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public class NfsServerV3READDIR_3Test {
4343
@Before
4444
public void setup() throws Exception {
4545
dirHandle = new FileHandle(0, 1, 0, new byte[] {0, 0, 0, 1}); // the dir we want to read
46-
dirInode = new Inode(dirHandle);
46+
dirInode = Inode.forFileHandle(dirHandle);
4747
dirStat = new Stat(); // the stat marking it as a dir
4848
// noinspection OctalInteger
4949
dirStat.setMode(Stat.S_IFDIR | 0755);

core/src/test/java/org/dcache/nfs/v4/OperationREADDIRTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public class OperationREADDIRTest {
6161
@Before
6262
public void setup() throws Exception {
6363
dirHandle = new FileHandle(0, 0, 0, new byte[] {0, 0, 0, 0}); // the dir we want to read
64-
dirInode = new Inode(dirHandle);
64+
dirInode = Inode.forFileHandle(dirHandle);
6565
dirStat = new Stat(); // the stat marking it as a dir
6666
// noinspection OctalInteger
6767
dirStat.setMode(Stat.S_IFDIR | 0755);
@@ -174,7 +174,7 @@ private DirectoryEntry dir(String name) {
174174
int cookie = ino++;
175175

176176
FileHandle handle = new FileHandle(0, 1, 0, Ints.toByteArray(cookie));
177-
Inode inode = new Inode(handle);
177+
Inode inode = Inode.forFileHandle(handle);
178178
Stat stat = new Stat(); // the stat marking it as a dir
179179
// noinspection OctalInteger
180180
stat.setMode(Stat.S_IFDIR | 0755);
@@ -202,7 +202,7 @@ private DirectoryEntry file(String name) {
202202
int cookie = ino++;
203203

204204
FileHandle handle = new FileHandle(0, 1, 0, Ints.toByteArray(cookie));
205-
Inode inode = new Inode(handle);
205+
Inode inode = Inode.forFileHandle(handle);
206206
Stat stat = new Stat(); // the stat marking it as a dir
207207
// noinspection OctalInteger
208208
stat.setMode(Stat.S_IFREG | 0644);

core/src/test/java/org/dcache/nfs/vfs/DirectoryStreamTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public void setUp() {
2626

2727
for (int i = 0; i < 10; i++) {
2828
FileHandle fh = new FileHandle(0, 0, 0, Ints.toByteArray(i));
29-
Inode inode = new Inode(fh);
29+
Inode inode = Inode.forFileHandle(fh);
3030
Stat stat = new Stat();
3131

3232
stat.setMode(Stat.S_IFDIR | 0755);

core/src/test/java/org/dcache/nfs/vfs/PseudoFsTest.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,9 @@ public void testAccessDenyOnHighjackedInode() throws IOException {
384384
given(mockedRpc.getTransport()).willReturn(mockedTransport);
385385
given(mockedRpc.getCredential()).willReturn(mockedAuth);
386386

387-
Inode inode = new Inode(
388-
new FileHandle.FileHandleBuilder()
387+
Inode inode = new FileHandle.FileHandleBuilder()
389388
.setExportIdx(1)
390-
.build(Longs.toByteArray(1L)));
389+
.buildInode(Longs.toByteArray(1L));
391390

392391
given(mockedExportFile.getExport(1, localAddress.getAddress())).willReturn(null);
393392

0 commit comments

Comments
 (0)