1 // fileread.cc -- read files for gold
3 // Copyright 2006, 2007 Free Software Foundation, Inc.
4 // Written by Ian Lance Taylor <iant@google.com>.
6 // This file is part of gold.
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 3 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
21 // MA 02110-1301, USA.
31 #include "dirsearch.h"
37 // Class File_read::View.
39 File_read::View::~View()
41 gold_assert(!this->is_locked());
46 File_read::View::lock()
52 File_read::View::unlock()
54 gold_assert(this->lock_count_
> 0);
59 File_read::View::is_locked()
61 return this->lock_count_
> 0;
66 // The File_read class is designed to support file descriptor caching,
67 // but this is not currently implemented.
69 File_read::~File_read()
71 gold_assert(this->lock_count_
== 0);
72 if (this->descriptor_
>= 0)
74 if (close(this->descriptor_
) < 0)
75 fprintf(stderr
, _("%s: warning: close(%s) failed: %s"),
76 program_name
, this->name_
.c_str(), strerror(errno
));
77 this->descriptor_
= -1;
80 this->clear_views(true);
86 File_read::open(const std::string
& name
)
88 gold_assert(this->lock_count_
== 0
89 && this->descriptor_
< 0
90 && this->name_
.empty());
93 this->descriptor_
= ::open(this->name_
.c_str(), O_RDONLY
);
95 if (this->descriptor_
>= 0)
98 if (::fstat(this->descriptor_
, &s
) < 0)
100 fprintf(stderr
, _("%s: %s: fstat failed: %s"), program_name
,
101 this->name_
.c_str(), strerror(errno
));
104 this->size_
= s
.st_size
;
109 return this->descriptor_
>= 0;
112 // Open the file for testing purposes.
115 File_read::open(const std::string
& name
, const unsigned char* contents
,
118 gold_assert(this->lock_count_
== 0
119 && this->descriptor_
< 0
120 && this->name_
.empty());
122 this->contents_
= contents
;
137 gold_assert(this->lock_count_
> 0);
142 File_read::is_locked()
144 return this->lock_count_
> 0;
147 // See if we have a view which covers the file starting at START for
148 // SIZE bytes. Return a pointer to the View if found, NULL if not.
150 inline File_read::View
*
151 File_read::find_view(off_t start
, off_t size
)
153 off_t page
= File_read::page_offset(start
);
154 Views::iterator p
= this->views_
.find(page
);
155 if (p
== this->views_
.end())
157 if (p
->second
->size() - (start
- page
) < size
)
162 // Read SIZE bytes from the file starting at offset START. Read into
163 // the buffer at P. Return the number of bytes read, which should
164 // always be at least SIZE except at the end of the file.
167 File_read::do_read(off_t start
, off_t size
, void* p
)
169 gold_assert(this->lock_count_
> 0);
171 if (this->contents_
!= NULL
)
173 off_t bytes
= this->size_
- start
;
176 else if (bytes
> size
)
178 memcpy(p
, this->contents_
+ start
, bytes
);
182 off_t bytes
= ::pread(this->descriptor_
, p
, size
, start
);
185 fprintf(stderr
, _("%s: %s: pread failed: %s\n"),
186 program_name
, this->filename().c_str(), strerror(errno
));
193 // Read exactly SIZE bytes from the file starting at offset START.
194 // Read into the buffer at P.
197 File_read::do_read_exact(off_t start
, off_t size
, void* p
)
199 off_t bytes
= this->do_read(start
, size
, p
);
203 _("%s: %s: file too short: read only %lld of %lld "
205 program_name
, this->filename().c_str(),
206 static_cast<long long>(bytes
),
207 static_cast<long long>(size
),
208 static_cast<long long>(start
));
213 // Read data from the file.
216 File_read::read(off_t start
, off_t size
, void* p
)
218 gold_assert(this->lock_count_
> 0);
220 File_read::View
* pv
= this->find_view(start
, size
);
223 memcpy(p
, pv
->data() + (start
- pv
->start()), size
);
227 this->do_read_exact(start
, size
, p
);
230 // Find an existing view or make a new one.
233 File_read::find_or_make_view(off_t start
, off_t size
)
235 gold_assert(this->lock_count_
> 0);
237 off_t poff
= File_read::page_offset(start
);
239 File_read::View
* const vnull
= NULL
;
240 std::pair
<Views::iterator
, bool> ins
=
241 this->views_
.insert(std::make_pair(poff
, vnull
));
245 // There was an existing view at this offset.
246 File_read::View
* v
= ins
.first
->second
;
247 if (v
->size() - (start
- v
->start()) >= size
)
250 // This view is not large enough.
251 this->saved_views_
.push_back(v
);
254 // We need to read data from the file. We read full pages for
255 // greater efficiency on small files.
257 off_t psize
= File_read::pages(size
+ (start
- poff
));
259 if (poff
+ psize
>= this->size_
)
261 psize
= this->size_
- poff
;
262 gold_assert(psize
>= size
);
265 unsigned char* p
= new unsigned char[psize
];
267 this->do_read_exact(poff
, psize
, p
);
269 File_read::View
* v
= new File_read::View(poff
, psize
, p
);
270 ins
.first
->second
= v
;
274 // This implementation of get_view just reads into a memory buffer,
275 // which we store on view_list_. At some point we should support
279 File_read::get_view(off_t start
, off_t size
)
281 gold_assert(this->lock_count_
> 0);
282 File_read::View
* pv
= this->find_or_make_view(start
, size
);
283 return pv
->data() + (start
- pv
->start());
287 File_read::get_lasting_view(off_t start
, off_t size
)
289 gold_assert(this->lock_count_
> 0);
290 File_read::View
* pv
= this->find_or_make_view(start
, size
);
292 return new File_view(*this, pv
, pv
->data() + (start
- pv
->start()));
295 // Remove all the file views.
298 File_read::clear_views(bool destroying
)
300 for (Views::iterator p
= this->views_
.begin();
301 p
!= this->views_
.end();
304 if (!p
->second
->is_locked())
308 gold_assert(!destroying
);
309 this->saved_views_
.push_back(p
->second
);
312 this->views_
.clear();
314 Saved_views::iterator p
= this->saved_views_
.begin();
315 while (p
!= this->saved_views_
.end())
317 if (!(*p
)->is_locked())
320 p
= this->saved_views_
.erase(p
);
324 gold_assert(!destroying
);
332 File_view::~File_view()
334 gold_assert(this->file_
.is_locked());
335 this->view_
->unlock();
340 // Create a file for testing.
342 Input_file::Input_file(const char* name
, const unsigned char* contents
,
346 this->input_argument_
=
347 new Input_file_argument(name
, false, Position_dependent_options());
348 bool ok
= file_
.open(name
, contents
, size
);
355 Input_file::open(const General_options
& options
, const Dirsearch
& dirpath
)
358 if (!this->input_argument_
->is_lib())
359 name
= this->input_argument_
->name();
362 std::string
n1("lib");
363 n1
+= this->input_argument_
->name();
365 if (options
.is_static())
372 name
= dirpath
.find(n1
, n2
);
375 fprintf(stderr
, _("%s: cannot find %s\n"), program_name
,
376 this->input_argument_
->name());
381 if (!this->file_
.open(name
))
383 fprintf(stderr
, _("%s: cannot open %s: %s\n"), program_name
,
384 name
.c_str(), strerror(errno
));
389 } // End namespace gold.
This page took 0.038342 seconds and 5 git commands to generate.