| 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::Utf8ToSystemString(name); | 108 const char* system_name = StringUtils::Utf8ToConsoleString(name); |
| 109 int fd = open(system_name, flags, 0666); | 109 int fd = open(system_name, flags, 0666); |
| 110 free(const_cast<char*>(system_name)); | 110 free(const_cast<char*>(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::Utf8ToSystemString(name); | 132 const char* system_name = StringUtils::Utf8ToConsoleString(name); |
| 133 bool stat_status = stat(system_name, &st); | 133 bool stat_status = stat(system_name, &st); |
| 134 free(const_cast<char*>(system_name)); | 134 free(const_cast<char*>(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::Utf8ToSystemString(name); | 144 const char* system_name = StringUtils::Utf8ToConsoleString(name); |
| 145 int fd = open(system_name, O_RDONLY | O_CREAT, 0666); | 145 int fd = open(system_name, O_RDONLY | O_CREAT, 0666); |
| 146 free(const_cast<char*>(system_name)); | 146 free(const_cast<char*>(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::Utf8ToSystemString(name); | 155 const char* system_name = StringUtils::Utf8ToConsoleString(name); |
| 156 int status = remove(system_name); | 156 int status = remove(system_name); |
| 157 free(const_cast<char*>(system_name)); | 157 free(const_cast<char*>(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::Utf8ToSystemString(name); | 167 const char* system_name = StringUtils::Utf8ToConsoleString(name); |
| 168 int stat_status = stat(system_name, &st); | 168 int stat_status = stat(system_name, &st); |
| 169 free(const_cast<char*>(system_name)); | 169 free(const_cast<char*>(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::Utf8ToSystemString(name); | 179 const char* system_name = StringUtils::Utf8ToConsoleString(name); |
| 180 int stat_status = stat(system_name, &st); | 180 int stat_status = stat(system_name, &st); |
| 181 free(const_cast<char*>(system_name)); | 181 free(const_cast<char*>(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::Utf8ToSystemString(pathname); | 200 const char* system_name = StringUtils::Utf8ToConsoleString(pathname); |
| 201 int stat_status = stat(system_name, &st); | 201 int stat_status = stat(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<char*>(system_name)); |
| 205 return NULL; | 205 return NULL; |
| 206 } | 206 } |
| 207 int required_size = GetFullPathName(system_name, 0, NULL, NULL); | 207 int required_size = GetFullPathName(system_name, 0, NULL, NULL); |
| 208 char* path = static_cast<char*>(malloc(required_size)); | 208 char* path = static_cast<char*>(malloc(required_size)); |
| 209 int written = GetFullPathName(system_name, required_size, path, NULL); | 209 int written = GetFullPathName(system_name, required_size, path, NULL); |
| 210 free(const_cast<char*>(system_name)); | 210 free(const_cast<char*>(system_name)); |
| 211 ASSERT(written == (required_size - 1)); | 211 ASSERT(written == (required_size - 1)); |
| 212 char* result = StringUtils::SystemStringToUtf8(path); | 212 char* result = StringUtils::ConsoleStringToUtf8(path); |
| 213 free(path); | 213 free(path); |
| 214 return result; | 214 return result; |
| 215 } | 215 } |
| 216 | 216 |
| 217 | 217 |
| 218 char* File::GetContainingDirectory(char* pathname) { | 218 char* File::GetContainingDirectory(char* pathname) { |
| 219 struct stat st; | 219 struct stat st; |
| 220 char* system_name = StringUtils::Utf8ToSystemString(pathname); | 220 char* system_name = StringUtils::Utf8ToConsoleString(pathname); |
| 221 int stat_status = stat(system_name, &st); | 221 int stat_status = stat(system_name, &st); |
| 222 if (stat_status == 0) { | 222 if (stat_status == 0) { |
| 223 if ((st.st_mode & S_IFMT) != S_IFREG) { | 223 if ((st.st_mode & S_IFMT) != S_IFREG) { |
| 224 SetLastError(ERROR_FILE_NOT_FOUND); | 224 SetLastError(ERROR_FILE_NOT_FOUND); |
| 225 free(system_name); | 225 free(system_name); |
| 226 return NULL; | 226 return NULL; |
| 227 } | 227 } |
| 228 } else { | 228 } else { |
| 229 SetLastError(ERROR_FILE_NOT_FOUND); | 229 SetLastError(ERROR_FILE_NOT_FOUND); |
| 230 free(system_name); | 230 free(system_name); |
| 231 return NULL; | 231 return NULL; |
| 232 } | 232 } |
| 233 int required_size = GetFullPathName(system_name, 0, NULL, NULL); | 233 int required_size = GetFullPathName(system_name, 0, NULL, NULL); |
| 234 char* path = static_cast<char*>(malloc(required_size)); | 234 char* path = static_cast<char*>(malloc(required_size)); |
| 235 char* file_part = NULL; | 235 char* file_part = NULL; |
| 236 int written = | 236 int written = |
| 237 GetFullPathName(system_name, required_size, path, &file_part); | 237 GetFullPathName(system_name, required_size, path, &file_part); |
| 238 free(system_name); | 238 free(system_name); |
| 239 ASSERT(written == (required_size - 1)); | 239 ASSERT(written == (required_size - 1)); |
| 240 ASSERT(file_part != NULL); | 240 ASSERT(file_part != NULL); |
| 241 ASSERT(file_part > path); | 241 ASSERT(file_part > path); |
| 242 ASSERT(file_part[-1] == '\\'); | 242 ASSERT(file_part[-1] == '\\'); |
| 243 file_part[-1] = '\0'; | 243 file_part[-1] = '\0'; |
| 244 char* result = StringUtils::SystemStringToUtf8(path); | 244 char* result = StringUtils::ConsoleStringToUtf8(path); |
| 245 free(path); | 245 free(path); |
| 246 return result; | 246 return result; |
| 247 } | 247 } |
| 248 | 248 |
| 249 | 249 |
| 250 const char* File::PathSeparator() { | 250 const char* File::PathSeparator() { |
| 251 return "\\"; | 251 return "\\"; |
| 252 } | 252 } |
| 253 | 253 |
| 254 | 254 |
| 255 const char* File::StringEscapedPathSeparator() { | 255 const char* File::StringEscapedPathSeparator() { |
| 256 return "\\\\"; | 256 return "\\\\"; |
| 257 } | 257 } |
| 258 | 258 |
| 259 | 259 |
| 260 File::StdioHandleType File::GetStdioHandleType(int fd) { | 260 File::StdioHandleType File::GetStdioHandleType(int fd) { |
| 261 // Treat all stdio handles as pipes. The Windows event handler and | 261 // Treat all stdio handles as pipes. The Windows event handler and |
| 262 // socket code will handle the different handle types. | 262 // socket code will handle the different handle types. |
| 263 return kPipe; | 263 return kPipe; |
| 264 } | 264 } |
| OLD | NEW |