1 // fileread.cc -- read files for gold
11 #include "dirsearch.h"
17 // Class File_read::View.
19 File_read::View::~View()
21 gold_assert(!this->is_locked());
26 File_read::View::lock()
32 File_read::View::unlock()
34 gold_assert(this->lock_count_ > 0);
39 File_read::View::is_locked()
41 return this->lock_count_ > 0;
46 // The File_read class is designed to support file descriptor caching,
47 // but this is not currently implemented.
49 File_read::~File_read()
51 gold_assert(this->lock_count_ == 0);
52 if (this->descriptor_ >= 0)
54 if (close(this->descriptor_) < 0)
55 fprintf(stderr, _("%s: warning: close(%s) failed: %s"),
56 program_name, this->name_.c_str(), strerror(errno));
57 this->descriptor_ = -1;
60 this->clear_views(true);
64 File_read::open(const std::string& name)
66 gold_assert(this->lock_count_ == 0
67 && this->descriptor_ < 0
68 && this->name_.empty());
70 this->descriptor_ = ::open(this->name_.c_str(), O_RDONLY);
72 return this->descriptor_ >= 0;
76 File_read::get_descriptor()
78 gold_assert(this->lock_count_ > 0);
79 return this->descriptor_;
91 gold_assert(this->lock_count_ > 0);
96 File_read::is_locked()
98 return this->lock_count_ > 0;
101 // See if we have a view which covers the file starting at START for
102 // SIZE bytes. Return a pointer to the View if found, NULL if not.
104 inline File_read::View*
105 File_read::find_view(off_t start, off_t size)
107 off_t page = File_read::page_offset(start);
108 Views::iterator p = this->views_.find(page);
109 if (p == this->views_.end())
111 if (p->second->size() - (start - page) < size)
116 // Read data from the file. Return the number of bytes read. If
117 // PBYTES is not NULL, store the number of bytes in *PBYTES, otherwise
118 // require that we read exactly the number of bytes requested.
121 File_read::do_read(off_t start, off_t size, void* p, off_t* pbytes)
123 gold_assert(this->lock_count_ > 0);
124 int o = this->descriptor_;
126 if (lseek(o, start, SEEK_SET) < 0)
128 fprintf(stderr, _("%s: %s: lseek to %lld failed: %s"),
129 program_name, this->filename().c_str(),
130 static_cast<long long>(start),
135 off_t bytes = ::read(o, p, size);
138 fprintf(stderr, _("%s: %s: read failed: %s\n"),
139 program_name, this->filename().c_str(), strerror(errno));
145 else if (bytes != size)
148 _("%s: %s: file too short: read only %lld of %lld "
150 program_name, this->filename().c_str(),
151 static_cast<long long>(bytes),
152 static_cast<long long>(size),
153 static_cast<long long>(start));
161 File_read::read(off_t start, off_t size, void* p, off_t* pbytes)
163 gold_assert(this->lock_count_ > 0);
165 File_read::View* pv = this->find_view(start, size);
168 memcpy(p, pv->data() + (start - pv->start()), size);
174 this->do_read(start, size, p, pbytes);
177 // Find an existing view or make a new one.
180 File_read::find_or_make_view(off_t start, off_t size, off_t* pbytes)
182 gold_assert(this->lock_count_ > 0);
184 off_t poff = File_read::page_offset(start);
186 File_read::View* const vnull = NULL;
187 std::pair<Views::iterator, bool> ins =
188 this->views_.insert(std::make_pair(poff, vnull));
192 // There was an existing view at this offset.
193 File_read::View* v = ins.first->second;
194 if (v->size() - (start - v->start()) >= size)
201 // This view is not large enough.
202 this->saved_views_.push_back(v);
205 // We need to read data from the file.
207 off_t psize = File_read::pages(size + (start - poff));
208 unsigned char* p = new unsigned char[psize];
211 off_t bytes = this->do_read(poff, psize, p, &got_bytes);
213 File_read::View* v = new File_read::View(poff, bytes, p);
215 ins.first->second = v;
217 if (bytes - (start - poff) >= size)
226 *pbytes = bytes - (start - poff);
231 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
232 program_name, this->filename().c_str(),
233 static_cast<long long>(bytes - (start - poff)),
234 static_cast<long long>(size),
235 static_cast<long long>(start));
239 // This implementation of get_view just reads into a memory buffer,
240 // which we store on view_list_. At some point we should support
244 File_read::get_view(off_t start, off_t size, off_t* pbytes)
246 gold_assert(this->lock_count_ > 0);
247 File_read::View* pv = this->find_or_make_view(start, size, pbytes);
248 return pv->data() + (start - pv->start());
252 File_read::get_lasting_view(off_t start, off_t size, off_t* pbytes)
254 gold_assert(this->lock_count_ > 0);
255 File_read::View* pv = this->find_or_make_view(start, size, pbytes);
257 return new File_view(*this, pv, pv->data() + (start - pv->start()));
260 // Remove all the file views.
263 File_read::clear_views(bool destroying)
265 for (Views::iterator p = this->views_.begin();
266 p != this->views_.end();
269 if (!p->second->is_locked())
273 gold_assert(!destroying);
274 this->saved_views_.push_back(p->second);
277 this->views_.clear();
279 Saved_views::iterator p = this->saved_views_.begin();
280 while (p != this->saved_views_.end())
282 if (!(*p)->is_locked())
285 p = this->saved_views_.erase(p);
289 gold_assert(!destroying);
297 File_view::~File_view()
299 gold_assert(this->file_.is_locked());
300 this->view_->unlock();
306 Input_file::open(const General_options& options, const Dirsearch& dirpath)
309 if (!this->input_argument_.is_lib())
310 name = this->input_argument_.name();
313 std::string n1("lib");
314 n1 += this->input_argument_.name();
316 if (options.is_static())
323 name = dirpath.find(n1, n2);
326 fprintf(stderr, _("%s: cannot find %s\n"), program_name,
327 this->input_argument_.name());
332 if (!this->file_.open(name))
334 fprintf(stderr, _("%s: cannot open %s: %s\n"), program_name,
335 name.c_str(), strerror(errno));
340 } // End namespace gold.