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