blob: 1300c47dc229af4b985b4e0ee707b22133a057f6 [file] [log] [blame]
Daichi Hironobee50c02015-12-14 11:00:54 +09001/*
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
17package com.android.mtp;
18
19import android.os.ParcelFileDescriptor;
Daichi Hironocc9a7d72015-10-28 09:43:48 +090020import android.os.Process;
Daichi Hironobee50c02015-12-14 11:00:54 +090021import android.os.storage.StorageManager;
Daichi Hirono91e3b502015-12-16 09:24:16 +090022import android.util.Log;
Daichi Hironobee50c02015-12-14 11:00:54 +090023import com.android.internal.annotations.VisibleForTesting;
Daichi Hironoe6054c02016-01-20 15:36:04 +090024import com.android.internal.util.Preconditions;
Daichi Hironof52ef002016-01-11 18:07:01 +090025import com.android.mtp.annotations.UsedByNative;
Daichi Hironobee50c02015-12-14 11:00:54 +090026import java.io.File;
Daichi Hironocc9a7d72015-10-28 09:43:48 +090027import java.io.FileNotFoundException;
Daichi Hirono91e3b502015-12-16 09:24:16 +090028import java.io.IOException;
29
Daichi Hironobee50c02015-12-14 11:00:54 +090030public class AppFuse {
31 static {
32 System.loadLibrary("appfuse_jni");
33 }
34
Daichi Hironof52ef002016-01-11 18:07:01 +090035 /**
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 Hironobee50c02015-12-14 11:00:54 +090041 private final String mName;
Daichi Hironocc9a7d72015-10-28 09:43:48 +090042 private final Callback mCallback;
Daichi Hironoe6054c02016-01-20 15:36:04 +090043 private Thread mMessageThread;
Daichi Hironobee50c02015-12-14 11:00:54 +090044 private ParcelFileDescriptor mDeviceFd;
45
Daichi Hironocc9a7d72015-10-28 09:43:48 +090046 AppFuse(String name, Callback callback) {
Daichi Hironobee50c02015-12-14 11:00:54 +090047 mName = name;
Daichi Hironocc9a7d72015-10-28 09:43:48 +090048 mCallback = callback;
Daichi Hironobee50c02015-12-14 11:00:54 +090049 }
50
Daichi Hironoe6054c02016-01-20 15:36:04 +090051 void mount(StorageManager storageManager) throws IOException {
52 Preconditions.checkState(mDeviceFd == null);
Daichi Hironobee50c02015-12-14 11:00:54 +090053 mDeviceFd = storageManager.mountAppFuse(mName);
Daichi Hironoe6054c02016-01-20 15:36:04 +090054 mMessageThread = new AppFuseMessageThread(mDeviceFd.dup().detachFd());
Daichi Hironobee50c02015-12-14 11:00:54 +090055 mMessageThread.start();
56 }
57
58 @VisibleForTesting
Daichi Hirono91e3b502015-12-16 09:24:16 +090059 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 Hironocc9a7d72015-10-28 09:43:48 +090073 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 Hironobee50c02015-12-14 11:00:54 +090080 File getMountPoint() {
81 return new File("/mnt/appfuse/" + Process.myUid() + "_" + mName);
82 }
83
Daichi Hironocc9a7d72015-10-28 09:43:48 +090084 static interface Callback {
85 long getFileSize(int inode) throws FileNotFoundException;
86 byte[] getObjectBytes(int inode, long offset, int size) throws IOException;
87 }
88
Daichi Hironof52ef002016-01-11 18:07:01 +090089 @UsedByNative("com_android_mtp_AppFuse.cpp")
Daichi Hironocc9a7d72015-10-28 09:43:48 +090090 private long getFileSize(int inode) {
91 try {
92 return mCallback.getFileSize(inode);
93 } catch (IOException e) {
94 return -1;
95 }
96 }
97
Daichi Hironof52ef002016-01-11 18:07:01 +090098 @UsedByNative("com_android_mtp_AppFuse.cpp")
Daichi Hironocc9a7d72015-10-28 09:43:48 +090099 private byte[] getObjectBytes(int inode, long offset, int size) {
Daichi Hironof52ef002016-01-11 18:07:01 +0900100 if (offset < 0 || size < 0 || size > MAX_READ) {
101 return null;
102 }
Daichi Hironocc9a7d72015-10-28 09:43:48 +0900103 try {
104 return mCallback.getObjectBytes(inode, offset, size);
105 } catch (IOException e) {
106 return null;
107 }
108 }
109
Daichi Hironobee50c02015-12-14 11:00:54 +0900110 private native boolean native_start_app_fuse_loop(int fd);
Daichi Hironoe6054c02016-01-20 15:36:04 +0900111
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 Hironobee50c02015-12-14 11:00:54 +0900129}