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 #include "bin/file.h" | 5 #include "bin/file.h" |
6 | 6 |
7 #include <fcntl.h> | 7 #include <fcntl.h> |
8 #include <io.h> | 8 #include <io.h> |
9 #include <stdio.h> | 9 #include <stdio.h> |
10 #include <string.h> | 10 #include <string.h> |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
97 | 97 |
98 | 98 |
99 File* File::Open(const char* name, FileOpenMode mode) { | 99 File* File::Open(const char* name, FileOpenMode mode) { |
100 int flags = O_RDONLY | O_BINARY | O_NOINHERIT; | 100 int flags = O_RDONLY | O_BINARY | O_NOINHERIT; |
101 if ((mode & kWrite) != 0) { | 101 if ((mode & kWrite) != 0) { |
102 flags = (O_RDWR | O_CREAT | O_BINARY | O_NOINHERIT); | 102 flags = (O_RDWR | O_CREAT | O_BINARY | O_NOINHERIT); |
103 } | 103 } |
104 if ((mode & kTruncate) != 0) { | 104 if ((mode & kTruncate) != 0) { |
105 flags = flags | O_TRUNC; | 105 flags = flags | O_TRUNC; |
106 } | 106 } |
107 int fd = open(name, flags, 0666); | 107 const char* system_name = StringUtils::Utf8ToSystemString(name); |
| 108 int fd = open(system_name, flags, 0666); |
| 109 free(const_cast<char*>(system_name)); |
108 if (fd < 0) { | 110 if (fd < 0) { |
109 return NULL; | 111 return NULL; |
110 } | 112 } |
111 if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) { | 113 if (((mode & kWrite) != 0) && ((mode & kTruncate) == 0)) { |
112 int position = lseek(fd, 0, SEEK_END); | 114 int position = lseek(fd, 0, SEEK_END); |
113 if (position < 0) { | 115 if (position < 0) { |
114 return NULL; | 116 return NULL; |
115 } | 117 } |
116 } | 118 } |
117 return new File(new FileHandle(fd)); | 119 return new File(new FileHandle(fd)); |
118 } | 120 } |
119 | 121 |
120 | 122 |
121 File* File::OpenStdio(int fd) { | 123 File* File::OpenStdio(int fd) { |
122 UNREACHABLE(); | 124 UNREACHABLE(); |
123 return NULL; | 125 return NULL; |
124 } | 126 } |
125 | 127 |
126 | 128 |
127 bool File::Exists(const char* name) { | 129 bool File::Exists(const char* name) { |
128 struct stat st; | 130 struct stat st; |
129 if (stat(name, &st) == 0) { | 131 const char* system_name = StringUtils::Utf8ToSystemString(name); |
| 132 bool stat_status = stat(system_name, &st); |
| 133 free(const_cast<char*>(system_name)); |
| 134 if (stat_status == 0) { |
130 return ((st.st_mode & S_IFMT) == S_IFREG); | 135 return ((st.st_mode & S_IFMT) == S_IFREG); |
131 } else { | 136 } else { |
132 return false; | 137 return false; |
133 } | 138 } |
134 } | 139 } |
135 | 140 |
136 | 141 |
137 bool File::Create(const char* name) { | 142 bool File::Create(const char* name) { |
138 int fd = open(name, O_RDONLY | O_CREAT, 0666); | 143 const char* system_name = StringUtils::Utf8ToSystemString(name); |
| 144 int fd = open(system_name, O_RDONLY | O_CREAT, 0666); |
| 145 free(const_cast<char*>(system_name)); |
139 if (fd < 0) { | 146 if (fd < 0) { |
140 return false; | 147 return false; |
141 } | 148 } |
142 return (close(fd) == 0); | 149 return (close(fd) == 0); |
143 } | 150 } |
144 | 151 |
145 | 152 |
146 bool File::Delete(const char* name) { | 153 bool File::Delete(const char* name) { |
147 int status = remove(name); | 154 const char* system_name = StringUtils::Utf8ToSystemString(name); |
| 155 int status = remove(system_name); |
| 156 free(const_cast<char*>(system_name)); |
148 if (status == -1) { | 157 if (status == -1) { |
149 return false; | 158 return false; |
150 } | 159 } |
151 return true; | 160 return true; |
152 } | 161 } |
153 | 162 |
154 | 163 |
155 off_t File::LengthFromName(const char* name) { | 164 off_t File::LengthFromName(const char* name) { |
156 struct stat st; | 165 struct stat st; |
157 if (stat(name, &st) == 0) { | 166 const char* system_name = StringUtils::Utf8ToSystemString(name); |
| 167 int stat_status = stat(system_name, &st); |
| 168 free(const_cast<char*>(system_name)); |
| 169 if (stat_status == 0) { |
158 return st.st_size; | 170 return st.st_size; |
159 } | 171 } |
160 return -1; | 172 return -1; |
161 } | 173 } |
162 | 174 |
163 | 175 |
164 time_t File::LastModified(const char* name) { | 176 time_t File::LastModified(const char* name) { |
165 struct stat st; | 177 struct stat st; |
166 if (stat(name, &st) == 0) { | 178 const char* system_name = StringUtils::Utf8ToSystemString(name); |
| 179 int stat_status = stat(system_name, &st); |
| 180 free(const_cast<char*>(system_name)); |
| 181 if (stat_status == 0) { |
167 return st.st_mtime; | 182 return st.st_mtime; |
168 } | 183 } |
169 return -1; | 184 return -1; |
170 } | 185 } |
171 | 186 |
172 | 187 |
173 bool File::IsAbsolutePath(const char* pathname) { | 188 bool File::IsAbsolutePath(const char* pathname) { |
174 // Should we consider network paths? | 189 // Should we consider network paths? |
175 if (pathname == NULL) return false; | 190 if (pathname == NULL) return false; |
176 return (strlen(pathname) > 2) && | 191 return (strlen(pathname) > 2) && |
177 (pathname[1] == ':') && | 192 (pathname[1] == ':') && |
178 (pathname[2] == '\\' || pathname[2] == '/'); | 193 (pathname[2] == '\\' || pathname[2] == '/'); |
179 } | 194 } |
180 | 195 |
181 | 196 |
182 char* File::GetCanonicalPath(const char* pathname) { | 197 char* File::GetCanonicalPath(const char* pathname) { |
183 struct stat st; | 198 struct stat st; |
184 if (stat(pathname, &st) != 0) { | 199 const char* system_name = StringUtils::Utf8ToSystemString(pathname); |
| 200 int stat_status = stat(system_name, &st); |
| 201 if (stat_status != 0) { |
185 SetLastError(ERROR_FILE_NOT_FOUND); | 202 SetLastError(ERROR_FILE_NOT_FOUND); |
| 203 free(const_cast<char*>(system_name)); |
186 return NULL; | 204 return NULL; |
187 } | 205 } |
188 int required_size = GetFullPathName(pathname, 0, NULL, NULL); | 206 int required_size = GetFullPathName(system_name, 0, NULL, NULL); |
189 char* path = static_cast<char*>(malloc(required_size)); | 207 char* path = static_cast<char*>(malloc(required_size)); |
190 int written = GetFullPathName(pathname, required_size, path, NULL); | 208 int written = GetFullPathName(system_name, required_size, path, NULL); |
| 209 free(const_cast<char*>(system_name)); |
191 ASSERT(written == (required_size - 1)); | 210 ASSERT(written == (required_size - 1)); |
192 return path; | 211 char* result = StringUtils::SystemStringToUtf8(path); |
| 212 free(path); |
| 213 return result; |
193 } | 214 } |
194 | 215 |
195 | 216 |
196 char* File::GetContainingDirectory(char* pathname) { | 217 char* File::GetContainingDirectory(char* pathname) { |
197 struct stat st; | 218 struct stat st; |
198 if (stat(pathname, &st) == 0) { | 219 char* system_name = StringUtils::Utf8ToSystemString(pathname); |
| 220 int stat_status = stat(system_name, &st); |
| 221 if (stat_status == 0) { |
199 if ((st.st_mode & S_IFMT) != S_IFREG) { | 222 if ((st.st_mode & S_IFMT) != S_IFREG) { |
200 SetLastError(ERROR_FILE_NOT_FOUND); | 223 SetLastError(ERROR_FILE_NOT_FOUND); |
| 224 free(system_name); |
201 return NULL; | 225 return NULL; |
202 } | 226 } |
203 } else { | 227 } else { |
204 SetLastError(ERROR_FILE_NOT_FOUND); | 228 SetLastError(ERROR_FILE_NOT_FOUND); |
| 229 free(system_name); |
205 return NULL; | 230 return NULL; |
206 } | 231 } |
207 int required_size = GetFullPathName(pathname, 0, NULL, NULL); | 232 int required_size = GetFullPathName(system_name, 0, NULL, NULL); |
208 char* path = static_cast<char*>(malloc(required_size)); | 233 char* path = static_cast<char*>(malloc(required_size)); |
209 char* file_part = NULL; | 234 char* file_part = NULL; |
210 int written = GetFullPathName(pathname, required_size, path, &file_part); | 235 int written = |
| 236 GetFullPathName(system_name, required_size, path, &file_part); |
| 237 free(system_name); |
211 ASSERT(written == (required_size - 1)); | 238 ASSERT(written == (required_size - 1)); |
212 ASSERT(file_part != NULL); | 239 ASSERT(file_part != NULL); |
213 ASSERT(file_part > path); | 240 ASSERT(file_part > path); |
214 ASSERT(file_part[-1] == '\\'); | 241 ASSERT(file_part[-1] == '\\'); |
215 file_part[-1] = '\0'; | 242 file_part[-1] = '\0'; |
216 return path; | 243 char* result = StringUtils::SystemStringToUtf8(path); |
| 244 free(path); |
| 245 return result; |
217 } | 246 } |
218 | 247 |
219 | 248 |
220 const char* File::PathSeparator() { | 249 const char* File::PathSeparator() { |
221 return "\\"; | 250 return "\\"; |
222 } | 251 } |
223 | 252 |
224 | 253 |
225 const char* File::StringEscapedPathSeparator() { | 254 const char* File::StringEscapedPathSeparator() { |
226 return "\\\\"; | 255 return "\\\\"; |
227 } | 256 } |
228 | 257 |
229 | 258 |
230 File::StdioHandleType File::GetStdioHandleType(int fd) { | 259 File::StdioHandleType File::GetStdioHandleType(int fd) { |
231 // Treat all stdio handles as pipes. The Windows event handler and | 260 // Treat all stdio handles as pipes. The Windows event handler and |
232 // socket code will handle the different handle types. | 261 // socket code will handle the different handle types. |
233 return kPipe; | 262 return kPipe; |
234 } | 263 } |
OLD | NEW |