Throw FileNotFoundException for remote file operations

This commit is contained in:
Nullptr 2023-01-16 19:08:57 +08:00
parent 3e7e7bda38
commit 502a5142bb
No known key found for this signature in database
1 changed files with 29 additions and 26 deletions

View File

@ -10,6 +10,7 @@ import androidx.annotation.NonNull;
import androidx.annotation.Nullable; import androidx.annotation.Nullable;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
@ -125,7 +126,7 @@ public final class XposedService {
FRAMEWORK_PRIVILEGE_APP, FRAMEWORK_PRIVILEGE_APP,
/** /**
* The framework is embedded in the hooked app, which means {@link #getRemotePreferences} and remote file streams will be null. * The framework is embedded in the hooked app, which means {@link #getRemotePreferences} will be null and remote file is unsupported.
*/ */
FRAMEWORK_PRIVILEGE_EMBEDDED FRAMEWORK_PRIVILEGE_EMBEDDED
} }
@ -326,67 +327,69 @@ public final class XposedService {
/** /**
* Open an InputStream to read a file from the module's shared data directory. * Open an InputStream to read a file from the module's shared data directory.
* *
* @param name File name * @param name File name, must not contain path separators and . or ..
* @return The InputStream, null if the framework is embedded * @return The InputStream
* @throws ServiceException If the service is dead or error occurred * @throws FileNotFoundException If the file does not exist, the path is forbidden or remote file is not supported by the framework
*/ */
@Nullable @NonNull
public FileInputStream openRemoteFileInput(@NonNull String name) { public FileInputStream openRemoteFileInput(@NonNull String name) throws FileNotFoundException {
try { try {
var file = mService.openRemoteFile(name, MODE_READ_ONLY); var file = mService.openRemoteFile(name, MODE_READ_ONLY);
if (file == null) return null; if (file == null) throw new FileNotFoundException();
return new FileInputStream(file.getFileDescriptor()); return new FileInputStream(file.getFileDescriptor());
} catch (RemoteException e) { } catch (RemoteException e) {
throw new ServiceException(e); throw new FileNotFoundException(e.getMessage());
} }
} }
/** /**
* Open an OutputStream to write a file to the module's shared data directory. * Open an OutputStream to write a file to the module's shared data directory.
* *
* @param name File name * @param name File name, must not contain path separators and . or ..
* @param mode Operating mode * @param mode Operating mode
* @return The OutputStream, null if the framework is embedded * @return The OutputStream
* @throws ServiceException If the service is dead or error occurred * @throws FileNotFoundException If the path is forbidden or remote file is not supported by the framework
*/ */
@Nullable @NonNull
public FileOutputStream openRemoteFileOutput(@NonNull String name, int mode) { public FileOutputStream openRemoteFileOutput(@NonNull String name, int mode) throws FileNotFoundException {
try { try {
var file = mService.openRemoteFile(name, mode); var file = mService.openRemoteFile(name, mode);
if (file == null) return null; if (file == null) throw new FileNotFoundException();
return new FileOutputStream(file.getFileDescriptor()); return new FileOutputStream(file.getFileDescriptor());
} catch (RemoteException e) { } catch (RemoteException e) {
throw new ServiceException(e); throw new FileNotFoundException(e.getMessage());
} }
} }
/** /**
* Delete a file in the module's shared data directory. * Delete a file in the module's shared data directory.
* *
* @param name File name * @param name File name, must not contain path separators and . or ..
* @return true if successful, false if failed or the framework is embedded * @return true if successful, false if the file does not exist
* @throws ServiceException If the service is dead or error occurred * @throws FileNotFoundException If the path is forbidden or remote file is not supported by the framework
*/ */
public boolean deleteRemoteFile(@NonNull String name) { public boolean deleteRemoteFile(@NonNull String name) throws FileNotFoundException {
try { try {
return mService.deleteRemoteFile(name); return mService.deleteRemoteFile(name);
} catch (RemoteException e) { } catch (RemoteException e) {
throw new ServiceException(e); throw new FileNotFoundException(e.getMessage());
} }
} }
/** /**
* List all files in the module's shared data directory. * List all files in the module's shared data directory.
* *
* @return The file list, null if the framework is embedded * @return The file list
* @throws ServiceException If the service is dead or error occurred * @throws FileNotFoundException If remote file is not supported by the framework
*/ */
@Nullable @NonNull
public String[] listRemoteFiles() { public String[] listRemoteFiles() throws FileNotFoundException {
try { try {
return mService.listRemoteFiles(); var files = mService.listRemoteFiles();
if (files == null) throw new FileNotFoundException();
return files;
} catch (RemoteException e) { } catch (RemoteException e) {
throw new ServiceException(e); throw new FileNotFoundException(e.getMessage());
} }
} }
} }