| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 /** | |
| 6 * [Directory] objects are used for working with directories. | |
| 7 */ | |
| 8 abstract class Directory { | |
| 9 /** | |
| 10 * Creates a directory object. The path is either an absolute path, | |
| 11 * or it is a relative path which is interpreted relative to the directory | |
| 12 * in which the Dart VM was started. | |
| 13 */ | |
| 14 factory Directory(String path) => new _Directory(path); | |
| 15 | |
| 16 /** | |
| 17 * Creates a directory object from a Path object. The path is either | |
| 18 * an absolute path, or it is a relative path which is interpreted | |
| 19 * relative to the directory in which the Dart VM was started. | |
| 20 */ | |
| 21 factory Directory.fromPath(Path path) => new _Directory.fromPath(path); | |
| 22 | |
| 23 /** | |
| 24 * Creates a directory object pointing to the current working | |
| 25 * directory. | |
| 26 */ | |
| 27 factory Directory.current() => new _Directory.current(); | |
| 28 | |
| 29 /** | |
| 30 * Check whether a directory with this name already exists. Returns | |
| 31 * a [:Future<bool>:] that completes with the result. | |
| 32 */ | |
| 33 Future<bool> exists(); | |
| 34 | |
| 35 /** | |
| 36 * Synchronously check whether a directory with this name already exists. | |
| 37 */ | |
| 38 bool existsSync(); | |
| 39 | |
| 40 /** | |
| 41 * Creates the directory with this name. If the directory already | |
| 42 * exists nothing is done. Returns a [:Future<Directory>:] that | |
| 43 * completes with this directory once it has been created. If the | |
| 44 * directory does not exist and cannot be created the future | |
| 45 * completes with an exception. | |
| 46 */ | |
| 47 Future<Directory> create(); | |
| 48 | |
| 49 /** | |
| 50 * Synchronously creates the directory with this name. If the | |
| 51 * directory already exists nothing is done. If the directory does | |
| 52 * not exist and cannot be created an exception is thrown. | |
| 53 */ | |
| 54 void createSync(); | |
| 55 | |
| 56 /** | |
| 57 * Creates a temporary directory with a name based on the current | |
| 58 * path. This name and path is used as a template, and additional | |
| 59 * characters are appended to it by the call to make a unique | |
| 60 * directory name. If the path is the empty string, a default | |
| 61 * system temp directory and name are used for the template. | |
| 62 * | |
| 63 * Returns a [:Future<Directory>:] that completes with the newly | |
| 64 * created temporary directory. | |
| 65 */ | |
| 66 Future<Directory> createTemp(); | |
| 67 | |
| 68 /** | |
| 69 * Synchronously creates a temporary directory with a name based on the | |
| 70 * current path. This name and path is used as a template, and additional | |
| 71 * characters are appended to it by the call to make a unique directory name. | |
| 72 * If the path is the empty string, a default system temp directory and name | |
| 73 * are used for the template. Returns the newly created temporary directory. | |
| 74 */ | |
| 75 Directory createTempSync(); | |
| 76 | |
| 77 /** | |
| 78 * Deletes the directory with this name. The directory must be | |
| 79 * empty. Returns a [:Future<Directory>:] that completes with | |
| 80 * this directory when the deletion is done. | |
| 81 */ | |
| 82 Future<Directory> delete(); | |
| 83 | |
| 84 /** | |
| 85 * Synchronously deletes the directory with this name. The directory | |
| 86 * must be empty. Throws an exception if the directory cannot be | |
| 87 * deleted. | |
| 88 */ | |
| 89 void deleteSync(); | |
| 90 | |
| 91 /** | |
| 92 * Deletes this directory and all sub-directories and files in the | |
| 93 * directories. Returns a [:Future<Directory>:] that completes with | |
| 94 * this directory when the deletion is done. | |
| 95 */ | |
| 96 Future<Directory> deleteRecursively(); | |
| 97 | |
| 98 /** | |
| 99 * Synchronously deletes this directory and all sub-directories and | |
| 100 * files in the directories. Throws an exception if the directory | |
| 101 * cannot be deleted. | |
| 102 */ | |
| 103 void deleteRecursivelySync(); | |
| 104 | |
| 105 /** | |
| 106 * Rename this directory. Returns a [:Future<Directory>:] that completes | |
| 107 * with a [Directory] instance for the renamed directory. | |
| 108 * | |
| 109 * If newPath identifies an existing directory, that directory is | |
| 110 * replaced. If newPath identifies an existing file the operation | |
| 111 * fails and the future completes with an exception. | |
| 112 */ | |
| 113 Future<Directory> rename(String newPath); | |
| 114 | |
| 115 /** | |
| 116 * Synchronously rename this directory. Returns a [Directory] | |
| 117 * instance for the renamed directory. | |
| 118 * | |
| 119 * If newPath identifies an existing directory, that directory is | |
| 120 * replaced. If newPath identifies an existing file the operation | |
| 121 * fails and an exception is thrown. | |
| 122 */ | |
| 123 Directory renameSync(String newPath); | |
| 124 | |
| 125 /** | |
| 126 * List the sub-directories and files of this | |
| 127 * [Directory]. Optionally recurse into sub-directories. Returns a | |
| 128 * [DirectoryLister] object representing the active listing | |
| 129 * operation. Handlers for files and directories should be | |
| 130 * registered on this DirectoryLister object. | |
| 131 */ | |
| 132 DirectoryLister list({bool recursive: false}); | |
| 133 | |
| 134 /** | |
| 135 * Gets the path of this directory. | |
| 136 */ | |
| 137 final String path; | |
| 138 } | |
| 139 | |
| 140 | |
| 141 /** | |
| 142 * A [DirectoryLister] represents an actively running listing operation. | |
| 143 * | |
| 144 * For each file and directory, the file or directory handler is | |
| 145 * called. When all directories have been listed the done handler is | |
| 146 * called. If the listing operation is recursive, the error handler is | |
| 147 * called if a subdirectory cannot be opened for listing. | |
| 148 */ | |
| 149 abstract class DirectoryLister { | |
| 150 /** | |
| 151 * Sets the directory handler that is called for all directories | |
| 152 * during listing. The directory handler is called with the full | |
| 153 * path of the directory. | |
| 154 */ | |
| 155 void set onDir(void onDir(String dir)); | |
| 156 | |
| 157 /** | |
| 158 * Sets the handler that is called for all files during listing. The | |
| 159 * file handler is called with the full path of the file. | |
| 160 */ | |
| 161 void set onFile(void onFile(String file)); | |
| 162 | |
| 163 /** | |
| 164 * Set the handler that is called when a listing is done. The | |
| 165 * handler is called with an indication of whether or not the | |
| 166 * listing operation completed. | |
| 167 */ | |
| 168 void set onDone(void onDone(bool completed)); | |
| 169 | |
| 170 /** | |
| 171 * Sets the handler that is called if there is an error while | |
| 172 * listing directories. | |
| 173 */ | |
| 174 void set onError(void onError(e)); | |
| 175 } | |
| 176 | |
| 177 | |
| 178 class DirectoryIOException implements Exception { | |
| 179 const DirectoryIOException([String this.message = "", | |
| 180 String this.path = "", | |
| 181 OSError this.osError = null]); | |
| 182 String toString() { | |
| 183 StringBuffer sb = new StringBuffer(); | |
| 184 sb.add("DirectoryIOException"); | |
| 185 if (!message.isEmpty) { | |
| 186 sb.add(": $message"); | |
| 187 if (path != null) { | |
| 188 sb.add(", path = $path"); | |
| 189 } | |
| 190 if (osError != null) { | |
| 191 sb.add(" ($osError)"); | |
| 192 } | |
| 193 } else if (osError != null) { | |
| 194 sb.add(": $osError"); | |
| 195 if (path != null) { | |
| 196 sb.add(", path = $path"); | |
| 197 } | |
| 198 } | |
| 199 return sb.toString(); | |
| 200 } | |
| 201 final String message; | |
| 202 final String path; | |
| 203 final OSError osError; | |
| 204 } | |
| OLD | NEW |