| OLD | NEW |
| 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 <errno.h> | 5 #include <errno.h> |
| 6 #include <fcntl.h> | 6 #include <fcntl.h> |
| 7 #include <sys/stat.h> | 7 #include <sys/stat.h> |
| 8 #include <unistd.h> | 8 #include <unistd.h> |
| 9 #include <libgen.h> | 9 #include <libgen.h> |
| 10 #include <limits.h> | 10 #include <limits.h> |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 85 if (position < 0) { | 85 if (position < 0) { |
| 86 // The file is not capable of seeking. Return an error. | 86 // The file is not capable of seeking. Return an error. |
| 87 return -1; | 87 return -1; |
| 88 } | 88 } |
| 89 off_t result = lseek(handle_->fd(), 0, SEEK_END); | 89 off_t result = lseek(handle_->fd(), 0, SEEK_END); |
| 90 lseek(handle_->fd(), position, SEEK_SET); | 90 lseek(handle_->fd(), position, SEEK_SET); |
| 91 return result; | 91 return result; |
| 92 } | 92 } |
| 93 | 93 |
| 94 | 94 |
| 95 File* File::OpenFile(const char* name, bool writable) { | 95 File* File::Open(const char* name, bool writable) { |
| 96 int flags = O_RDONLY; | 96 int flags = O_RDONLY; |
| 97 if (writable) { | 97 if (writable) { |
| 98 flags = (O_RDWR | O_CREAT | O_TRUNC); | 98 flags = (O_RDWR | O_CREAT | O_TRUNC); |
| 99 } | 99 } |
| 100 int fd = open(name, flags, 0666); | 100 int fd = open(name, flags, 0666); |
| 101 if (fd < 0) { | 101 if (fd < 0) { |
| 102 return NULL; | 102 return NULL; |
| 103 } | 103 } |
| 104 return new File(name, new FileHandle(fd)); | 104 return new File(name, new FileHandle(fd)); |
| 105 } | 105 } |
| 106 | 106 |
| 107 | 107 |
| 108 bool File::FileExists(const char* name) { | 108 bool File::Exists(const char* name) { |
| 109 struct stat st; | 109 struct stat st; |
| 110 if (stat(name, &st) == 0) { | 110 if (stat(name, &st) == 0) { |
| 111 return S_ISREG(st.st_mode); // Deal with symlinks? | 111 return S_ISREG(st.st_mode); // Deal with symlinks? |
| 112 } else { | 112 } else { |
| 113 return false; | 113 return false; |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 | 116 |
| 117 | 117 |
| 118 bool File::IsAbsolutePath(const char* pathname) { | 118 bool File::IsAbsolutePath(const char* pathname) { |
| 119 return (pathname != NULL && pathname[0] == '/'); | 119 return (pathname != NULL && pathname[0] == '/'); |
| 120 } | 120 } |
| 121 | 121 |
| 122 | 122 |
| 123 bool File::Create(const char* name) { |
| 124 int fd = open(name, O_RDONLY | O_CREAT, 0666); |
| 125 if (fd < 0) { |
| 126 return false; |
| 127 } |
| 128 return (close(fd) == 0); |
| 129 } |
| 130 |
| 131 |
| 123 char* File::GetCanonicalPath(const char* pathname) { | 132 char* File::GetCanonicalPath(const char* pathname) { |
| 124 char* abs_path = NULL; | 133 char* abs_path = NULL; |
| 125 if (pathname != NULL) { | 134 if (pathname != NULL) { |
| 126 // On some older MacOs versions the default behaviour of realpath allocating | 135 // On some older MacOs versions the default behaviour of realpath allocating |
| 127 // space for the resolved_path when a NULL is passed in does not seem to | 136 // space for the resolved_path when a NULL is passed in does not seem to |
| 128 // work, so we explicitly allocate space. The caller is responsible for | 137 // work, so we explicitly allocate space. The caller is responsible for |
| 129 // freeing this space as in a regular realpath call. | 138 // freeing this space as in a regular realpath call. |
| 130 char* resolved_path = reinterpret_cast<char*>(malloc(PATH_MAX+1)); | 139 char* resolved_path = reinterpret_cast<char*>(malloc(PATH_MAX+1)); |
| 131 ASSERT(resolved_path != NULL); | 140 ASSERT(resolved_path != NULL); |
| 132 abs_path = realpath(pathname, resolved_path); | 141 abs_path = realpath(pathname, resolved_path); |
| 133 assert(abs_path == NULL || IsAbsolutePath(abs_path)); | 142 assert(abs_path == NULL || IsAbsolutePath(abs_path)); |
| 134 } | 143 } |
| 135 return abs_path; | 144 return abs_path; |
| 136 } | 145 } |
| 137 | 146 |
| 138 | 147 |
| 139 const char* File::PathSeparator() { | 148 const char* File::PathSeparator() { |
| 140 return "/"; | 149 return "/"; |
| 141 } | 150 } |
| 142 | 151 |
| 143 | 152 |
| 144 const char* File::StringEscapedPathSeparator() { | 153 const char* File::StringEscapedPathSeparator() { |
| 145 return "/"; | 154 return "/"; |
| 146 } | 155 } |
| OLD | NEW |