OSDN Git Service

de7edc91e2e233f31afde63a556974877bedfa3c
[pf3gnuchains/gcc-fork.git] / gcc / go / gofrontend / import.cc
1 // import.cc -- Go frontend import declarations.
2
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.
6
7 #include "go-system.h"
8
9 #include "filenames.h"
10 #include "simple-object.h"
11
12 #include "go-c.h"
13 #include "gogo.h"
14 #include "types.h"
15 #include "export.h"
16 #include "import.h"
17
18 #ifndef O_BINARY
19 #define O_BINARY 0
20 #endif
21
22 // The list of paths we search for import files.
23
24 static std::vector<std::string> search_path;
25
26 // Add a directory to the search path.  This is called from the option
27 // handling language hook.
28
29 GO_EXTERN_C
30 void
31 go_add_search_path(const char* path)
32 {
33   search_path.push_back(std::string(path));
34 }
35
36 // The name used for parameters, receivers, and results in imported
37 // function types.
38
39 const char* const Import::import_marker = "*imported*";
40
41 // Find import data.  This searches the file system for FILENAME and
42 // returns a pointer to a Stream object to read the data that it
43 // exports.  If the file is not found, it returns NULL.
44
45 // When FILENAME is not an absolute path and does not start with ./ or
46 // ../, we use the search path provided by -I and -L options.
47
48 // When FILENAME does not exist, we try modifying FILENAME to find the
49 // file.  We use the first of these which exists:
50 //   * We append ".gox".
51 //   * We turn the base of FILENAME into libFILENAME.so.
52 //   * We turn the base of FILENAME into libFILENAME.a.
53 //   * We append ".o".
54
55 // When using a search path, we apply each of these transformations at
56 // each entry on the search path before moving on to the next entry.
57 // If the file exists, but does not contain any Go export data, we
58 // stop; we do not keep looking for another file with the same name
59 // later in the search path.
60
61 Import::Stream*
62 Import::open_package(const std::string& filename, Location location)
63 {
64   bool is_local;
65   if (IS_ABSOLUTE_PATH(filename))
66     is_local = true;
67   else if (filename[0] == '.' && IS_DIR_SEPARATOR(filename[1]))
68     is_local = true;
69   else if (filename[0] == '.'
70            && filename[1] == '.'
71            && IS_DIR_SEPARATOR(filename[2]))
72     is_local = true;
73   else
74     is_local = false;
75   if (!is_local)
76     {
77       for (std::vector<std::string>::const_iterator p = search_path.begin();
78            p != search_path.end();
79            ++p)
80         {
81           std::string indir = *p;
82           if (!indir.empty() && indir[indir.size() - 1] != '/')
83             indir += '/';
84           indir += filename;
85           Stream* s = Import::try_package_in_directory(indir, location);
86           if (s != NULL)
87             return s;
88         }
89     }
90
91   Stream* s = Import::try_package_in_directory(filename, location);
92   if (s != NULL)
93     return s;
94
95   return NULL;
96 }
97
98 // Try to find the export data for FILENAME.
99
100 Import::Stream*
101 Import::try_package_in_directory(const std::string& filename,
102                                  Location location)
103 {
104   std::string found_filename = filename;
105   int fd = open(found_filename.c_str(), O_RDONLY | O_BINARY);
106
107   if (fd >= 0)
108     {
109       struct stat s;
110       if (fstat(fd, &s) >= 0 && S_ISDIR(s.st_mode))
111         {
112           close(fd);
113           fd = -1;
114           errno = EISDIR;
115         }
116     }
117
118   if (fd < 0)
119     {
120       if (errno != ENOENT && errno != EISDIR)
121         warning_at(location, 0, "%s: %m", filename.c_str());
122
123       fd = Import::try_suffixes(&found_filename);
124       if (fd < 0)
125         return NULL;
126     }
127
128   // The export data may not be in this file.
129   Stream* s = Import::find_export_data(found_filename, fd, location);
130   if (s != NULL)
131     return s;
132
133   close(fd);
134
135   error_at(location, "%s exists but does not contain any Go export data",
136            found_filename.c_str());
137
138   return NULL;
139 }
140
141 // Given import "*PFILENAME", where *PFILENAME does not exist, try
142 // various suffixes.  If we find one, set *PFILENAME to the one we
143 // found.  Return the open file descriptor.
144
145 int
146 Import::try_suffixes(std::string* pfilename)
147 {
148   std::string filename = *pfilename + ".gox";
149   int fd = open(filename.c_str(), O_RDONLY | O_BINARY);
150   if (fd >= 0)
151     {
152       *pfilename = filename;
153       return fd;
154     }
155
156   const char* basename = lbasename(pfilename->c_str());
157   size_t basename_pos = basename - pfilename->c_str();
158   filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".so";
159   fd = open(filename.c_str(), O_RDONLY | O_BINARY);
160   if (fd >= 0)
161     {
162       *pfilename = filename;
163       return fd;
164     }
165
166   filename = pfilename->substr(0, basename_pos) + "lib" + basename + ".a";
167   fd = open(filename.c_str(), O_RDONLY | O_BINARY);
168   if (fd >= 0)
169     {
170       *pfilename = filename;
171       return fd;
172     }
173
174   filename = *pfilename + ".o";
175   fd = open(filename.c_str(), O_RDONLY | O_BINARY);
176   if (fd >= 0)
177     {
178       *pfilename = filename;
179       return fd;
180     }
181
182   return -1;
183 }
184
185 // Look for export data in the file descriptor FD.
186
187 Import::Stream*
188 Import::find_export_data(const std::string& filename, int fd,
189                          Location location)
190 {
191   // See if we can read this as an object file.
192   Import::Stream* stream = Import::find_object_export_data(filename, fd, 0,
193                                                            location);
194   if (stream != NULL)
195     return stream;
196
197   const int len = MAX(Export::v1_magic_len, Import::archive_magic_len);
198
199   if (lseek(fd, 0, SEEK_SET) < 0)
200     {
201       error_at(location, "lseek %s failed: %m", filename.c_str());
202       return NULL;
203     }
204
205   char buf[len];
206   ssize_t c = read(fd, buf, len);
207   if (c < len)
208     return NULL;
209
210   // Check for a file containing nothing but Go export data.
211   if (memcmp(buf, Export::v1_magic, Export::v1_magic_len) == 0)
212     return new Stream_from_file(fd);
213
214   // See if we can read this as an archive.
215   if (Import::is_archive_magic(buf))
216     return Import::find_archive_export_data(filename, fd, location);
217
218   return NULL;
219 }
220
221 // Look for export data in a simple_object.
222
223 Import::Stream*
224 Import::find_object_export_data(const std::string& filename,
225                                 int fd,
226                                 off_t offset,
227                                 Location location)
228 {
229   char *buf;
230   size_t len;
231   int err;
232   const char *errmsg = go_read_export_data(fd, offset, &buf, &len, &err);
233   if (errmsg != NULL)
234     {
235       if (err == 0)
236         error_at(location, "%s: %s", filename.c_str(), errmsg);
237       else
238         error_at(location, "%s: %s: %s", filename.c_str(), errmsg,
239                  xstrerror(err));
240       return NULL;
241     }
242
243   if (buf == NULL)
244     return NULL;
245
246   return new Stream_from_buffer(buf, len);
247 }
248
249 // Class Import.
250
251 // Construct an Import object.  We make the builtin_types_ vector
252 // large enough to hold all the builtin types.
253
254 Import::Import(Stream* stream, Location location)
255   : gogo_(NULL), stream_(stream), location_(location), package_(NULL),
256     add_to_globals_(false),
257     builtin_types_((- SMALLEST_BUILTIN_CODE) + 1),
258     types_()
259 {
260 }
261
262 // Import the data in the associated stream.
263
264 Package*
265 Import::import(Gogo* gogo, const std::string& local_name,
266                bool is_local_name_exported)
267 {
268   // Hold on to the Gogo structure.  Otherwise we need to pass it
269   // through all the import functions, because we need it when reading
270   // a type.
271   this->gogo_ = gogo;
272
273   // A stream of export data can include data from more than one input
274   // file.  Here we loop over each input file.
275   Stream* stream = this->stream_;
276   while (!stream->at_eof() && !stream->saw_error())
277     {
278       // The vector of types is package specific.
279       this->types_.clear();
280
281       stream->require_bytes(this->location_, Export::v1_magic,
282                             Export::v1_magic_len);
283
284       this->require_c_string("package ");
285       std::string package_name = this->read_identifier();
286       this->require_c_string(";\n");
287
288       this->require_c_string("prefix ");
289       std::string unique_prefix = this->read_identifier();
290       this->require_c_string(";\n");
291
292       this->package_ = gogo->add_imported_package(package_name, local_name,
293                                                   is_local_name_exported,
294                                                   unique_prefix,
295                                                   this->location_,
296                                                   &this->add_to_globals_);
297       if (this->package_ == NULL)
298         {
299           stream->set_saw_error();
300           return NULL;
301         }
302
303       this->require_c_string("priority ");
304       std::string priority_string = this->read_identifier();
305       int prio;
306       if (!this->string_to_int(priority_string, false, &prio))
307         return NULL;
308       this->package_->set_priority(prio);
309       this->require_c_string(";\n");
310
311       if (stream->match_c_string("import "))
312         this->read_import_init_fns(gogo);
313
314       // Loop over all the input data for this package.
315       while (!stream->saw_error())
316         {
317           if (stream->match_c_string("const "))
318             this->import_const();
319           else if (stream->match_c_string("type "))
320             this->import_type();
321           else if (stream->match_c_string("var "))
322             this->import_var();
323           else if (stream->match_c_string("func "))
324             this->import_func(this->package_);
325           else if (stream->match_c_string("checksum "))
326             break;
327           else
328             {
329               error_at(this->location_,
330                        ("error in import data at %d: "
331                         "expected %<const%>, %<type%>, %<var%>, "
332                         "%<func%>, or %<checksum%>"),
333                        stream->pos());
334               stream->set_saw_error();
335               return NULL;
336             }
337         }
338
339       // We currently ignore the checksum.  In the future we could
340       // store the checksum somewhere in the generated object and then
341       // verify that the checksum matches at link time or at dynamic
342       // load time.
343       this->require_c_string("checksum ");
344       stream->advance(Export::v1_checksum_len * 2);
345       this->require_c_string(";\n");
346     }
347
348   return this->package_;
349 }
350
351 // Read the list of import control functions.
352
353 void
354 Import::read_import_init_fns(Gogo* gogo)
355 {
356   this->require_c_string("import");
357   while (!this->match_c_string(";"))
358     {
359       this->require_c_string(" ");
360       std::string package_name = this->read_identifier();
361       this->require_c_string(" ");
362       std::string init_name = this->read_identifier();
363       this->require_c_string(" ");
364       std::string prio_string = this->read_identifier();
365       int prio;
366       if (!this->string_to_int(prio_string, false, &prio))
367         return;
368       gogo->add_import_init_fn(package_name, init_name, prio);
369     }
370   this->require_c_string(";\n");
371 }
372
373 // Import a constant.
374
375 void
376 Import::import_const()
377 {
378   std::string name;
379   Type* type;
380   Expression* expr;
381   Named_constant::import_const(this, &name, &type, &expr);
382   Typed_identifier tid(name, type, this->location_);
383   Named_object* no = this->package_->add_constant(tid, expr);
384   if (this->add_to_globals_)
385     this->gogo_->add_named_object(no);
386 }
387
388 // Import a type.
389
390 void
391 Import::import_type()
392 {
393   Named_type* type;
394   Named_type::import_named_type(this, &type);
395
396   // The named type has been added to the package by the type import
397   // process.  Here we need to make it visible to the parser, and it
398   // to the global bindings if necessary.
399   type->set_is_visible();
400
401   if (this->add_to_globals_)
402     this->gogo_->add_named_type(type);
403 }
404
405 // Import a variable.
406
407 void
408 Import::import_var()
409 {
410   std::string name;
411   Type* type;
412   Variable::import_var(this, &name, &type);
413   Variable* var = new Variable(type, NULL, true, false, false,
414                                this->location_);
415   Named_object* no;
416   no = this->package_->add_variable(name, var);
417   if (this->add_to_globals_)
418     this->gogo_->add_named_object(no);
419 }
420
421 // Import a function into PACKAGE.  PACKAGE is normally
422 // THIS->PACKAGE_, but it will be different for a method associated
423 // with a type defined in a different package.
424
425 Named_object*
426 Import::import_func(Package* package)
427 {
428   std::string name;
429   Typed_identifier* receiver;
430   Typed_identifier_list* parameters;
431   Typed_identifier_list* results;
432   bool is_varargs;
433   Function::import_func(this, &name, &receiver, &parameters, &results,
434                         &is_varargs);
435   Function_type *fntype = Type::make_function_type(receiver, parameters,
436                                                    results, this->location_);
437   if (is_varargs)
438     fntype->set_is_varargs();
439
440   Location loc = this->location_;
441   Named_object* no;
442   if (fntype->is_method())
443     {
444       Type* rtype = receiver->type()->deref();
445       if (rtype->is_error_type())
446         return NULL;
447       Named_type* named_rtype = rtype->named_type();
448       go_assert(named_rtype != NULL);
449       no = named_rtype->add_method_declaration(name, package, fntype, loc);
450     }
451   else
452     {
453       no = package->add_function_declaration(name, fntype, loc);
454       if (this->add_to_globals_)
455         this->gogo_->add_named_object(no);
456     }
457   return no;
458 }
459
460 // Read a type in the import stream.  This records the type by the
461 // type index.  If the type is named, it registers the name, but marks
462 // it as invisible.
463
464 Type*
465 Import::read_type()
466 {
467   Stream* stream = this->stream_;
468   this->require_c_string("<type ");
469
470   std::string number;
471   int c;
472   while (true)
473     {
474       c = stream->get_char();
475       if (c != '-' && (c < '0' || c > '9'))
476         break;
477       number += c;
478     }
479
480   int index;
481   if (!this->string_to_int(number, true, &index))
482     return Type::make_error_type();
483
484   if (c == '>')
485     {
486       // This type was already defined.
487       if (index < 0
488           ? (static_cast<size_t>(- index) >= this->builtin_types_.size()
489              || this->builtin_types_[- index] == NULL)
490           : (static_cast<size_t>(index) >= this->types_.size()
491              || this->types_[index] == NULL))
492         {
493           error_at(this->location_,
494                    "error in import data at %d: bad type index %d",
495                    stream->pos(), index);
496           stream->set_saw_error();
497           return Type::make_error_type();
498         }
499
500       return index < 0 ? this->builtin_types_[- index] : this->types_[index];
501     }
502
503   if (c != ' ')
504     {
505       if (!stream->saw_error())
506         error_at(this->location_,
507                  "error in import data at %d: expect %< %> or %<>%>'",
508                  stream->pos());
509       stream->set_saw_error();
510       stream->advance(1);
511       return Type::make_error_type();
512     }
513
514   if (index <= 0
515       || (static_cast<size_t>(index) < this->types_.size()
516           && this->types_[index] != NULL))
517     {
518       error_at(this->location_,
519                "error in import data at %d: type index already defined",
520                stream->pos());
521       stream->set_saw_error();
522       return Type::make_error_type();
523     }
524
525   if (static_cast<size_t>(index) >= this->types_.size())
526     {
527       int newsize = std::max(static_cast<size_t>(index) + 1,
528                              this->types_.size() * 2);
529       this->types_.resize(newsize, NULL);
530     }
531
532   if (stream->peek_char() != '"')
533     {
534       Type* type = Type::import_type(this);
535       this->require_c_string(">");
536       this->types_[index] = type;
537       return type;
538     }
539
540   // This type has a name.
541
542   stream->advance(1);
543   std::string type_name;
544   while ((c = stream->get_char()) != '"')
545     type_name += c;
546
547   // If this type is in the current package, the name will be
548   // .PREFIX.PACKAGE.NAME or simply NAME with no dots.  Otherwise, a
549   // non-hidden symbol will be PREFIX.PACKAGE.NAME and a hidden symbol
550   // will be .PREFIX.PACKAGE.NAME.
551   std::string package_name;
552   std::string unique_prefix;
553   if (type_name.find('.') != std::string::npos)
554     {
555       bool is_hidden = false;
556       size_t start = 0;
557       if (type_name[0] == '.')
558         {
559           ++start;
560           is_hidden = true;
561         }
562       size_t dot1 = type_name.find('.', start);
563       size_t dot2;
564       if (dot1 == std::string::npos)
565         dot2 = std::string::npos;
566       else
567         dot2 = type_name.find('.', dot1 + 1);
568       if (dot1 == std::string::npos || dot2 == std::string::npos)
569         {
570           error_at(this->location_,
571                    ("error at import data at %d: missing dot in type name"),
572                    stream->pos());
573           stream->set_saw_error();
574         }
575       else
576         {
577           unique_prefix = type_name.substr(start, dot1 - start);
578           package_name = type_name.substr(dot1 + 1, dot2 - (dot1 + 1));
579         }
580       if (!is_hidden)
581         type_name.erase(0, dot2 + 1);
582     }
583
584   this->require_c_string(" ");
585
586   // Declare the type in the appropriate package.  If we haven't seen
587   // it before, mark it as invisible.  We declare it before we read
588   // the actual definition of the type, since the definition may refer
589   // to the type itself.
590   Package* package;
591   if (package_name.empty())
592     package = this->package_;
593   else
594     package = this->gogo_->register_package(package_name, unique_prefix,
595                                             Linemap::unknown_location());
596
597   Named_object* no = package->bindings()->lookup(type_name);
598   if (no == NULL)
599     no = package->add_type_declaration(type_name, this->location_);
600   else if (!no->is_type_declaration() && !no->is_type())
601     {
602       error_at(this->location_, "imported %<%s.%s%> both type and non-type",
603                Gogo::message_name(package->name()).c_str(),
604                Gogo::message_name(type_name).c_str());
605       stream->set_saw_error();
606       return Type::make_error_type();
607     }
608   else
609     go_assert(no->package() == package);
610
611   if (this->types_[index] == NULL)
612     {
613       if (no->is_type_declaration())
614         {
615           // FIXME: It's silly to make a forward declaration every time.
616           this->types_[index] = Type::make_forward_declaration(no);
617         }
618       else
619         {
620           go_assert(no->is_type());
621           this->types_[index] = no->type_value();
622         }
623     }
624
625   // If there is no type definition, then this is just a forward
626   // declaration of a type defined in some other file.
627   Type* type;
628   if (this->match_c_string(">"))
629     type = this->types_[index];
630   else
631     {
632       type = this->read_type();
633
634       if (no->is_type_declaration())
635         {
636           // We can define the type now.
637
638           no = package->add_type(type_name, type, this->location_);
639           Named_type* ntype = no->type_value();
640
641           // This type has not yet been imported.
642           ntype->clear_is_visible();
643
644           type = ntype;
645         }
646       else if (no->is_type())
647         {
648           // We have seen this type before.  FIXME: it would be a good
649           // idea to check that the two imported types are identical,
650           // but we have not finalized the methds yet, which means
651           // that we can nt reliably compare interface types.
652           type = no->type_value();
653
654           // Don't change the visibility of the existing type.
655         }
656
657       this->types_[index] = type;
658
659       // Read the type methods.
660       if (this->match_c_string("\n"))
661         {
662           this->advance(1);
663           while (this->match_c_string(" func"))
664             {
665               this->advance(1);
666               this->import_func(package);
667             }
668         }
669     }
670
671   this->require_c_string(">");
672
673   return type;
674 }
675
676 // Register the builtin types.
677
678 void
679 Import::register_builtin_types(Gogo* gogo)
680 {
681   this->register_builtin_type(gogo, "int8", BUILTIN_INT8);
682   this->register_builtin_type(gogo, "int16", BUILTIN_INT16);
683   this->register_builtin_type(gogo, "int32", BUILTIN_INT32);
684   this->register_builtin_type(gogo, "int64", BUILTIN_INT64);
685   this->register_builtin_type(gogo, "uint8", BUILTIN_UINT8);
686   this->register_builtin_type(gogo, "uint16", BUILTIN_UINT16);
687   this->register_builtin_type(gogo, "uint32", BUILTIN_UINT32);
688   this->register_builtin_type(gogo, "uint64", BUILTIN_UINT64);
689   this->register_builtin_type(gogo, "float32", BUILTIN_FLOAT32);
690   this->register_builtin_type(gogo, "float64", BUILTIN_FLOAT64);
691   this->register_builtin_type(gogo, "complex64", BUILTIN_COMPLEX64);
692   this->register_builtin_type(gogo, "complex128", BUILTIN_COMPLEX128);
693   this->register_builtin_type(gogo, "int", BUILTIN_INT);
694   this->register_builtin_type(gogo, "uint", BUILTIN_UINT);
695   this->register_builtin_type(gogo, "uintptr", BUILTIN_UINTPTR);
696   this->register_builtin_type(gogo, "bool", BUILTIN_BOOL);
697   this->register_builtin_type(gogo, "string", BUILTIN_STRING);
698   this->register_builtin_type(gogo, "error", BUILTIN_ERROR);
699   this->register_builtin_type(gogo, "byte", BUILTIN_BYTE);
700   this->register_builtin_type(gogo, "rune", BUILTIN_RUNE);
701 }
702
703 // Register a single builtin type.
704
705 void
706 Import::register_builtin_type(Gogo* gogo, const char* name, Builtin_code code)
707 {
708   Named_object* named_object = gogo->lookup_global(name);
709   go_assert(named_object != NULL && named_object->is_type());
710   int index = - static_cast<int>(code);
711   go_assert(index > 0
712              && static_cast<size_t>(index) < this->builtin_types_.size());
713   this->builtin_types_[index] = named_object->type_value();
714 }
715
716 // Read an identifier from the stream.
717
718 std::string
719 Import::read_identifier()
720 {
721   std::string ret;
722   Stream* stream = this->stream_;
723   int c;
724   while (true)
725     {
726       c = stream->peek_char();
727       if (c == -1 || c == ' ' || c == ';')
728         break;
729       ret += c;
730       stream->advance(1);
731     }
732   return ret;
733 }
734
735 // Turn a string into a integer with appropriate error handling.
736
737 bool
738 Import::string_to_int(const std::string &s, bool is_neg_ok, int* ret)
739 {
740   char* end;
741   long prio = strtol(s.c_str(), &end, 10);
742   if (*end != '\0' || prio > 0x7fffffff || (prio < 0 && !is_neg_ok))
743     {
744       error_at(this->location_, "invalid integer in import data at %d",
745                this->stream_->pos());
746       this->stream_->set_saw_error();
747       return false;
748     }
749   *ret = prio;
750   return true;
751 }
752
753 // Class Import::Stream.
754
755 Import::Stream::Stream()
756   : pos_(0), saw_error_(false)
757 {
758 }
759
760 Import::Stream::~Stream()
761 {
762 }
763
764 // Return the next character to come from the stream.
765
766 int
767 Import::Stream::peek_char()
768 {
769   const char* read;
770   if (!this->do_peek(1, &read))
771     return -1;
772   // Make sure we return an unsigned char, so that we don't get
773   // confused by \xff.
774   unsigned char ret = *read;
775   return ret;
776 }
777
778 // Return true if the next LENGTH characters from the stream match
779 // BYTES
780
781 bool
782 Import::Stream::match_bytes(const char* bytes, size_t length)
783 {
784   const char* read;
785   if (!this->do_peek(length, &read))
786     return false;
787   return memcmp(bytes, read, length) == 0;
788 }
789
790 // Require that the next LENGTH bytes from the stream match BYTES.
791
792 void
793 Import::Stream::require_bytes(Location location, const char* bytes,
794                               size_t length)
795 {
796   const char* read;
797   if (!this->do_peek(length, &read)
798       || memcmp(bytes, read, length) != 0)
799     {
800       if (!this->saw_error_)
801         error_at(location, "import error at %d: expected %<%.*s%>",
802                  this->pos(), static_cast<int>(length), bytes);
803       this->saw_error_ = true;
804       return;
805     }
806   this->advance(length);
807 }
808
809 // Class Stream_from_file.
810
811 Stream_from_file::Stream_from_file(int fd)
812   : fd_(fd), data_()
813 {
814   if (lseek(fd, 0, SEEK_SET) != 0)
815     {
816       error("lseek failed: %m");
817       this->set_saw_error();
818     }
819 }
820
821 Stream_from_file::~Stream_from_file()
822 {
823   close(this->fd_);
824 }
825
826 // Read next bytes.
827
828 bool
829 Stream_from_file::do_peek(size_t length, const char** bytes)
830 {
831   if (this->data_.length() <= length)
832     {
833       *bytes = this->data_.data();
834       return true;
835     }
836   // Don't bother to handle the general case, since we don't need it.
837   go_assert(length < 64);
838   char buf[64];
839   ssize_t got = read(this->fd_, buf, length);
840
841   if (got < 0)
842     {
843       if (!this->saw_error())
844         error("read failed: %m");
845       this->set_saw_error();
846       return false;
847     }
848
849   if (lseek(this->fd_, - got, SEEK_CUR) != 0)
850     {
851       if (!this->saw_error())
852         error("lseek failed: %m");
853       this->set_saw_error();
854       return false;
855     }
856
857   if (static_cast<size_t>(got) < length)
858     return false;
859
860   this->data_.assign(buf, got);
861
862   *bytes = this->data_.data();
863   return true;
864 }
865
866 // Advance.
867
868 void
869 Stream_from_file::do_advance(size_t skip)
870 {
871   if (lseek(this->fd_, skip, SEEK_CUR) != 0)
872     {
873       if (!this->saw_error())
874         error("lseek failed: %m");
875       this->set_saw_error();
876     }
877   if (!this->data_.empty())
878     {
879       if (this->data_.length() < skip)
880         this->data_.erase(0, skip);
881       else
882         this->data_.clear();
883     }
884 }