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 [existsHandler] is called with | 18 * operation completes successfully the callback is called with the |
19 * the result. Otherwise the [errorHandler] is called. | 19 * result. Otherwise [onError] is called. |
20 */ | 20 */ |
21 void exists(); | 21 void exists(void callback(bool 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. | 29 * Creates the directory with this name if it does not exist. If |
30 * If the directory is successfully created the [createHandler] is | 30 * the directory is successfully created the callback is |
31 * called. Otherwise the [errorHandler] is called. | 31 * called. Otherwise [onError] is called. |
32 */ | 32 */ |
33 void create(); | 33 void create(void callback()); |
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 path. | 42 * Creates a temporary directory with a name based on the current |
43 * This name and path is used as a template, and additional characters are | 43 * path. This name and path is used as a template, and additional |
44 * appended to it by the call to make a unique directory name. If the | 44 * characters are appended to it by the call to make a unique |
45 * path is the empty string, a default system temp directory and name | 45 * directory name. If the path is the empty string, a default |
46 * are used for the template. | 46 * system temp directory and name are used for the template. |
47 * The path is modified to be the path of the new directory. | 47 * |
48 * After the new directory is created, and the path modified, the callback | 48 * The path is modified to be the path of the new directory. After |
49 * createTempHandler will be called. The error handler is called if | 49 * the new directory is created, and the path modified, the callback |
50 * the temporary directory cannot be created. | 50 * will be called. The error handler is called if the temporary |
| 51 * directory cannot be created. |
51 */ | 52 */ |
52 void createTemp(); | 53 void createTemp(void callback()); |
53 | 54 |
54 /** | 55 /** |
55 * Synchronously creates a temporary directory with a name based on the | 56 * Synchronously creates a temporary directory with a name based on the |
56 * current path. This name and path is used as a template, and additional | 57 * current path. This name and path is used as a template, and additional |
57 * characters are appended to it by the call to make a unique directory name. | 58 * characters are appended to it by the call to make a unique directory name. |
58 * If the path is the empty string, a default system temp directory and name | 59 * If the path is the empty string, a default system temp directory and name |
59 * are used for the template. | 60 * are used for the template. |
60 * The path is modified to be the path of the new directory. | 61 * The path is modified to be the path of the new directory. |
61 */ | 62 */ |
62 void createTempSync(); | 63 void createTempSync(); |
63 | 64 |
64 /** | 65 /** |
65 * Deletes the directory with this name. If the operation completes | 66 * Deletes the directory with this name. The directory must be |
66 * successfully the [deleteHandler] is called. Otherwise the | 67 * empty. If the operation completes successfully the callback is |
67 * [errorHandler] is called. | 68 * called. Otherwise [onError] 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. | |
73 */ | 69 */ |
74 void delete([bool recursive]); | 70 void delete(void callback()); |
75 | 71 |
76 /** | 72 /** |
77 * Deletes the directory with this name. Throws an exception | 73 * Deletes this directory and all sub-directories and files in the |
78 * if the directory cannot be deleted. | 74 * directories. If the operation completes successfully the callback |
79 * | 75 * is called. Otherwise [onError] is called. |
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. | |
84 */ | 76 */ |
85 void deleteSync([bool recursive]); | 77 void deleteRecursively(void callback()); |
| 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(); |
86 | 92 |
87 /** | 93 /** |
88 * List the sub-directories and files of this | 94 * List the sub-directories and files of this |
89 * [Directory]. Optionally recurse into sub-directories. For each | 95 * [Directory]. Optionally recurse into sub-directories. For each |
90 * file and directory, the file or directory handler is called. When | 96 * file and directory, the file or directory handler is called. When |
91 * all directories have been listed the done handler is called. If | 97 * all directories have been listed the done handler is called. If |
92 * the listing operation is recursive, the error handler is called | 98 * the listing operation is recursive, the error handler is called |
93 * if a subdirectory cannot be opened for listing. | 99 * if a subdirectory cannot be opened for listing. |
94 */ | 100 */ |
| 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. |
95 void list([bool recursive]); | 105 void list([bool recursive]); |
96 | 106 |
97 /** | 107 /** |
98 * Sets the directory handler that is called for all directories | 108 * Sets the directory handler that is called for all directories |
99 * during listing operations. The directory handler is called with | 109 * during listing operations. The directory handler is called with |
100 * the full path of the directory. | 110 * the full path of the directory. |
101 */ | 111 */ |
102 void set dirHandler(void dirHandler(String dir)); | 112 void set onDir(void onDir(String dir)); |
103 | 113 |
104 /** | 114 /** |
105 * Sets the handler that is called for all files during listing | 115 * Sets the handler that is called for all files during listing |
106 * operations. The file handler is called with the full path of the | 116 * operations. The file handler is called with the full path of the |
107 * file. | 117 * file. |
108 */ | 118 */ |
109 void set fileHandler(void fileHandler(String file)); | 119 void set onFile(void onFile(String file)); |
110 | 120 |
111 /** | 121 /** |
112 * Set the handler that is called when a directory listing is | 122 * Set the handler that is called when a directory listing is |
113 * done. The handler is called with an indication of whether or not | 123 * done. The handler is called with an indication of whether or not |
114 * the listing operation completed. | 124 * the listing operation completed. |
115 */ | 125 */ |
116 void set doneHandler(void doneHandler(bool completed)); | 126 void set onDone(void onDone(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()); | |
140 | 127 |
141 /** | 128 /** |
142 * Sets the handler that is called if there is an error while listing | 129 * Sets the handler that is called if there is an error while listing |
143 * or creating directories. | 130 * or creating directories. |
144 */ | 131 */ |
145 void set errorHandler(void errorHandler(String error)); | 132 void set onError(void onError(String error)); |
146 | 133 |
147 /** | 134 /** |
148 * Gets the path of this directory. | 135 * Gets the path of this directory. |
149 */ | 136 */ |
150 final String path; | 137 final String path; |
151 } | 138 } |
152 | 139 |
153 | 140 |
154 class DirectoryException { | 141 class DirectoryException { |
155 const DirectoryException([String this.message, int this.errorCode = 0]); | 142 const DirectoryException([String this.message, int this.errorCode = 0]); |
156 String toString() => "DirectoryException: $message"; | 143 String toString() => "DirectoryException: $message"; |
157 final String message; | 144 final String message; |
158 final int errorCode; | 145 final int errorCode; |
159 } | 146 } |
OLD | NEW |