Skip to content

Commit b2bf3a4

Browse files
committed
Change Android mv() back to using the system renameTo() method.
A previous change to rn-fetch-blob's mv implementation entailed copying the source file to the destination file and then deleting the source file. This approach has an undesirable impact on the mv operation, where the operation takes orders of magnitude longer to complete. It also adds disk space requirements that shouldn't be necessary, since both files need to exist at the same time. This change goes back to using the renameTo() method, while adding some extra error checking to hopefully alleviate whatever problems caused the original fix. Note that the POSIX mv() and rename() methods both stipulate that moving to a non-existent directory should be treated as an error.
1 parent 404d40b commit b2bf3a4

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

android/src/main/java/com/ReactNativeBlobUtil/ReactNativeBlobUtilFS.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -648,22 +648,23 @@ static void mv(String path, String dest, Callback callback) {
648648
}
649649

650650
try {
651-
InputStream in = new FileInputStream(path);
652-
OutputStream out = new FileOutputStream(dest);
653-
654-
//read source path to byte buffer. Write from input to output stream
655-
byte[] buffer = new byte[1024];
656-
int read;
657-
while ((read = in.read(buffer)) != -1) { //read is successful
658-
out.write(buffer, 0, read);
651+
// mv should fail if the destination directory does not exist.
652+
File destFile = new File(dest);
653+
File parentDir = destFile.getParentFile();
654+
if (parentDir != null && !parentDir.exists()) {
655+
callback.invoke("mv failed because the destination directory doesn't exist");
656+
return;
657+
}
658+
// mv overwrites files, so delete any existing file.
659+
if (destFile.exists()) {
660+
destFile.delete();
661+
}
662+
// mv by renaming the file.
663+
boolean result = src.renameTo(destFile);
664+
if (!result) {
665+
callback.invoke("mv failed for unknown reasons");
666+
return;
659667
}
660-
in.close();
661-
out.flush();
662-
663-
src.delete(); //remove original file
664-
} catch (FileNotFoundException exception) {
665-
callback.invoke("Source file not found.");
666-
return;
667668
} catch (Exception e) {
668669
callback.invoke(e.toString());
669670
return;

0 commit comments

Comments
 (0)