File indexing completed on 2024-12-22 04:41:40

0001 /*
0002  * SPDX-FileCopyrightText: 2018 Erik Duisters <e.duisters1@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 package org.kde.kdeconnect.Plugins.SftpPlugin;
0008 
0009 import android.content.Context;
0010 
0011 import org.apache.sshd.common.file.FileSystemView;
0012 import org.apache.sshd.common.file.SshFile;
0013 import org.apache.sshd.common.file.nativefs.NativeFileSystemView;
0014 import org.apache.sshd.common.file.nativefs.NativeSshFile;
0015 
0016 import java.io.File;
0017 import java.util.ArrayList;
0018 import java.util.List;
0019 import java.util.Map;
0020 
0021 class AndroidFileSystemView extends NativeFileSystemView {
0022     final private String userName;
0023     final private Context context;
0024     private final Map<String, String> roots;
0025     private final RootFile rootFile;
0026 
0027     AndroidFileSystemView(Map<String, String> roots, String currentRoot, final String userName, Context context) {
0028         super(userName, roots, currentRoot, File.separatorChar, true);
0029         this.roots = roots;
0030         this.userName = userName;
0031         this.context = context;
0032         this.rootFile = new RootFile( createFileList(), userName, true);
0033     }
0034 
0035     private List<SshFile> createFileList() {
0036         List<SshFile> list = new ArrayList<>();
0037         for (Map.Entry<String, String> entry : roots.entrySet()) {
0038             String displayName = entry.getKey();
0039             String path = entry.getValue();
0040 
0041             list.add(createNativeSshFile(displayName, new File(path), userName));
0042         }
0043 
0044         return list;
0045     }
0046 
0047     @Override
0048     public SshFile getFile(String file) {
0049         return getFile("/", file);
0050     }
0051 
0052     @Override
0053     public SshFile getFile(SshFile baseDir, String file) {
0054         return getFile(baseDir.getAbsolutePath(), file);
0055     }
0056 
0057     @Override
0058     protected SshFile getFile(String dir, String file) {
0059         if (!dir.endsWith("/")) {
0060             dir = dir + "/";
0061         }
0062 
0063         if (!file.startsWith("/")) {
0064             file = dir + file;
0065         }
0066 
0067         String filename = NativeSshFile.getPhysicalName("/", "/", file, false);
0068 
0069         if (filename.equals("/")) {
0070             return rootFile;
0071         }
0072 
0073         for (String root : roots.keySet()) {
0074             if (filename.indexOf(root) == 1) {
0075                 String nameWithoutRoot = filename.substring(root.length() + 1);
0076                 String path = roots.get(root);
0077 
0078                 if (nameWithoutRoot.isEmpty()) {
0079                     return createNativeSshFile(filename, new File(path), userName);
0080                 } else {
0081                     return createNativeSshFile(filename, new File(path, nameWithoutRoot), userName);
0082                 }
0083             }
0084         }
0085 
0086         //It's a file under / but not one covered by any Tree
0087         return new RootFile(new ArrayList<>(0), userName, false);
0088     }
0089 
0090     // NativeFileSystemView.getFile(), NativeSshFile.getParentFile() and NativeSshFile.listSshFiles() call
0091     // createNativeSshFile to create new NativeSshFiles so override that instead of getFile() to always create an AndroidSshFile
0092     @Override
0093     public AndroidSshFile createNativeSshFile(String name, File file, String username) {
0094         return new AndroidSshFile(this, name, file, username, context);
0095     }
0096 
0097     @Override
0098     public FileSystemView getNormalizedView() {
0099         return this;
0100     }
0101 }