OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 /** | 5 /** |
6 * [Directory] objects are used for working with directories. | 6 * [Directory] objects are used for working with directories. |
7 */ | 7 */ |
8 interface Directory default _Directory { | 8 interface Directory default _Directory { |
9 /** | 9 /** |
10 * Creates a directory object. The path is either a full path or | 10 * Creates a directory object. The path is either a full path or |
11 * relative to the directory in which the Dart VM was | 11 * relative to the directory in which the Dart VM was |
12 * started. | 12 * started. |
13 */ | 13 */ |
14 Directory(String path); | 14 Directory(String path); |
15 | 15 |
16 /** | 16 /** |
17 * Check whether a directory with this name already exists. If the | 17 * Check whether a directory with this name already exists. If the |
18 * operation completes successfully the callback is called with the | 18 * operation completes successfully the [existsHandler] is called with |
19 * result. Otherwise [onError] is called. | 19 * the result. Otherwise the [errorHandler] is called. |
20 */ | 20 */ |
21 void exists(void callback(bool exists)); | 21 void exists(); |
22 | 22 |
23 /** | 23 /** |
24 * Synchronously check whether a directory with this name already exists. | 24 * Synchronously check whether a directory with this name already exists. |
25 */ | 25 */ |
26 bool existsSync(); | 26 bool existsSync(); |
27 | 27 |
28 /** | 28 /** |
29 * Creates the directory with this name if it does not exist. If | 29 * Creates the directory with this name if it does not exist. |
30 * the directory is successfully created the callback is | 30 * If the directory is successfully created the [createHandler] is |
31 * called. Otherwise [onError] is called. | 31 * called. Otherwise the [errorHandler] is called. |
32 */ | 32 */ |
33 void create(void callback()); | 33 void create(); |
34 | 34 |
35 /** | 35 /** |
36 * Synchronously creates the directory with this name if it does not exist. | 36 * Synchronously creates the directory with this name if it does not exist. |
37 * Throws an exception if the directory already exists. | 37 * Throws an exception if the directory already exists. |
38 */ | 38 */ |
39 void createSync(); | 39 void createSync(); |
40 | 40 |
41 /** | 41 /** |
42 * Creates a temporary directory with a name based on the current | 42 * Creates a temporary directory with a name based on the current path. |
43 * path. This name and path is used as a template, and additional | 43 * This name and path is used as a template, and additional characters are |
44 * characters are appended to it by the call to make a unique | 44 * appended to it by the call to make a unique directory name. If the |
45 * directory name. If the path is the empty string, a default | 45 * path is the empty string, a default system temp directory and name |
46 * system temp directory and name are used for the template. | 46 * are used for the template. |
47 * | 47 * The path is modified to be the path of the new directory. |
48 * The path is modified to be the path of the new directory. After | 48 * After the new directory is created, and the path modified, the callback |
49 * the new directory is created, and the path modified, the callback | 49 * createTempHandler will be called. The error handler is called if |
50 * will be called. The error handler is called if the temporary | 50 * the temporary directory cannot be created. |
51 * directory cannot be created. | |
52 */ | 51 */ |
53 void createTemp(void callback()); | 52 void createTemp(); |
54 | 53 |
55 /** | 54 /** |
56 * Synchronously creates a temporary directory with a name based on the | 55 * Synchronously creates a temporary directory with a name based on the |
57 * current path. This name and path is used as a template, and additional | 56 * current path. This name and path is used as a template, and additional |
58 * characters are appended to it by the call to make a unique directory name. | 57 * characters are appended to it by the call to make a unique directory name. |
59 * If the path is the empty string, a default system temp directory and name | 58 * If the path is the empty string, a default system temp directory and name |
60 * are used for the template. | 59 * are used for the template. |
61 * The path is modified to be the path of the new directory. | 60 * The path is modified to be the path of the new directory. |
62 */ | 61 */ |
63 void createTempSync(); | 62 void createTempSync(); |
64 | 63 |
65 /** | 64 /** |
66 * Deletes the directory with this name. The directory must be | 65 * Deletes the directory with this name. If the operation completes |
67 * empty. If the operation completes successfully the callback is | 66 * successfully the [deleteHandler] is called. Otherwise the |
68 * called. Otherwise [onError] is called. | 67 * [errorHandler] is called. |
| 68 * |
| 69 * If [recursive] is [:true:] this directory and all sub-directories |
| 70 * and files in the directory are deleted. If [recursive] is |
| 71 * [:false:] only this directory (which must be empty) is |
| 72 * deleted. [recursive] is [:false:] by default. |
69 */ | 73 */ |
70 void delete(void callback()); | 74 void delete([bool recursive]); |
71 | 75 |
72 /** | 76 /** |
73 * Deletes this directory and all sub-directories and files in the | 77 * Deletes the directory with this name. Throws an exception |
74 * directories. If the operation completes successfully the callback | 78 * if the directory cannot be deleted. |
75 * is called. Otherwise [onError] is called. | 79 * |
| 80 * If [recursive] is [:true:] this directory and all sub-directories |
| 81 * and files in the directory are deleted. If [recursive] is |
| 82 * [:false:] only this directory (which must be empty) is |
| 83 * deleted. [recursive] is [:false:] by default. |
76 */ | 84 */ |
77 void deleteRecursively(void callback()); | 85 void deleteSync([bool recursive]); |
78 | |
79 /** | |
80 * Synchronously deletes the directory with this name. The directory | |
81 * must be empty. Throws an exception if the directory cannot be | |
82 * deleted. | |
83 */ | |
84 void deleteSync(); | |
85 | |
86 /** | |
87 * Synchronously deletes this directory and all sub-directories and | |
88 * files in the directories. Throws an exception if the directory | |
89 * cannot be deleted. | |
90 */ | |
91 void deleteRecursivelySync(); | |
92 | 86 |
93 /** | 87 /** |
94 * List the sub-directories and files of this | 88 * List the sub-directories and files of this |
95 * [Directory]. Optionally recurse into sub-directories. For each | 89 * [Directory]. Optionally recurse into sub-directories. For each |
96 * file and directory, the file or directory handler is called. When | 90 * file and directory, the file or directory handler is called. When |
97 * all directories have been listed the done handler is called. If | 91 * all directories have been listed the done handler is called. If |
98 * the listing operation is recursive, the error handler is called | 92 * the listing operation is recursive, the error handler is called |
99 * if a subdirectory cannot be opened for listing. | 93 * if a subdirectory cannot be opened for listing. |
100 */ | 94 */ |
101 // TODO(ager): Should we change this to return an event emitting | |
102 // DirectoryLister object. Alternatively, pass in one callback that | |
103 // gets called with an indication of whether what it is called with | |
104 // is a file, a directory or an indication that the listing is over. | |
105 void list([bool recursive]); | 95 void list([bool recursive]); |
106 | 96 |
107 /** | 97 /** |
108 * Sets the directory handler that is called for all directories | 98 * Sets the directory handler that is called for all directories |
109 * during listing operations. The directory handler is called with | 99 * during listing operations. The directory handler is called with |
110 * the full path of the directory. | 100 * the full path of the directory. |
111 */ | 101 */ |
112 void set onDir(void onDir(String dir)); | 102 void set dirHandler(void dirHandler(String dir)); |
113 | 103 |
114 /** | 104 /** |
115 * Sets the handler that is called for all files during listing | 105 * Sets the handler that is called for all files during listing |
116 * operations. The file handler is called with the full path of the | 106 * operations. The file handler is called with the full path of the |
117 * file. | 107 * file. |
118 */ | 108 */ |
119 void set onFile(void onFile(String file)); | 109 void set fileHandler(void fileHandler(String file)); |
120 | 110 |
121 /** | 111 /** |
122 * Set the handler that is called when a directory listing is | 112 * Set the handler that is called when a directory listing is |
123 * done. The handler is called with an indication of whether or not | 113 * done. The handler is called with an indication of whether or not |
124 * the listing operation completed. | 114 * the listing operation completed. |
125 */ | 115 */ |
126 void set onDone(void onDone(bool completed)); | 116 void set doneHandler(void doneHandler(bool completed)); |
| 117 |
| 118 /** |
| 119 * Set the handler that is called when checking if a directory with this |
| 120 * name exists. |
| 121 */ |
| 122 void set existsHandler(void existsHandler(bool exists)); |
| 123 |
| 124 /** |
| 125 * Set the handler that is called when a directory is successfully created. |
| 126 */ |
| 127 void set createHandler(void createHandler()); |
| 128 |
| 129 /** |
| 130 * Set the handler that is called when a temporary directory is |
| 131 * successfully created. |
| 132 */ |
| 133 void set createTempHandler(void createTempHandler()); |
| 134 |
| 135 /** |
| 136 * Set the handler that is called when a directory is successfully |
| 137 * deleted. |
| 138 */ |
| 139 void set deleteHandler(void deleteHandler()); |
127 | 140 |
128 /** | 141 /** |
129 * Sets the handler that is called if there is an error while listing | 142 * Sets the handler that is called if there is an error while listing |
130 * or creating directories. | 143 * or creating directories. |
131 */ | 144 */ |
132 void set onError(void onError(String error)); | 145 void set errorHandler(void errorHandler(String error)); |
133 | 146 |
134 /** | 147 /** |
135 * Gets the path of this directory. | 148 * Gets the path of this directory. |
136 */ | 149 */ |
137 final String path; | 150 final String path; |
138 } | 151 } |
139 | 152 |
140 | 153 |
141 class DirectoryException { | 154 class DirectoryException { |
142 const DirectoryException([String this.message, int this.errorCode = 0]); | 155 const DirectoryException([String this.message, int this.errorCode = 0]); |
143 String toString() => "DirectoryException: $message"; | 156 String toString() => "DirectoryException: $message"; |
144 final String message; | 157 final String message; |
145 final int errorCode; | 158 final int errorCode; |
146 } | 159 } |
OLD | NEW |