Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(147)

Side by Side Diff: runtime/bin/file_linux.cc

Issue 10065013: Support length operation without opening file. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Fix error message and fix Windows port Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « runtime/bin/file_impl.dart ('k') | runtime/bin/file_macos.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 <errno.h> 7 #include <errno.h>
8 #include <fcntl.h> 8 #include <fcntl.h>
9 #include <sys/stat.h> 9 #include <sys/stat.h>
10 #include <unistd.h> 10 #include <unistd.h>
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
86 86
87 87
88 bool File::Flush() { 88 bool File::Flush() {
89 ASSERT(handle_->fd() >= 0); 89 ASSERT(handle_->fd() >= 0);
90 return TEMP_FAILURE_RETRY(fsync(handle_->fd()) != -1); 90 return TEMP_FAILURE_RETRY(fsync(handle_->fd()) != -1);
91 } 91 }
92 92
93 93
94 off_t File::Length() { 94 off_t File::Length() {
95 ASSERT(handle_->fd() >= 0); 95 ASSERT(handle_->fd() >= 0);
96 off_t position = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_CUR)); 96 struct stat st;
97 if (position < 0) { 97 if (TEMP_FAILURE_RETRY(fstat(handle_->fd(), &st)) == 0) {
98 // The file is not capable of seeking. Return an error. 98 return st.st_size;
99 return -1;
100 } 99 }
101 off_t result = TEMP_FAILURE_RETRY(lseek(handle_->fd(), 0, SEEK_END)); 100 return -1;
102 TEMP_FAILURE_RETRY(lseek(handle_->fd(), position, SEEK_SET));
103 return result;
104 } 101 }
105 102
106 103
107 File* File::Open(const char* name, FileOpenMode mode) { 104 File* File::Open(const char* name, FileOpenMode mode) {
108 // Report errors for non-regular files. 105 // Report errors for non-regular files.
109 struct stat st; 106 struct stat st;
110 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) { 107 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) {
111 if (!S_ISREG(st.st_mode)) { 108 if (!S_ISREG(st.st_mode)) {
112 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT; 109 errno = (S_ISDIR(st.st_mode)) ? EISDIR : ENOENT;
113 return NULL; 110 return NULL;
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 158
162 bool File::Delete(const char* name) { 159 bool File::Delete(const char* name) {
163 int status = TEMP_FAILURE_RETRY(remove(name)); 160 int status = TEMP_FAILURE_RETRY(remove(name));
164 if (status == -1) { 161 if (status == -1) {
165 return false; 162 return false;
166 } 163 }
167 return true; 164 return true;
168 } 165 }
169 166
170 167
168 off_t File::LengthFromName(const char* name) {
169 struct stat st;
170 if (TEMP_FAILURE_RETRY(stat(name, &st)) == 0) {
171 return st.st_size;
172 }
173 return -1;
174 }
175
176
171 bool File::IsAbsolutePath(const char* pathname) { 177 bool File::IsAbsolutePath(const char* pathname) {
172 return (pathname != NULL && pathname[0] == '/'); 178 return (pathname != NULL && pathname[0] == '/');
173 } 179 }
174 180
175 181
176 char* File::GetCanonicalPath(const char* pathname) { 182 char* File::GetCanonicalPath(const char* pathname) {
177 char* abs_path = NULL; 183 char* abs_path = NULL;
178 if (pathname != NULL) { 184 if (pathname != NULL) {
179 do { 185 do {
180 abs_path = realpath(pathname, NULL); 186 abs_path = realpath(pathname, NULL);
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
220 int result = fstat(fd, &buf); 226 int result = fstat(fd, &buf);
221 if (result == -1) { 227 if (result == -1) {
222 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno)); 228 FATAL2("Failed stat on file descriptor %d: %s", fd, strerror(errno));
223 } 229 }
224 if (S_ISCHR(buf.st_mode)) return kTerminal; 230 if (S_ISCHR(buf.st_mode)) return kTerminal;
225 if (S_ISFIFO(buf.st_mode)) return kPipe; 231 if (S_ISFIFO(buf.st_mode)) return kPipe;
226 if (S_ISSOCK(buf.st_mode)) return kSocket; 232 if (S_ISSOCK(buf.st_mode)) return kSocket;
227 if (S_ISREG(buf.st_mode)) return kFile; 233 if (S_ISREG(buf.st_mode)) return kFile;
228 return kOther; 234 return kOther;
229 } 235 }
OLDNEW
« no previous file with comments | « runtime/bin/file_impl.dart ('k') | runtime/bin/file_macos.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698