Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for copying files from one filesystem to another #134

Open
mymikemiller opened this issue May 18, 2020 · 4 comments
Open

Support for copying files from one filesystem to another #134

mymikemiller opened this issue May 18, 2020 · 4 comments

Comments

@mymikemiller
Copy link

I'd like to copy a file from the MemoryFileSystem to the LocalFileSystem (basically, write it to disk). When I try to run the following, I get the below error (localFileSystemPath is '/Users/mikem/example.mp4')

  File copyFile(File memoryFileSystemFile, String localFileSystemPath) {
    file.copy(path);
  }

FileSystemException (FileSystemException: No such file or directory, path = 'Users' (OS Error: No such file or directory, errno = 2))

I assume this happens because file.copy tries to copy into a new file on its own filesystem (MemoryFileSystem) but the local file system's /Users directory isn't there. What is the recommended way to copy a file from one FileSystem to another? And can this be done without making an extra copy of the data in memory?

The following does work, but requires the entire file to be read into memory before writing can even begin. Maybe this isn't an issue because it's already in memory (it's on a MemoryFileSystem), and the readAsBytesSync call shortcuts and returns a reference to the existing list of bytes? Just trying to figure out the best way to write a potentially very large file from memory to disk, and this doesn't seem like it. Can't this be done in chunks with Streams to avoid having to read the whole file at once?

  File copyFile(File memoryFileSystemFile, String localFileSystemPath) {
    List bytes = memoryFileSystemFile.readAsBytesSync();
    File newFile = LocalFileSystem().file(localFileSystemPath);
    newFile.writeAsBytesSync(bytes);
    return newFile;
  }
@dnfield
Copy link
Contributor

dnfield commented May 18, 2020

memoryFilesystemFile.readAsBytesSync() shouldn't need to do any new allocations - the "file" is already basically just a Uint8List in memory. How would streams help here?

@mymikemiller
Copy link
Author

I guess I figured there would be a way to read a file as a stream, where only small chunks were ever loaded into memory at a time as they are, in my case, written directly to a file on another FileSystem.

@dnfield
Copy link
Contributor

dnfield commented May 19, 2020

But in a MemoryFileSystem, the file is already just an in memory list of bytes, right?

In a regular file system implementation that would make sense, but right now Dart itself doesn't have API for that (you can only read files in their entirety in Dart). There's probably a bug in dart-lang/sdk somewhere for that....

@dnfield
Copy link
Contributor

dnfield commented May 19, 2020

Or I suppose you'd want a RandomAccessFile

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants