Changeset View
Changeset View
Standalone View
Standalone View
src/org/kde/kdeconnect/Plugins/SftpPlugin/AndroidSshFile.java
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright 2018 Erik Duisters <e.duisters1@gmail.com> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | package org.kde.kdeconnect.Plugins.SftpPlugin; | ||||
22 | | ||||
23 | import android.content.Context; | ||||
24 | import android.net.Uri; | ||||
25 | | ||||
26 | import org.apache.sshd.common.file.nativefs.NativeSshFile; | ||||
27 | import org.kde.kdeconnect.Helpers.MediaStoreHelper; | ||||
28 | | ||||
29 | import java.io.File; | ||||
30 | import java.io.FileOutputStream; | ||||
31 | import java.io.IOException; | ||||
32 | import java.io.OutputStream; | ||||
33 | import java.io.RandomAccessFile; | ||||
34 | | ||||
35 | class AndroidSshFile extends NativeSshFile { | ||||
36 | private final static String TAG = AndroidSshFile.class.getSimpleName(); | ||||
37 | final private Context context; | ||||
38 | final private File file; | ||||
39 | | ||||
40 | AndroidSshFile(final AndroidFileSystemView view, String name, final File file, final String userName, Context context) { | ||||
41 | super(view, name, file, userName); | ||||
42 | this.context = context; | ||||
43 | this.file = file; | ||||
44 | } | ||||
45 | | ||||
46 | @Override | ||||
47 | public OutputStream createOutputStream(long offset) throws IOException { | ||||
48 | if (!isWritable()) { | ||||
49 | throw new IOException("No write permission : " + file.getName()); | ||||
50 | } | ||||
51 | | ||||
52 | final RandomAccessFile raf = new RandomAccessFile(file, "rw"); | ||||
53 | try { | ||||
54 | if (offset < raf.length()) { | ||||
55 | throw new IOException("Your SSHFS is bugged"); //SSHFS 3.0 and 3.2 cause data corruption, abort the transfer if this happens | ||||
56 | } | ||||
57 | raf.setLength(offset); | ||||
58 | raf.seek(offset); | ||||
59 | | ||||
60 | return new FileOutputStream(raf.getFD()) { | ||||
61 | public void close() throws IOException { | ||||
62 | super.close(); | ||||
63 | raf.close(); | ||||
64 | } | ||||
65 | }; | ||||
66 | } catch (IOException e) { | ||||
67 | raf.close(); | ||||
68 | throw e; | ||||
69 | } | ||||
70 | } | ||||
71 | | ||||
72 | @Override | ||||
73 | public boolean delete() { | ||||
74 | boolean ret = super.delete(); | ||||
75 | if (ret) { | ||||
76 | MediaStoreHelper.indexFile(context, Uri.fromFile(file)); | ||||
77 | } | ||||
78 | return ret; | ||||
79 | | ||||
80 | } | ||||
81 | | ||||
82 | @Override | ||||
83 | public boolean create() throws IOException { | ||||
84 | boolean ret = super.create(); | ||||
85 | if (ret) { | ||||
86 | MediaStoreHelper.indexFile(context, Uri.fromFile(file)); | ||||
87 | } | ||||
88 | return ret; | ||||
89 | | ||||
90 | } | ||||
91 | | ||||
92 | // Based on https://github.com/wolpi/prim-ftpd/blob/master/primitiveFTPd/src/org/primftpd/filesystem/FsFile.java | ||||
93 | @Override | ||||
94 | public boolean doesExist() { | ||||
95 | boolean exists = file.exists(); | ||||
96 | | ||||
97 | if (!exists) { | ||||
98 | // file.exists() returns false when we don't have read permission | ||||
99 | // try to figure out if it really does not exist | ||||
100 | File parentFile = file.getParentFile(); | ||||
101 | File[] children = parentFile.listFiles(); | ||||
102 | if (children != null) { | ||||
103 | for (File child : children) { | ||||
104 | if (file.equals(child)) { | ||||
105 | exists = true; | ||||
106 | break; | ||||
107 | } | ||||
108 | } | ||||
109 | } | ||||
110 | } | ||||
111 | | ||||
112 | return exists; | ||||
113 | } | ||||
114 | } |