Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 android.os; |
| 18 | |
| 19 | import android.system.ErrnoException; |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 20 | import android.system.OsConstants; |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 21 | |
| 22 | /** |
| 23 | * Callback that handles file system requests from ProxyFileDescriptor. |
Daichi Hirono | e200ae8 | 2017-04-04 13:13:24 +0900 | [diff] [blame] | 24 | * |
Daichi Hirono | 0a3b2c6 | 2017-05-12 10:59:33 +0900 | [diff] [blame] | 25 | * All callback methods except for onRelease should throw {@link android.system.ErrnoException} |
| 26 | * with proper errno on errors. See |
| 27 | * <a href="http://man7.org/linux/man-pages/man3/errno.3.html">errno(3)</a> and |
| 28 | * {@link android.system.OsConstants}. |
| 29 | * |
| 30 | * Typical errnos are |
| 31 | * |
| 32 | * <ul> |
| 33 | * <li>{@link android.system.OsConstants#EIO} for general I/O issues |
| 34 | * <li>{@link android.system.OsConstants#ENOENT} when the file is not found |
| 35 | * <li>{@link android.system.OsConstants#EBADF} if the file doesn't allow read/write operations |
| 36 | * based on how it was opened. (For example, trying to write a file that was opened read-only.) |
| 37 | * <li>{@link android.system.OsConstants#ENOSPC} if you cannot handle a write operation to |
| 38 | * space/quota limitations. |
| 39 | * </ul> |
| 40 | * @see android.os.storage.StorageManager#openProxyFileDescriptor(int, ProxyFileDescriptorCallback, |
| 41 | * Handler) |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 42 | */ |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 43 | public abstract class ProxyFileDescriptorCallback { |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 44 | /** |
| 45 | * Returns size of bytes provided by the file descriptor. |
Daichi Hirono | e200ae8 | 2017-04-04 13:13:24 +0900 | [diff] [blame] | 46 | * @return Size of bytes. |
| 47 | * @throws ErrnoException ErrnoException containing E constants in OsConstants. |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 48 | */ |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 49 | public long onGetSize() throws ErrnoException { |
| 50 | throw new ErrnoException("onGetSize", OsConstants.EBADF); |
| 51 | } |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 52 | |
| 53 | /** |
| 54 | * Provides bytes read from file descriptor. |
| 55 | * It needs to return exact requested size of bytes unless it reaches file end. |
Daichi Hirono | e200ae8 | 2017-04-04 13:13:24 +0900 | [diff] [blame] | 56 | * @param offset Offset in bytes from the file head specifying where to read bytes. If a seek |
| 57 | * operation is conducted on the file descriptor, then a read operation is requested, the |
| 58 | * offset refrects the proper position of requested bytes. |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 59 | * @param size Size for read bytes. |
| 60 | * @param data Byte array to store read bytes. |
| 61 | * @return Size of bytes returned by the function. |
Daichi Hirono | e200ae8 | 2017-04-04 13:13:24 +0900 | [diff] [blame] | 62 | * @throws ErrnoException ErrnoException containing E constants in OsConstants. |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 63 | */ |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 64 | public int onRead(long offset, int size, byte[] data) throws ErrnoException { |
| 65 | throw new ErrnoException("onRead", OsConstants.EBADF); |
| 66 | } |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 67 | |
| 68 | /** |
| 69 | * Handles bytes written to file descriptor. |
Daichi Hirono | e200ae8 | 2017-04-04 13:13:24 +0900 | [diff] [blame] | 70 | * @param offset Offset in bytes from the file head specifying where to write bytes. If a seek |
| 71 | * operation is conducted on the file descriptor, then a write operation is requested, the |
| 72 | * offset refrects the proper position of requested bytes. |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 73 | * @param size Size for write bytes. |
| 74 | * @param data Byte array to be written to somewhere. |
| 75 | * @return Size of bytes processed by the function. |
Daichi Hirono | e200ae8 | 2017-04-04 13:13:24 +0900 | [diff] [blame] | 76 | * @throws ErrnoException ErrnoException containing E constants in OsConstants. |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 77 | */ |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 78 | public int onWrite(long offset, int size, byte[] data) throws ErrnoException { |
| 79 | throw new ErrnoException("onWrite", OsConstants.EBADF); |
| 80 | } |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 81 | |
| 82 | /** |
Daichi Hirono | e200ae8 | 2017-04-04 13:13:24 +0900 | [diff] [blame] | 83 | * Ensures all the written data are stored in permanent storage device. |
| 84 | * For example, if it has data stored in on memory cache, it needs to flush data to storage |
| 85 | * device. |
| 86 | * @throws ErrnoException ErrnoException containing E constants in OsConstants. |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 87 | */ |
Daichi Hirono | 9fb0018 | 2016-11-08 14:12:17 +0900 | [diff] [blame] | 88 | public void onFsync() throws ErrnoException { |
| 89 | throw new ErrnoException("onFsync", OsConstants.EINVAL); |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Invoked after the file is closed. |
| 94 | */ |
| 95 | abstract public void onRelease(); |
Daichi Hirono | 878e86f | 2016-10-31 09:33:30 +0900 | [diff] [blame] | 96 | } |