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);
66 File_read::open(const std::string
& name
)
68 gold_assert(this->lock_count_
== 0
69 && this->descriptor_
< 0
70 && this->name_
.empty());
72 this->descriptor_
= ::open(this->name_
.c_str(), O_RDONLY
);
74 return this->descriptor_
>= 0;
77 // Open the file for testing purposes.
80 File_read::open(const std::string
& name
, const unsigned char* contents
,
83 gold_assert(this->lock_count_
== 0
84 && this->descriptor_
< 0
85 && this->name_
.empty());
87 this->contents_
= contents
;
88 this->contents_size_
= contents_size
;
102 gold_assert(this->lock_count_
> 0);
107 File_read::is_locked()
109 return this->lock_count_
> 0;
112 // See if we have a view which covers the file starting at START for
113 // SIZE bytes. Return a pointer to the View if found, NULL if not.
115 inline File_read::View
*
116 File_read::find_view(off_t start
, off_t size
)
118 off_t page
= File_read::page_offset(start
);
119 Views::iterator p
= this->views_
.find(page
);
120 if (p
== this->views_
.end())
122 if (p
->second
->size() - (start
- page
) < size
)
127 // Read data from the file. Return the number of bytes read. If
128 // PBYTES is not NULL, store the number of bytes in *PBYTES, otherwise
129 // require that we read exactly the number of bytes requested.
132 File_read::do_read(off_t start
, off_t size
, void* p
, off_t
* pbytes
)
134 gold_assert(this->lock_count_
> 0);
137 if (this->contents_
== NULL
)
139 int o
= this->descriptor_
;
141 if (lseek(o
, start
, SEEK_SET
) < 0)
143 fprintf(stderr
, _("%s: %s: lseek to %lld failed: %s"),
144 program_name
, this->filename().c_str(),
145 static_cast<long long>(start
),
150 bytes
= ::read(o
, p
, size
);
153 fprintf(stderr
, _("%s: %s: read failed: %s\n"),
154 program_name
, this->filename().c_str(), strerror(errno
));
160 bytes
= this->contents_size_
- start
;
163 else if (bytes
> size
)
165 memcpy(p
, this->contents_
+ start
, bytes
);
170 else if (bytes
!= size
)
173 _("%s: %s: file too short: read only %lld of %lld "
175 program_name
, this->filename().c_str(),
176 static_cast<long long>(bytes
),
177 static_cast<long long>(size
),
178 static_cast<long long>(start
));
186 File_read::read(off_t start
, off_t size
, void* p
, off_t
* pbytes
)
188 gold_assert(this->lock_count_
> 0);
190 File_read::View
* pv
= this->find_view(start
, size
);
193 memcpy(p
, pv
->data() + (start
- pv
->start()), size
);
199 this->do_read(start
, size
, p
, pbytes
);
202 // Find an existing view or make a new one.
205 File_read::find_or_make_view(off_t start
, off_t size
, off_t
* pbytes
)
207 gold_assert(this->lock_count_
> 0);
209 off_t poff
= File_read::page_offset(start
);
211 File_read::View
* const vnull
= NULL
;
212 std::pair
<Views::iterator
, bool> ins
=
213 this->views_
.insert(std::make_pair(poff
, vnull
));
217 // There was an existing view at this offset.
218 File_read::View
* v
= ins
.first
->second
;
219 if (v
->size() - (start
- v
->start()) >= size
)
226 // This view is not large enough.
227 this->saved_views_
.push_back(v
);
230 // We need to read data from the file.
232 off_t psize
= File_read::pages(size
+ (start
- poff
));
233 unsigned char* p
= new unsigned char[psize
];
236 off_t bytes
= this->do_read(poff
, psize
, p
, &got_bytes
);
238 File_read::View
* v
= new File_read::View(poff
, bytes
, p
);
240 ins
.first
->second
= v
;
242 if (bytes
- (start
- poff
) >= size
)
251 *pbytes
= bytes
- (start
- poff
);
256 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
257 program_name
, this->filename().c_str(),
258 static_cast<long long>(bytes
- (start
- poff
)),
259 static_cast<long long>(size
),
260 static_cast<long long>(start
));
264 // This implementation of get_view just reads into a memory buffer,
265 // which we store on view_list_. At some point we should support
269 File_read::get_view(off_t start
, off_t size
, off_t
* pbytes
)
271 gold_assert(this->lock_count_
> 0);
272 File_read::View
* pv
= this->find_or_make_view(start
, size
, pbytes
);
273 return pv
->data() + (start
- pv
->start());
277 File_read::get_lasting_view(off_t start
, off_t size
, off_t
* pbytes
)
279 gold_assert(this->lock_count_
> 0);
280 File_read::View
* pv
= this->find_or_make_view(start
, size
, pbytes
);
282 return new File_view(*this, pv
, pv
->data() + (start
- pv
->start()));
285 // Remove all the file views.
288 File_read::clear_views(bool destroying
)
290 for (Views::iterator p
= this->views_
.begin();
291 p
!= this->views_
.end();
294 if (!p
->second
->is_locked())
298 gold_assert(!destroying
);
299 this->saved_views_
.push_back(p
->second
);
302 this->views_
.clear();
304 Saved_views::iterator p
= this->saved_views_
.begin();
305 while (p
!= this->saved_views_
.end())
307 if (!(*p
)->is_locked())
310 p
= this->saved_views_
.erase(p
);
314 gold_assert(!destroying
);
322 File_view::~File_view()
324 gold_assert(this->file_
.is_locked());
325 this->view_
->unlock();
330 // Create a file for testing.
332 Input_file::Input_file(const char* name
, const unsigned char* contents
,
336 this->input_argument_
=
337 new Input_file_argument(name
, false, Position_dependent_options());
338 bool ok
= file_
.open(name
, contents
, size
);
345 Input_file::open(const General_options
& options
, const Dirsearch
& dirpath
)
348 if (!this->input_argument_
->is_lib())
349 name
= this->input_argument_
->name();
352 std::string
n1("lib");
353 n1
+= this->input_argument_
->name();
355 if (options
.is_static())
362 name
= dirpath
.find(n1
, n2
);
365 fprintf(stderr
, _("%s: cannot find %s\n"), program_name
,
366 this->input_argument_
->name());
371 if (!this->file_
.open(name
))
373 fprintf(stderr
, _("%s: cannot open %s: %s\n"), program_name
,
374 name
.c_str(), strerror(errno
));
379 } // End namespace gold.
This page took 0.036478 seconds and 4 git commands to generate.