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