Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.mtp; |
| 18 | |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 19 | import android.annotation.WorkerThread; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 20 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 21 | import android.os.Process; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 22 | import android.os.storage.StorageManager; |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 23 | import android.system.OsConstants; |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 24 | import android.util.Log; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 25 | import com.android.internal.annotations.VisibleForTesting; |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 26 | import com.android.internal.util.Preconditions; |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 27 | import com.android.mtp.annotations.UsedByNative; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 28 | import java.io.File; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 29 | import java.io.FileNotFoundException; |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 30 | import java.io.IOException; |
| 31 | |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 32 | public class AppFuse { |
| 33 | static { |
| 34 | System.loadLibrary("appfuse_jni"); |
| 35 | } |
| 36 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 37 | /** |
| 38 | * Max read amount specified at the FUSE kernel implementation. |
| 39 | * The value is copied from sdcard.c. |
| 40 | */ |
| 41 | static final int MAX_READ = 128 * 1024; |
| 42 | |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 43 | private final String mName; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 44 | private final Callback mCallback; |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * Buffer for read bytes request. |
| 48 | * Don't use the buffer from the out of AppFuseMessageThread. |
| 49 | */ |
| 50 | private byte[] mBuffer = new byte[MAX_READ]; |
| 51 | |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 52 | private Thread mMessageThread; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 53 | private ParcelFileDescriptor mDeviceFd; |
| 54 | |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 55 | AppFuse(String name, Callback callback) { |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 56 | mName = name; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 57 | mCallback = callback; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 58 | } |
| 59 | |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 60 | void mount(StorageManager storageManager) throws IOException { |
| 61 | Preconditions.checkState(mDeviceFd == null); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 62 | mDeviceFd = storageManager.mountAppFuse(mName); |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 63 | mMessageThread = new AppFuseMessageThread(mDeviceFd.dup().detachFd()); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 64 | mMessageThread.start(); |
| 65 | } |
| 66 | |
| 67 | @VisibleForTesting |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 68 | void close() { |
| 69 | try { |
| 70 | // Remote side of ParcelFileDescriptor is tracking the close of mDeviceFd, and unmount |
| 71 | // the corresponding fuse file system. The mMessageThread will receive FUSE_FORGET, and |
| 72 | // then terminate itself. |
| 73 | mDeviceFd.close(); |
| 74 | mMessageThread.join(); |
| 75 | } catch (IOException exp) { |
| 76 | Log.e(MtpDocumentsProvider.TAG, "Failed to close device FD.", exp); |
| 77 | } catch (InterruptedException exp) { |
| 78 | Log.e(MtpDocumentsProvider.TAG, "Failed to terminate message thread.", exp); |
| 79 | } |
| 80 | } |
| 81 | |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 82 | public ParcelFileDescriptor openFile(int i) throws FileNotFoundException { |
| 83 | return ParcelFileDescriptor.open(new File( |
| 84 | getMountPoint(), |
| 85 | Integer.toString(i)), |
| 86 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 87 | } |
| 88 | |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 89 | File getMountPoint() { |
| 90 | return new File("/mnt/appfuse/" + Process.myUid() + "_" + mName); |
| 91 | } |
| 92 | |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 93 | static interface Callback { |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 94 | /** |
| 95 | * Returns file size for the given inode. |
| 96 | * @param inode |
| 97 | * @return File size. Must not be negative. |
| 98 | * @throws FileNotFoundException |
| 99 | */ |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 100 | long getFileSize(int inode) throws FileNotFoundException; |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 101 | |
| 102 | /** |
| 103 | * Returns flie bytes for the give inode. |
| 104 | * @param inode |
| 105 | * @param offset Offset for file bytes. |
| 106 | * @param size Size for file bytes. |
| 107 | * @param bytes Buffer to store file bytes. |
| 108 | * @return Number of read bytes. Must not be negative. |
| 109 | * @throws IOException |
| 110 | */ |
| 111 | long readObjectBytes(int inode, long offset, long size, byte[] bytes) throws IOException; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 112 | } |
| 113 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 114 | @UsedByNative("com_android_mtp_AppFuse.cpp") |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 115 | @WorkerThread |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 116 | private long getFileSize(int inode) { |
| 117 | try { |
| 118 | return mCallback.getFileSize(inode); |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 119 | } catch (FileNotFoundException e) { |
| 120 | return -OsConstants.ENOENT; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 124 | @UsedByNative("com_android_mtp_AppFuse.cpp") |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 125 | @WorkerThread |
| 126 | private long readObjectBytes(int inode, long offset, long size) { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 127 | if (offset < 0 || size < 0 || size > MAX_READ) { |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 128 | return -OsConstants.EINVAL; |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 129 | } |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 130 | try { |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 131 | // It's OK to share the same mBuffer among requests because the requests are processed |
| 132 | // by AppFuseMessageThread sequentially. |
| 133 | return mCallback.readObjectBytes(inode, offset, size, mBuffer); |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 134 | } catch (IOException e) { |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 135 | return -OsConstants.EIO; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 136 | } catch (UnsupportedOperationException e) { |
| 137 | return -OsConstants.ENOTSUP; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 141 | private native boolean native_start_app_fuse_loop(int fd); |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 142 | |
| 143 | private class AppFuseMessageThread extends Thread { |
| 144 | /** |
| 145 | * File descriptor used by native loop. |
| 146 | * It's owned by native loop and does not need to close here. |
| 147 | */ |
| 148 | private final int mRawFd; |
| 149 | |
| 150 | AppFuseMessageThread(int fd) { |
| 151 | super("AppFuseMessageThread"); |
| 152 | mRawFd = fd; |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public void run() { |
| 157 | native_start_app_fuse_loop(mRawFd); |
| 158 | } |
| 159 | } |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 160 | } |