Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(188)

Unified Diff: utils/pub/io.dart

Issue 13116020: Clean up the semantics of io.dart operations w.r.t. symlinks. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « utils/pub/entrypoint.dart ('k') | utils/pub/oauth2.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: utils/pub/io.dart
diff --git a/utils/pub/io.dart b/utils/pub/io.dart
index f4bb6de9e243da207bf8cd92126f792fea4efc90..a32c4714198b4e94c7e8969d3f1202177721bc63 100644
--- a/utils/pub/io.dart
+++ b/utils/pub/io.dart
@@ -28,12 +28,16 @@ bool isBeneath(String entry, String dir) {
}
/// Determines if a file or directory exists at [path].
-bool entryExists(String path) => dirExists(path) || fileExists(path);
+bool entryExists(String path) =>
+ dirExists(path) || fileExists(path) || linkExists(path);
-/// Determines if [file] exists on the file system. Will also return `true` if
-/// [file] points to a symlink, even a directory symlink.
-bool fileExists(String file) =>
- new File(file).existsSync() || new Link(file).existsSync();
+/// Returns whether [link] exists on the file system. This will return `true`
+/// for any symlink, regardless of what it points at or whether it's broken.
+bool linkExists(String path) => new Link(path).existsSync();
+
+/// Returns whether [file] exists on the file system. This will return `true`
+/// for a symlink only if that symlink is unbroken and points to a file.
+bool fileExists(String file) => new File(file).existsSync();
/// Reads the contents of the text file [file].
String readTextFile(String file) =>
@@ -61,11 +65,6 @@ String writeTextFile(String file, String contents, {dontLogContents: false}) {
return file;
}
-/// Deletes [file].
-void deleteFile(String file) {
- new File(file).deleteSync();
-}
-
/// Creates [file] and writes [contents] to it.
String writeBinaryFile(String file, List<int> contents) {
log.io("Writing ${contents.length} bytes to binary file $file.");
@@ -126,12 +125,6 @@ String createTempDir([dir = '']) {
return tempDir.path;
}
-/// Recursively deletes [dir].
-void deleteDir(String dir) {
- log.io("Deleting directory $dir.");
- new Directory(dir).deleteSync(recursive: true);
-}
-
/// Asynchronously lists the contents of [dir]. If [recursive] is `true`, lists
/// subdirectory contents (defaults to `false`). If [includeHiddenFiles] is
/// `true`, includes files and directories beginning with `.` (defaults to
@@ -199,21 +192,29 @@ Future<List<String>> listDir(String dir,
return doList(dir, new Set<String>());
}
-/// Determines if [dir] exists on the file system.
+/// Returns whether [dir] exists on the file system. This will return `true` for
+/// a symlink only if that symlink is unbroken and points to a directory.
bool dirExists(String dir) => new Directory(dir).existsSync();
+/// Deletes whatever's at [path], whether it's a file, directory, or symlink. If
+/// it's a directory, it will be deleted recursively.
+void deleteEntry(String path) {
+ if (linkExists(path)) {
+ log.io("Deleting link $path.");
+ new Link(path).deleteSync();
+ } else if (dirExists(path)) {
+ log.io("Deleting directory $path.");
+ new Directory(path).deleteSync(recursive: true);
+ } else {
+ log.io("Deleting file $path.");
+ new File(path).deleteSync();
+ }
+}
+
/// "Cleans" [dir]. If that directory already exists, it will be deleted. Then a
/// new empty directory will be created.
void cleanDir(String dir) {
- if (dirExists(dir)) {
- // Delete it first.
- deleteDir(dir);
- } else if (fileExists(dir)) {
- // If there is a non-directory there (file or symlink), delete it.
- deleteFile(dir);
- }
-
- // Just create it.
+ if (entryExists(dir)) deleteEntry(dir);
createDir(dir);
}
@@ -582,7 +583,7 @@ Future withTempDir(Future fn(String path)) {
return defer(() {
var tempDir = createTempDir();
return new Future.of(() => fn(tempDir))
- .whenComplete(() => deleteDir(tempDir));
+ .whenComplete(() => deleteEntry(tempDir));
});
}
« no previous file with comments | « utils/pub/entrypoint.dart ('k') | utils/pub/oauth2.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698