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 | |
| 19 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 20 | import android.os.Process; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 21 | import android.os.storage.StorageManager; |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 22 | import android.util.Log; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 23 | import com.android.internal.annotations.VisibleForTesting; |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 24 | import com.android.internal.util.Preconditions; |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 25 | import com.android.mtp.annotations.UsedByNative; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 26 | import java.io.File; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 27 | import java.io.FileNotFoundException; |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 28 | import java.io.IOException; |
| 29 | |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 30 | public class AppFuse { |
| 31 | static { |
| 32 | System.loadLibrary("appfuse_jni"); |
| 33 | } |
| 34 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 35 | /** |
| 36 | * Max read amount specified at the FUSE kernel implementation. |
| 37 | * The value is copied from sdcard.c. |
| 38 | */ |
| 39 | static final int MAX_READ = 128 * 1024; |
| 40 | |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 41 | private final String mName; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 42 | private final Callback mCallback; |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 43 | private Thread mMessageThread; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 44 | private ParcelFileDescriptor mDeviceFd; |
| 45 | |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 46 | AppFuse(String name, Callback callback) { |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 47 | mName = name; |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 48 | mCallback = callback; |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 49 | } |
| 50 | |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 51 | void mount(StorageManager storageManager) throws IOException { |
| 52 | Preconditions.checkState(mDeviceFd == null); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 53 | mDeviceFd = storageManager.mountAppFuse(mName); |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 54 | mMessageThread = new AppFuseMessageThread(mDeviceFd.dup().detachFd()); |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 55 | mMessageThread.start(); |
| 56 | } |
| 57 | |
| 58 | @VisibleForTesting |
Daichi Hirono | 91e3b50 | 2015-12-16 09:24:16 +0900 | [diff] [blame] | 59 | void close() { |
| 60 | try { |
| 61 | // Remote side of ParcelFileDescriptor is tracking the close of mDeviceFd, and unmount |
| 62 | // the corresponding fuse file system. The mMessageThread will receive FUSE_FORGET, and |
| 63 | // then terminate itself. |
| 64 | mDeviceFd.close(); |
| 65 | mMessageThread.join(); |
| 66 | } catch (IOException exp) { |
| 67 | Log.e(MtpDocumentsProvider.TAG, "Failed to close device FD.", exp); |
| 68 | } catch (InterruptedException exp) { |
| 69 | Log.e(MtpDocumentsProvider.TAG, "Failed to terminate message thread.", exp); |
| 70 | } |
| 71 | } |
| 72 | |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 73 | public ParcelFileDescriptor openFile(int i) throws FileNotFoundException { |
| 74 | return ParcelFileDescriptor.open(new File( |
| 75 | getMountPoint(), |
| 76 | Integer.toString(i)), |
| 77 | ParcelFileDescriptor.MODE_READ_ONLY); |
| 78 | } |
| 79 | |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 80 | File getMountPoint() { |
| 81 | return new File("/mnt/appfuse/" + Process.myUid() + "_" + mName); |
| 82 | } |
| 83 | |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 84 | static interface Callback { |
| 85 | long getFileSize(int inode) throws FileNotFoundException; |
| 86 | byte[] getObjectBytes(int inode, long offset, int size) throws IOException; |
| 87 | } |
| 88 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 89 | @UsedByNative("com_android_mtp_AppFuse.cpp") |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 90 | private long getFileSize(int inode) { |
| 91 | try { |
| 92 | return mCallback.getFileSize(inode); |
| 93 | } catch (IOException e) { |
| 94 | return -1; |
| 95 | } |
| 96 | } |
| 97 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 98 | @UsedByNative("com_android_mtp_AppFuse.cpp") |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 99 | private byte[] getObjectBytes(int inode, long offset, int size) { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 100 | if (offset < 0 || size < 0 || size > MAX_READ) { |
| 101 | return null; |
| 102 | } |
Daichi Hirono | cc9a7d7 | 2015-10-28 09:43:48 +0900 | [diff] [blame] | 103 | try { |
| 104 | return mCallback.getObjectBytes(inode, offset, size); |
| 105 | } catch (IOException e) { |
| 106 | return null; |
| 107 | } |
| 108 | } |
| 109 | |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 110 | private native boolean native_start_app_fuse_loop(int fd); |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 111 | |
| 112 | private class AppFuseMessageThread extends Thread { |
| 113 | /** |
| 114 | * File descriptor used by native loop. |
| 115 | * It's owned by native loop and does not need to close here. |
| 116 | */ |
| 117 | private final int mRawFd; |
| 118 | |
| 119 | AppFuseMessageThread(int fd) { |
| 120 | super("AppFuseMessageThread"); |
| 121 | mRawFd = fd; |
| 122 | } |
| 123 | |
| 124 | @Override |
| 125 | public void run() { |
| 126 | native_start_app_fuse_loop(mRawFd); |
| 127 | } |
| 128 | } |
Daichi Hirono | bee50c0 | 2015-12-14 11:00:54 +0900 | [diff] [blame] | 129 | } |