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

Add APIs that can be applied to any fileSystemEntry #13280

Closed
sigmundch opened this issue Sep 12, 2013 · 10 comments
Closed

Add APIs that can be applied to any fileSystemEntry #13280

sigmundch opened this issue Sep 12, 2013 · 10 comments
Labels
area-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-obsolete Closed as the reported issue is no longer relevant library-io P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug

Comments

@sigmundch
Copy link
Member

Would be great if we could make it simple to operate with the file system entities in abstract ways (when we don't care whether the entity is a link/file/or directory).

As an example. I just added this to my library, which I presume is something many others might need as well:

 void deleteIfPresent(String path) {
   try {
     var link = new Link(path);
     if (link.existsSync()) {
       link.deleteSync();
       return;
     }
 
     var dir = new Directory(path);
     if (dir.existsSync()) {
       dir.deleteSync(recursive: true);
       return;
     }
 
     var file = new File(path);
     if (file.existsSync()) {
       file.deleteSync();
     }
   } catch (e) {
     print('Error while deleting $path: $e');
   }
 }

It would be great if I could just do:

  FileSystemEntity.deleteSync(path, recursive: true);

@sgjesse
Copy link
Contributor

sgjesse commented Sep 13, 2013

We already have the following methods

  delete
  deleteSync
  exists
  existsSync
  rename
  renameSync
  stat
  statSync

When we moved delete and deleteSync to FileSystemEntity, we changed the behavior of delete with recursive true to always delete no matter the type of the receiver. So now you can do:

 void deleteIfPresent(String path) {
   try {
     var dir = new Directory(path);
     dir.deleteSync(recursive: true);
   } catch (e) {
     print('Error while deleting $path: $e');
   }
 }

Is this what you are looking for?


Added NeedsInfo label.

@DartBot
Copy link

DartBot commented Sep 13, 2013

This comment was originally written by @seaneagan


issue #12245 is related.

Is recursive ignored for Files and Links which represent Files?

I think having the following constructor would help:

  FileSystemEntity(String path);

also could add a "onAbsent" arg to delete(Sync):

void deleteSync({bool recursive: false, var onAbsent});

Then you could do:

deleteIfPresent(String path) =>
    new FileSystemEntity(path).deleteSync(recursive: true, onAbsent: () {});

and to delete Directory contents:

deleteDirectoryContents(Directory directory) =>
    directory.listSync().forEach((fse) => fse.deleteSync(recursive: true));

@sigmundch
Copy link
Member Author

Interesting, I didn't know that Directory.delete did what I wanted =).

However, I agree with Sean that having a constructor that treats a path just as a FSE would be great. Without it, my main concern is that doing:

    new Directory(path).deleteSync(recursive: true);

Feels like I'm assuming that 'path' is a directory and it should fail if it's not. Instead, the following:

    new FileSystemEntity(path).deleteSync(recursive: true, followSymlinks: false);

expresses my intent better: I don't care what 'path' is, but I want to delete it.


Added Triaged label.

@sgjesse
Copy link
Contributor

sgjesse commented Sep 16, 2013

The current constructors for File, Directory and Link does not initiate any IO - they just create an object with a path in it. There is not check whether this path exists, and the File, Directory and Link instance can be used to create the actual file system object.

The FileSystemEntity class is abstract so if we add a factory constructor to it taking a path it will need to check the type of the the actual file system object to determine whether to create an instance of File, Directory or Link. If there is no file system object with the given path this is not possible and constructor will have to fail.

Having a constructor on FileSystemEntity doen not really work well with the subclassing into File, Directory and Link.

@sgjesse
Copy link
Contributor

sgjesse commented Sep 16, 2013

Removed Type-Defect label.
Added Type-Enhancement label.

@DartBot
Copy link

DartBot commented Sep 16, 2013

This comment was originally written by @seaneagan


The FileSystemEntity constructor could return an instance of a class whose
methods each determine whether it is a file, directory, or link and
delegate accordingly. The only extra failure case from this I can think of
would be create(Sync) when the FSE doesn't already exist:

new FileSystemEntity(path).createSync(); // Fails if path doesn't exist.

@sgjesse
Copy link
Contributor

sgjesse commented Sep 25, 2013

Added this to the Later milestone.
Removed Priority-Unassigned label.
Added Priority-Low label.

@kevmoo
Copy link
Member

kevmoo commented May 14, 2014

Removed Area-IO label.
Added Library-IO, Area-Library labels.

@kasperl
Copy link

kasperl commented Jul 10, 2014

Removed this from the Later milestone.
Added Oldschool-Milestone-Later label.

@kasperl
Copy link

kasperl commented Aug 4, 2014

Removed Oldschool-Milestone-Later label.

@sigmundch sigmundch added Type-Enhancement P3 A lower priority bug or feature request library-io area-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. labels Aug 4, 2014
@kevmoo kevmoo added type-enhancement A request for a change that isn't a bug and removed triaged labels Feb 29, 2016
@sigmundch sigmundch added the closed-obsolete Closed as the reported issue is no longer relevant label Jul 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-library SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries. closed-obsolete Closed as the reported issue is no longer relevant library-io P3 A lower priority bug or feature request type-enhancement A request for a change that isn't a bug
Projects
None yet
Development

No branches or pull requests

5 participants