2014-04-15 Richard Biener <rguenther@suse.de>
[official-gcc.git] / gcc / go / gofrontend / import.cc
blob4913100b5fddd17ba0db2a83bfe2cb1c2f52b0ac
1 // import.cc -- Go frontend import declarations.
3 // Copyright 2009 The Go Authors. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file.
7 #include "go-system.h"
9 #include "filenames.h"
10 #include "simple-object.h"
12 #include "go-c.h"
13 #include "gogo.h"
14 #include "lex.h"
15 #include "types.h"
16 #include "export.h"
17 #include "import.h"
19 #ifndef O_BINARY
20 #define O_BINARY 0
21 #endif
23 // The list of paths we search for import files.
25 static std::vector<std::string> search_path;
27 // Add a directory to the search path. This is called from the option
28 // handling language hook.
30 GO_EXTERN_C
31 void
32 go_add_search_path(const char* path)
34 search_path.push_back(std::string(path));
37 // Find import data. This searches the file system for FILENAME and
38 // returns a pointer to a Stream object to read the data that it
39 // exports. If the file is not found, it returns NULL.
41 // When FILENAME is not an absolute path and does not start with ./ or
42 // ../, we use the search path provided by -I and -L options.
44 // When FILENAME does start with ./ or ../, we use
45 // RELATIVE_IMPORT_PATH as a prefix.
47 // When FILENAME does not exist, we try modifying FILENAME to find the
48 // file. We use the first of these which exists:
49 // * We append ".gox".
50 // * We turn the base of FILENAME into libFILENAME.so.
51 // * We turn the base of FILENAME into libFILENAME.a.
52 // * We append ".o".
54 // When using a search path, we apply each of these transformations at
55 // each entry on the search path before moving on to the next entry.
56 // If the file exists, but does not contain any Go export data, we
57 // stop; we do not keep looking for another file with the same name
58 // later in the search path.
60 Import::Stream*
61 Import::open_package(const std::string& filename, Location location,
62 const std::string& relative_import_path)
64 bool is_local;
65 if (IS_ABSOLUTE_PATH(filename))
66 is_local = true;
67 else if (filename[0] == '.'
68 && (filename[1] == '\0' || IS_DIR_SEPARATOR(filename[1])))
69 is_local = true;
70 else if (filename[0] == '.'
71 && filename[1] == '.'
72 && (filename[2] == '\0' || IS_DIR_SEPARATOR(filename[2])))
73 is_local = true;
74 else
75 is_local = false;
77 std::string fn = filename;
78 if (is_local && !IS_ABSOLUTE_PATH(filename) && !relative_import_path.empty())
80 if (fn == ".")
82 // A special case.
83 fn = relative_import_path;
85 else
86 fn = relative_import_path + '/' + fn;
87 is_local = false;
90 if (!is_local)
92 for (std::vector<std::string>::const_iterator p = search_path.begin();
93 p != search_path.end();
94 ++p)
96 std::string indir = *p;
97 if (!indir.empty() && indir[indir.size() - 1] != '/')
98 indir += '/';
99 indir += fn;
100 Stream* s = Import::try_package_in_directory(indir, location);
101 if (s != NULL)
102 return s;
106 Stream* s = Import::try_package_in_directory(fn, location);
107 if (s != NULL)
108 return s;
110 return NULL;
113 // Try to find the export data for FILENAME.
115 Import::Stream*
116 Import::try_package_in_directory(const std::string& filename,
117 Location location)
119 std::string found_filename = filename;
120 int fd = open(found_filename.c_str(), O_RDONLY | O_BINARY);
122 if (fd >= 0)
124 struct stat s;
125 if (fstat(fd, &s) >= 0 && S_ISDIR(s.st_mode))
127 close(fd);
128 fd = -1;
129 errno = EISDIR;
133 if (fd < 0)
135 if (errno != ENOENT && errno != EISDIR)
136 warning_at(location, 0, "%s: %m", filename.c_str());
138 fd = Import::try_suffixes(&found_filename);
139 if (fd < 0)
140 return NULL;
143 // The export data may not be in this file.
144 Stream* s = Import::find_export_data(found_filename, fd, location);
145 if (s != NULL)
146 return s;
148 close(fd);
150 error_at(location, "%s exists but does not contain any Go export data",
151 found_filename.c_str());
153 return NULL;
156 // Given import "*PFILENAME", where *PFILENAME does not exist, try
157 // various suffixes. If we find one, set *PFILENAME to the one we
158 // found. Return the open file descriptor.
161 Import::try_suffixes(std::string* pfilename)
163 std::string filename = *pfilename + ".gox";
164 int fd = open(filename.c_str(), O_RDONLY | O_BINARY);
165 if (fd >= 0)
167 *pfilename = filename;
168 return fd;
171 const char* basename = lbasename(pfilename->c_str());
172 size_t basename_pos = basename - pfilename->c_str();
173 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".so";
174 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
175 if (fd >= 0)
177 *pfilename = filename;
178 return fd;
181 filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".a";
182 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
183 if (fd >= 0)
185 *pfilename = filename;
186 return fd;
189 filename = *pfilename + ".o";
190 fd = open(filename.c_str(), O_RDONLY | O_BINARY);
191 if (fd >= 0)
193 *pfilename = filename;
194 return fd;
197 return -1;
200 // Look for export data in the file descriptor FD.
202 Import::Stream*
203 Import::find_export_data(const std::string& filename, int fd,
204 Location location)
206 // See if we can read this as an object file.
207 Import::Stream* stream = Import::find_object_export_data(filename, fd, 0,
208 location);
209 if (stream != NULL)
210 return stream;
212 const int len = MAX(Export::v1_magic_len, Import::archive_magic_len);
214 if (lseek(fd, 0, SEEK_SET) < 0)
216 error_at(location, "lseek %s failed: %m", filename.c_str());
217 return NULL;
220 char buf[len];
221 ssize_t c = read(fd, buf, len);
222 if (c < len)
223 return NULL;
225 // Check for a file containing nothing but Go export data.
226 if (memcmp(buf, Export::v1_magic, Export::v1_magic_len) == 0)
227 return new Stream_from_file(fd);
229 // See if we can read this as an archive.
230 if (Import::is_archive_magic(buf))
231 return Import::find_archive_export_data(filename, fd, location);
233 return NULL;
236 // Look for export data in a simple_object.
238 Import::Stream*
239 Import::find_object_export_data(const std::string& filename,
240 int fd,
241 off_t offset,
242 Location location)
244 char *buf;
245 size_t len;
246 int err;
247 const char *errmsg = go_read_export_data(fd, offset, &buf, &len, &err);
248 if (errmsg != NULL)
250 if (err == 0)
251 error_at(location, "%s: %s", filename.c_str(), errmsg);
252 else
253 error_at(location, "%s: %s: %s", filename.c_str(), errmsg,
254 xstrerror(err));
255 return NULL;
258 if (buf == NULL)
259 return NULL;
261 return new Stream_from_buffer(buf, len);
264 // Class Import.
266 // Construct an Import object. We make the builtin_types_ vector
267 // large enough to hold all the builtin types.
269 Import::Import(Stream* stream, Location location)
270 : gogo_(NULL), stream_(stream), location_(location), package_(NULL),
271 add_to_globals_(false),
272 builtin_types_((- SMALLEST_BUILTIN_CODE) + 1),
273 types_()
277 // Import the data in the associated stream.
279 Package*
280 Import::import(Gogo* gogo, const std::string& local_name,
281 bool is_local_name_exported)
283 // Hold on to the Gogo structure. Otherwise we need to pass it
284 // through all the import functions, because we need it when reading
285 // a type.
286 this->gogo_ = gogo;
288 // A stream of export data can include data from more than one input
289 // file. Here we loop over each input file.
290 Stream* stream = this->stream_;
291 while (!stream->at_eof() && !stream->saw_error())
293 // The vector of types is package specific.
294 this->types_.clear();
296 stream->require_bytes(this->location_, Export::v1_magic,
297 Export::v1_magic_len);
299 this->require_c_string("package ");
300 std::string package_name = this->read_identifier();
301 this->require_c_string(";\n");
303 std::string pkgpath;
304 if (this->match_c_string("prefix "))
306 this->advance(7);
307 std::string unique_prefix = this->read_identifier();
308 this->require_c_string(";\n");
309 pkgpath = unique_prefix + '.' + package_name;
311 else
313 this->require_c_string("pkgpath ");
314 pkgpath = this->read_identifier();
315 this->require_c_string(";\n");
318 this->package_ = gogo->add_imported_package(package_name, local_name,
319 is_local_name_exported,
320 pkgpath,
321 this->location_,
322 &this->add_to_globals_);
323 if (this->package_ == NULL)
325 stream->set_saw_error();
326 return NULL;
329 this->require_c_string("priority ");
330 std::string priority_string = this->read_identifier();
331 int prio;
332 if (!this->string_to_int(priority_string, false, &prio))
333 return NULL;
334 this->package_->set_priority(prio);
335 this->require_c_string(";\n");
337 while (stream->match_c_string("import"))
338 this->read_one_import();
340 if (stream->match_c_string("init"))
341 this->read_import_init_fns(gogo);
343 // Loop over all the input data for this package.
344 while (!stream->saw_error())
346 if (stream->match_c_string("const "))
347 this->import_const();
348 else if (stream->match_c_string("type "))
349 this->import_type();
350 else if (stream->match_c_string("var "))
351 this->import_var();
352 else if (stream->match_c_string("func "))
353 this->import_func(this->package_);
354 else if (stream->match_c_string("checksum "))
355 break;
356 else
358 error_at(this->location_,
359 ("error in import data at %d: "
360 "expected %<const%>, %<type%>, %<var%>, "
361 "%<func%>, or %<checksum%>"),
362 stream->pos());
363 stream->set_saw_error();
364 return NULL;
368 // We currently ignore the checksum. In the future we could
369 // store the checksum somewhere in the generated object and then
370 // verify that the checksum matches at link time or at dynamic
371 // load time.
372 this->require_c_string("checksum ");
373 stream->advance(Export::v1_checksum_len * 2);
374 this->require_c_string(";\n");
377 return this->package_;
380 // Read an import line. We don't actually care about these.
382 void
383 Import::read_one_import()
385 this->require_c_string("import ");
386 std::string package_name = this->read_identifier();
387 this->require_c_string(" ");
388 std::string pkgpath = this->read_identifier();
389 this->require_c_string(" \"");
390 Stream* stream = this->stream_;
391 while (stream->peek_char() != '"')
392 stream->advance(1);
393 this->require_c_string("\";\n");
395 Package* p = this->gogo_->register_package(pkgpath,
396 Linemap::unknown_location());
397 p->set_package_name(package_name, this->location());
400 // Read the list of import control functions.
402 void
403 Import::read_import_init_fns(Gogo* gogo)
405 this->require_c_string("init");
406 while (!this->match_c_string(";"))
408 this->require_c_string(" ");
409 std::string package_name = this->read_identifier();
410 this->require_c_string(" ");
411 std::string init_name = this->read_identifier();
412 this->require_c_string(" ");
413 std::string prio_string = this->read_identifier();
414 int prio;
415 if (!this->string_to_int(prio_string, false, &prio))
416 return;
417 gogo->add_import_init_fn(package_name, init_name, prio);
419 this->require_c_string(";\n");
422 // Import a constant.
424 void
425 Import::import_const()
427 std::string name;
428 Type* type;
429 Expression* expr;
430 Named_constant::import_const(this, &name, &type, &expr);
431 Typed_identifier tid(name, type, this->location_);
432 Named_object* no = this->package_->add_constant(tid, expr);
433 if (this->add_to_globals_)
434 this->gogo_->add_named_object(no);
437 // Import a type.
439 void
440 Import::import_type()
442 Named_type* type;
443 Named_type::import_named_type(this, &type);
445 // The named type has been added to the package by the type import
446 // process. Here we need to make it visible to the parser, and it
447 // to the global bindings if necessary.
448 type->set_is_visible();
450 if (this->add_to_globals_)
451 this->gogo_->add_named_type(type);
454 // Import a variable.
456 void
457 Import::import_var()
459 std::string name;
460 Type* type;
461 Variable::import_var(this, &name, &type);
462 Variable* var = new Variable(type, NULL, true, false, false,
463 this->location_);
464 Named_object* no;
465 no = this->package_->add_variable(name, var);
466 if (this->add_to_globals_)
467 this->gogo_->add_named_object(no);
470 // Import a function into PACKAGE. PACKAGE is normally
471 // THIS->PACKAGE_, but it will be different for a method associated
472 // with a type defined in a different package.
474 Named_object*
475 Import::import_func(Package* package)
477 std::string name;
478 Typed_identifier* receiver;
479 Typed_identifier_list* parameters;
480 Typed_identifier_list* results;
481 bool is_varargs;
482 Function::import_func(this, &name, &receiver, &parameters, &results,
483 &is_varargs);
484 Function_type *fntype = Type::make_function_type(receiver, parameters,
485 results, this->location_);
486 if (is_varargs)
487 fntype->set_is_varargs();
489 Location loc = this->location_;
490 Named_object* no;
491 if (fntype->is_method())
493 Type* rtype = receiver->type();
495 // We may still be reading the definition of RTYPE, so we have
496 // to be careful to avoid calling base or convert. If RTYPE is
497 // a named type or a forward declaration, then we know that it
498 // is not a pointer, because we are reading a method on RTYPE
499 // and named pointers can't have methods.
501 if (rtype->classification() == Type::TYPE_POINTER)
502 rtype = rtype->points_to();
504 if (rtype->is_error_type())
505 return NULL;
506 else if (rtype->named_type() != NULL)
507 no = rtype->named_type()->add_method_declaration(name, package, fntype,
508 loc);
509 else if (rtype->forward_declaration_type() != NULL)
510 no = rtype->forward_declaration_type()->add_method_declaration(name,
511 package,
512 fntype,
513 loc);
514 else
515 go_unreachable();
517 else
519 no = package->add_function_declaration(name, fntype, loc);
520 if (this->add_to_globals_)
521 this->gogo_->add_named_object(no);
523 return no;
526 // Read a type in the import stream. This records the type by the
527 // type index. If the type is named, it registers the name, but marks
528 // it as invisible.
530 Type*
531 Import::read_type()
533 Stream* stream = this->stream_;
534 this->require_c_string("<type ");
536 std::string number;
537 int c;
538 while (true)
540 c = stream->get_char();
541 if (c != '-' && (c < '0' || c > '9'))
542 break;
543 number += c;
546 int index;
547 if (!this->string_to_int(number, true, &index))
548 return Type::make_error_type();
550 if (c == '>')
552 // This type was already defined.
553 if (index < 0
554 ? (static_cast<size_t>(- index) >= this->builtin_types_.size()
555 || this->builtin_types_[- index] == NULL)
556 : (static_cast<size_t>(index) >= this->types_.size()
557 || this->types_[index] == NULL))
559 error_at(this->location_,
560 "error in import data at %d: bad type index %d",
561 stream->pos(), index);
562 stream->set_saw_error();
563 return Type::make_error_type();
566 return index < 0 ? this->builtin_types_[- index] : this->types_[index];
569 if (c != ' ')
571 if (!stream->saw_error())
572 error_at(this->location_,
573 "error in import data at %d: expect %< %> or %<>%>'",
574 stream->pos());
575 stream->set_saw_error();
576 stream->advance(1);
577 return Type::make_error_type();
580 if (index <= 0
581 || (static_cast<size_t>(index) < this->types_.size()
582 && this->types_[index] != NULL))
584 error_at(this->location_,
585 "error in import data at %d: type index already defined",
586 stream->pos());
587 stream->set_saw_error();
588 return Type::make_error_type();
591 if (static_cast<size_t>(index) >= this->types_.size())
593 int newsize = std::max(static_cast<size_t>(index) + 1,
594 this->types_.size() * 2);
595 this->types_.resize(newsize, NULL);
598 if (stream->peek_char() != '"')
600 Type* type = Type::import_type(this);
601 this->require_c_string(">");
602 this->types_[index] = type;
603 return type;
606 // This type has a name.
608 stream->advance(1);
609 std::string type_name;
610 while ((c = stream->get_char()) != '"')
611 type_name += c;
613 // If this type is in the package we are currently importing, the
614 // name will be .PKGPATH.NAME or simply NAME with no dots.
615 // Otherwise, a non-hidden symbol will be PKGPATH.NAME and a hidden
616 // symbol will be .PKGPATH.NAME.
617 std::string pkgpath;
618 if (type_name.find('.') != std::string::npos)
620 size_t start = 0;
621 if (type_name[0] == '.')
622 start = 1;
623 size_t dot = type_name.rfind('.');
624 pkgpath = type_name.substr(start, dot - start);
625 if (type_name[0] != '.')
626 type_name.erase(0, dot + 1);
629 this->require_c_string(" ");
631 // The package name may follow. This is the name of the package in
632 // the package clause of that package. The type name will include
633 // the pkgpath, which may be different.
634 std::string package_name;
635 if (stream->peek_char() == '"')
637 stream->advance(1);
638 while ((c = stream->get_char()) != '"')
639 package_name += c;
640 this->require_c_string(" ");
643 // Declare the type in the appropriate package. If we haven't seen
644 // it before, mark it as invisible. We declare it before we read
645 // the actual definition of the type, since the definition may refer
646 // to the type itself.
647 Package* package;
648 if (pkgpath.empty() || pkgpath == this->gogo_->pkgpath())
649 package = this->package_;
650 else
652 package = this->gogo_->register_package(pkgpath,
653 Linemap::unknown_location());
654 if (!package_name.empty())
655 package->set_package_name(package_name, this->location());
658 Named_object* no = package->bindings()->lookup(type_name);
659 if (no == NULL)
660 no = package->add_type_declaration(type_name, this->location_);
661 else if (!no->is_type_declaration() && !no->is_type())
663 error_at(this->location_, "imported %<%s.%s%> both type and non-type",
664 pkgpath.c_str(), Gogo::message_name(type_name).c_str());
665 stream->set_saw_error();
666 return Type::make_error_type();
668 else
669 go_assert(no->package() == package);
671 if (this->types_[index] == NULL)
673 if (no->is_type_declaration())
675 // FIXME: It's silly to make a forward declaration every time.
676 this->types_[index] = Type::make_forward_declaration(no);
678 else
680 go_assert(no->is_type());
681 this->types_[index] = no->type_value();
685 // If there is no type definition, then this is just a forward
686 // declaration of a type defined in some other file.
687 Type* type;
688 if (this->match_c_string(">"))
689 type = this->types_[index];
690 else
692 type = this->read_type();
694 if (no->is_type_declaration())
696 // We can define the type now.
698 no = package->add_type(type_name, type, this->location_);
699 Named_type* ntype = no->type_value();
701 // This type has not yet been imported.
702 ntype->clear_is_visible();
704 if (!type->is_undefined() && type->interface_type() != NULL)
705 this->gogo_->record_interface_type(type->interface_type());
707 type = ntype;
709 else if (no->is_type())
711 // We have seen this type before. FIXME: it would be a good
712 // idea to check that the two imported types are identical,
713 // but we have not finalized the methods yet, which means
714 // that we can not reliably compare interface types.
715 type = no->type_value();
717 // Don't change the visibility of the existing type.
720 this->types_[index] = type;
722 // Read the type methods.
723 if (this->match_c_string("\n"))
725 this->advance(1);
726 while (this->match_c_string(" func"))
728 this->advance(1);
729 this->import_func(package);
734 this->require_c_string(">");
736 return type;
739 // Register the builtin types.
741 void
742 Import::register_builtin_types(Gogo* gogo)
744 this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
745 this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
746 this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
747 this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
748 this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
749 this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
750 this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
751 this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
752 this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
753 this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
754 this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
755 this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
756 this->register_builtin_type(gogo, "int", BUILTIN_INT);
757 this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
758 this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
759 this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
760 this->register_builtin_type(gogo, "string", BUILTIN_STRING);
761 this->register_builtin_type(gogo, "error", BUILTIN_ERROR);
762 this->register_builtin_type(gogo, "byte", BUILTIN_BYTE);
763 this->register_builtin_type(gogo, "rune", BUILTIN_RUNE);
766 // Register a single builtin type.
768 void
769 Import::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
771 Named_object* named_object = gogo->lookup_global(name);
772 go_assert(named_object != NULL && named_object->is_type());
773 int index = - static_cast<int>(code);
774 go_assert(index > 0
775 && static_cast<size_t>(index) < this->builtin_types_.size());
776 this->builtin_types_[index] = named_object->type_value();
779 // Read an identifier from the stream.
781 std::string
782 Import::read_identifier()
784 std::string ret;
785 Stream* stream = this->stream_;
786 int c;
787 while (true)
789 c = stream->peek_char();
790 if (c == -1 || c == ' ' || c == ';')
791 break;
792 ret += c;
793 stream->advance(1);
795 return ret;
798 // Read a name from the stream.
800 std::string
801 Import::read_name()
803 std::string ret = this->read_identifier();
804 if (ret == "?")
805 ret.clear();
806 else if (!Lex::is_exported_name(ret))
807 ret = '.' + this->package_->pkgpath() + '.' + ret;
808 return ret;
811 // Turn a string into a integer with appropriate error handling.
813 bool
814 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret)
816 char* end;
817 long prio = strtol(s.c_str(), &end, 10);
818 if (*end != '\0' || prio > 0x7fffffff || (prio < 0 && !is_neg_ok))
820 error_at(this->location_, "invalid integer in import data at %d",
821 this->stream_->pos());
822 this->stream_->set_saw_error();
823 return false;
825 *ret = prio;
826 return true;
829 // Class Import::Stream.
831 Import::Stream::Stream()
832 : pos_(0), saw_error_(false)
836 Import::Stream::~Stream()
840 // Return the next character to come from the stream.
843 Import::Stream::peek_char()
845 const char* read;
846 if (!this->do_peek(1, &read))
847 return -1;
848 // Make sure we return an unsigned char, so that we don't get
849 // confused by \xff.
850 unsigned char ret = *read;
851 return ret;
854 // Return true if the next LENGTH characters from the stream match
855 // BYTES
857 bool
858 Import::Stream::match_bytes(const char* bytes, size_t length)
860 const char* read;
861 if (!this->do_peek(length, &read))
862 return false;
863 return memcmp(bytes, read, length) == 0;
866 // Require that the next LENGTH bytes from the stream match BYTES.
868 void
869 Import::Stream::require_bytes(Location location, const char* bytes,
870 size_t length)
872 const char* read;
873 if (!this->do_peek(length, &read)
874 || memcmp(bytes, read, length) != 0)
876 if (!this->saw_error_)
877 error_at(location, "import error at %d: expected %<%.*s%>",
878 this->pos(), static_cast<int>(length), bytes);
879 this->saw_error_ = true;
880 return;
882 this->advance(length);
885 // Class Stream_from_file.
887 Stream_from_file::Stream_from_file(int fd)
888 : fd_(fd), data_()
890 if (lseek(fd, 0, SEEK_SET) != 0)
892 error("lseek failed: %m");
893 this->set_saw_error();
897 Stream_from_file::~Stream_from_file()
899 close(this->fd_);
902 // Read next bytes.
904 bool
905 Stream_from_file::do_peek(size_t length, const char** bytes)
907 if (this->data_.length() <= length)
909 *bytes = this->data_.data();
910 return true;
912 // Don't bother to handle the general case, since we don't need it.
913 go_assert(length < 64);
914 char buf[64];
915 ssize_t got = read(this->fd_, buf, length);
917 if (got < 0)
919 if (!this->saw_error())
920 error("read failed: %m");
921 this->set_saw_error();
922 return false;
925 if (lseek(this->fd_, - got, SEEK_CUR) != 0)
927 if (!this->saw_error())
928 error("lseek failed: %m");
929 this->set_saw_error();
930 return false;
933 if (static_cast<size_t>(got) < length)
934 return false;
936 this->data_.assign(buf, got);
938 *bytes = this->data_.data();
939 return true;
942 // Advance.
944 void
945 Stream_from_file::do_advance(size_t skip)
947 if (lseek(this->fd_, skip, SEEK_CUR) != 0)
949 if (!this->saw_error())
950 error("lseek failed: %m");
951 this->set_saw_error();
953 if (!this->data_.empty())
955 if (this->data_.length() < skip)
956 this->data_.erase(0, skip);
957 else
958 this->data_.clear();