Skip to content

Commit 365cc65

Browse files
committed
Path translation and normalisation fixes.
1 parent 1b2a7e5 commit 365cc65

File tree

13 files changed

+172
-74
lines changed

13 files changed

+172
-74
lines changed

cli/src/main/java/uk/co/bithatch/tnfs/cli/InteractiveConsole.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,12 @@ public void startIfNotStarted() throws Exception {
132132
}
133133

134134
@Override
135-
public String translatePath(String path) {
136-
return cntr.translatePath(path);
135+
public String nativeToLocalPath(String path) {
136+
return cntr.nativeToLocalPath(path);
137137
}
138138

139139
@Override
140-
public String untranslatePath(String path) {
141-
return cntr.untranslatePath(path);
140+
public String localToNativePath(String path) {
141+
return cntr.localToNativePath(path);
142142
}
143143
}

cli/src/main/java/uk/co/bithatch/tnfs/cli/TNFSContainer.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,20 +125,20 @@ public interface TNFSContainer {
125125
void startIfNotStarted() throws Exception;
126126

127127
/**
128-
* Translate the path separators from the native forward slashes to whatever
128+
* Translate the path separators from the TNFS forward slashes to whatever
129129
* is automatically detected for the platform (or chosen through configuration).
130130
*
131131
* @param path
132132
* @return translated path
133133
*/
134-
String translatePath(String path);
134+
String nativeToLocalPath(String path);
135135

136136
/**
137-
* Translate the path separators from the native forward slashes to whatever
138-
* is automatically detected for the platform (or chosen through configuration).
137+
* Translate the path separators from those automatically detected for the platform
138+
* (or chosen through configuration) to TNFS forward slashes.
139139
*
140140
* @param path
141141
* @return translated path
142142
*/
143-
String untranslatePath(String path);
143+
String localToNativePath(String path);
144144
}

cli/src/main/java/uk/co/bithatch/tnfs/cli/TNFSFUSE.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import java.nio.file.Files;
2424
import java.nio.file.Path;
25+
import java.util.Optional;
2526
import java.util.concurrent.Callable;
2627
import java.util.concurrent.TimeoutException;
2728

@@ -31,8 +32,8 @@
3132

3233
import picocli.CommandLine;
3334
import picocli.CommandLine.Command;
34-
import picocli.CommandLine.Option;
3535
import picocli.CommandLine.Model.CommandSpec;
36+
import picocli.CommandLine.Option;
3637
import picocli.CommandLine.Parameters;
3738
import picocli.CommandLine.Spec;
3839
import uk.co.bithatch.tnfs.fuse.TNFSFUSEFileSystem;
@@ -59,6 +60,9 @@ public static void main(String[] args) throws Exception {
5960
@Option(names = { "-c", "--create" }, description = "Create the mount point folder if it does not exist.")
6061
private boolean create;
6162

63+
@Option(names = {"--libpath" }, description = "The location of the libfuse library.", hidden = true)
64+
protected Optional<Path> libpath;
65+
6266
@Parameters(arity = "1", index = "0", description = "URI of TNFS resource to mount.")
6367
protected String remotePath;
6468

@@ -84,6 +88,7 @@ protected void onStart() throws Exception {
8488
var uri = TNFSURI.parse(remotePath);
8589
var mount = doMount(uri, doConnect(uri));
8690
var builder = Fuse.builder();
91+
libpath.ifPresent(p -> builder.setLibraryPath(p.toString()));
8792
var fuseOps = new TNFSFUSEFileSystem(mount, builder.errno());
8893
try (var fuse = builder.build(fuseOps)) {
8994
Runtime.getRuntime().addShutdownHook(new Thread(() -> {

cli/src/main/java/uk/co/bithatch/tnfs/cli/TNFSTP.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@ public void startIfNotStarted() throws Exception {
269269
}
270270

271271
@Override
272-
public String translatePath(String cwd) {
272+
public String nativeToLocalPath(String cwd) {
273273
/* TODO what if path has escaped forward slash? */
274274
var sep = getSeparator();
275275
if(!sep.equals("/"))
@@ -278,7 +278,7 @@ public String translatePath(String cwd) {
278278
return cwd.replace("\\", sep);
279279
}
280280
@Override
281-
public String untranslatePath(String cwd) {
281+
public String localToNativePath(String cwd) {
282282
/* TODO what if path has escaped forward slash? */
283283
var sep = getSeparator();
284284
if(sep.equals("/"))

cli/src/main/java/uk/co/bithatch/tnfs/cli/commands/Cd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected Integer onCall() throws Exception {
4848

4949
if (directory != null && directory.length() > 0) {
5050
directory = Util.relativizePath(container.getCwd(), directory, container.getSeparator());
51-
var file = mount.stat(directory);
51+
var file = mount.stat(container.localToNativePath(directory));
5252
if (file.isDirectory()) {
5353
container.setCwd(directory);
5454
} else {

cli/src/main/java/uk/co/bithatch/tnfs/cli/commands/Lcd.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,26 @@ public Lcd() {
4747
@Override
4848
protected Integer onCall() throws Exception {
4949
var container = getContainer();
50-
var resolved = directory.orElseGet(() -> Paths.get(System.getProperty("user.home")));
50+
var userhome = Paths.get(System.getProperty("user.home"));
51+
var resolved = directory.map(d -> {
52+
if(d.getName(0).getFileName().toString().equals("~")) {
53+
if(d.getNameCount() == 1) {
54+
return userhome;
55+
}
56+
else {
57+
return userhome.resolve(d.subpath(1, d.getNameCount()));
58+
}
59+
}
60+
else {
61+
return d;
62+
}
63+
}).orElse(userhome);
5164

5265
if (!resolved.isAbsolute())
5366
resolved = container.getLcwd().resolve(resolved);
5467

5568
if (Files.isDirectory(resolved))
56-
container.setLcwd(resolved);
69+
container.setLcwd(resolved.normalize());
5770
else
5871
throw new NotDirectoryException(resolved.toString());
5972
return 0;

cli/src/main/java/uk/co/bithatch/tnfs/cli/commands/Ls.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ protected Integer onCall() throws Exception {
9191

9292
var dirOptions = new ArrayList<DirOptionFlag>();
9393
var dirSort = new ArrayList<DirSortFlag>();
94-
var apath = container.untranslatePath(path.orElseGet(container::getCwd));
94+
var apath = container.localToNativePath(path.orElseGet(container::getCwd));
9595
var base = Util.basename(apath);
9696
var wildcard = "";
9797
var resolver = Styles.lsStyle();

cli/src/main/java/uk/co/bithatch/tnfs/cli/commands/Put.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
@Command(name = "put", aliases = { "upload"}, mixinStandardHelpOptions = true, description = "Upload local file.")
4040
public class Put extends TNFSTPCommand implements Callable<Integer> {
4141

42+
private static final int LOCAL_READ_BUFFER_SIZE = 65536;
43+
4244
@Parameters(index = "0", arity = "1", description = "File to store.")
4345
private String file;
4446

@@ -65,14 +67,14 @@ protected Integer onCall() throws Exception {
6567
// transfers.startedTransfer(path.toString(), target, Files.size(path));
6668

6769
try(var o = mnt.open(target, OpenFlag.WRITE, OpenFlag.TRUNCATE, OpenFlag.CREATE)) {
68-
var buf = ByteBuffer.allocate(mnt.client().messageSize());
69-
var rd = 0;
70-
while( ( rd = f.read(buf) ) != -1) {
70+
var buf = ByteBuffer.allocate(LOCAL_READ_BUFFER_SIZE);
71+
while( ( f.read(buf) ) != -1) {
7172
buf.flip();
72-
o.write(buf);
73+
while(buf.hasRemaining()) {
74+
var wrt = o.write(buf);
75+
pb.stepBy(wrt);
76+
}
7377
buf.clear();
74-
75-
pb.stepBy(rd);
7678

7779
// transfers.transferProgress(path.toString(), target, rd);
7880
}

cli/src/main/java/uk/co/bithatch/tnfs/cli/commands/Pwd.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Pwd() {
3737

3838
@Override
3939
protected Integer onCall() throws Exception {
40-
getContainer().getTerminal().writer().println(getContainer().translatePath(getContainer().getCwd()));
40+
getContainer().getTerminal().writer().println(getContainer().nativeToLocalPath(getContainer().getCwd()));
4141
return 0;
4242
}
4343
}

cli/src/main/java/uk/co/bithatch/tnfs/cli/commands/Stat.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import picocli.CommandLine.Command;
2727
import picocli.CommandLine.Parameters;
2828
import uk.co.bithatch.tnfs.cli.TNFSTP.FilenameCompletionMode;
29+
import uk.co.bithatch.tnfs.lib.Util;
2930

3031
/**
3132
* Stat file.
@@ -44,6 +45,7 @@ public Stat() {
4445
protected Integer onCall() throws Exception {
4546
var container = getContainer();
4647
var mount = container.getMount();
48+
file = Util.relativizePath(container.getCwd(), file, container.getSeparator());
4749
var stat = mount.stat(file);
4850
var wtr = getContainer().getTerminal().writer();
4951
wtr.println(String.format("%s %7d %7d %10d %10d %10d \"%s\" \"%s\"", stat.isDirectory() ? "d" : "-", stat.uid(), stat.gid(), stat.atime().to(TimeUnit.SECONDS), stat.ctime().to(TimeUnit.SECONDS), stat.mtime().to(TimeUnit.SECONDS), stat.uidString(), stat.gidString()));

0 commit comments

Comments
 (0)