1 // fileread.cc -- read files for gold
12 #include "dirsearch.h"
18 // Class File_read::View.
20 File_read::View::~View()
22 assert(!this->is_locked());
27 File_read::View::lock()
33 File_read::View::unlock()
35 assert(this->lock_count_
> 0);
40 File_read::View::is_locked()
42 return this->lock_count_
> 0;
47 // The File_read class is designed to support file descriptor caching,
48 // but this is not currently implemented.
50 File_read::~File_read()
52 assert(this->lock_count_
== 0);
53 if (this->descriptor_
>= 0)
55 if (close(this->descriptor_
) < 0)
56 fprintf(stderr
, _("%s: warning: close(%s) failed: %s"),
57 program_name
, this->name_
.c_str(), strerror(errno
));
58 this->descriptor_
= -1;
61 this->clear_views(true);
65 File_read::open(const std::string
& name
)
67 assert(this->lock_count_
== 0
68 && this->descriptor_
< 0
69 && this->name_
.empty());
71 this->descriptor_
= ::open(this->name_
.c_str(), O_RDONLY
);
73 return this->descriptor_
>= 0;
77 File_read::get_descriptor()
79 assert(this->lock_count_
> 0);
80 return this->descriptor_
;
92 assert(this->lock_count_
> 0);
97 File_read::is_locked()
99 return this->lock_count_
> 0;
102 // See if we have a view which covers the file starting at START for
103 // SIZE bytes. Return a pointer to the View if found, NULL if not.
105 inline File_read::View
*
106 File_read::find_view(off_t start
, off_t size
)
108 off_t page
= File_read::page_offset(start
);
109 Views::iterator p
= this->views_
.find(page
);
110 if (p
== this->views_
.end())
112 if (p
->second
->size() - (start
- page
) < size
)
117 // Read data from the file. Return the number of bytes read. If
118 // PBYTES is not NULL, store the number of bytes in *PBYTES, otherwise
119 // require that we read exactly the number of bytes requested.
122 File_read::do_read(off_t start
, off_t size
, void* p
, off_t
* pbytes
)
124 assert(this->lock_count_
> 0);
125 int o
= this->descriptor_
;
127 if (lseek(o
, start
, SEEK_SET
) < 0)
129 fprintf(stderr
, _("%s: %s: lseek to %lld failed: %s"),
130 program_name
, this->filename().c_str(),
131 static_cast<long long>(start
),
136 off_t bytes
= ::read(o
, p
, size
);
139 fprintf(stderr
, _("%s: %s: read failed: %s\n"),
140 program_name
, this->filename().c_str(), strerror(errno
));
146 else if (bytes
!= size
)
149 _("%s: %s: file too short: read only %lld of %lld "
151 program_name
, this->filename().c_str(),
152 static_cast<long long>(bytes
),
153 static_cast<long long>(size
),
154 static_cast<long long>(start
));
162 File_read::read(off_t start
, off_t size
, void* p
, off_t
* pbytes
)
164 assert(this->lock_count_
> 0);
166 File_read::View
* pv
= this->find_view(start
, size
);
169 memcpy(p
, pv
->data() + (start
- pv
->start()), size
);
175 this->do_read(start
, size
, p
, pbytes
);
178 // Find an existing view or make a new one.
181 File_read::find_or_make_view(off_t start
, off_t size
, off_t
* pbytes
)
183 assert(this->lock_count_
> 0);
185 off_t poff
= File_read::page_offset(start
);
187 File_read::View
* const vnull
= NULL
;
188 std::pair
<Views::iterator
, bool> ins
=
189 this->views_
.insert(std::make_pair(poff
, vnull
));
193 // There was an existing view at this offset.
194 File_read::View
* v
= ins
.first
->second
;
195 if (v
->size() - (start
- v
->start()) >= size
)
202 // This view is not large enough.
203 this->saved_views_
.push_back(v
);
206 // We need to read data from the file.
208 off_t psize
= File_read::pages(size
+ (start
- poff
));
209 unsigned char* p
= new unsigned char[psize
];
212 off_t bytes
= this->do_read(poff
, psize
, p
, &got_bytes
);
214 File_read::View
* v
= new File_read::View(poff
, bytes
, p
);
216 ins
.first
->second
= v
;
218 if (bytes
- (start
- poff
) >= size
)
227 *pbytes
= bytes
- (start
- poff
);
232 _("%s: %s: file too short: read only %lld of %lld bytes at %lld\n"),
233 program_name
, this->filename().c_str(),
234 static_cast<long long>(bytes
- (start
- poff
)),
235 static_cast<long long>(size
),
236 static_cast<long long>(start
));
240 // This implementation of get_view just reads into a memory buffer,
241 // which we store on view_list_. At some point we should support
245 File_read::get_view(off_t start
, off_t size
, off_t
* pbytes
)
247 assert(this->lock_count_
> 0);
248 File_read::View
* pv
= this->find_or_make_view(start
, size
, pbytes
);
249 return pv
->data() + (start
- pv
->start());
253 File_read::get_lasting_view(off_t start
, off_t size
, off_t
* pbytes
)
255 assert(this->lock_count_
> 0);
256 File_read::View
* pv
= this->find_or_make_view(start
, size
, pbytes
);
258 return new File_view(*this, pv
, pv
->data() + (start
- pv
->start()));
261 // Remove all the file views.
264 File_read::clear_views(bool destroying
)
266 for (Views::iterator p
= this->views_
.begin();
267 p
!= this->views_
.end();
270 if (!p
->second
->is_locked())
275 this->saved_views_
.push_back(p
->second
);
278 this->views_
.clear();
280 Saved_views::iterator p
= this->saved_views_
.begin();
281 while (p
!= this->saved_views_
.end())
283 if (!(*p
)->is_locked())
286 p
= this->saved_views_
.erase(p
);
298 File_view::~File_view()
300 assert(this->file_
.is_locked());
301 this->view_
->unlock();
307 Input_file::open(const General_options
& options
, const Dirsearch
& dirpath
)
310 if (!this->input_argument_
.is_lib())
311 name
= this->input_argument_
.name();
314 std::string
n1("lib");
315 n1
+= this->input_argument_
.name();
317 if (options
.is_static())
324 name
= dirpath
.find(n1
, n2
);
327 fprintf(stderr
, _("%s: cannot find %s\n"), program_name
,
328 this->input_argument_
.name());
333 if (!this->file_
.open(name
))
335 fprintf(stderr
, _("%s: cannot open %s: %s\n"), program_name
,
336 name
.c_str(), strerror(errno
));
341 } // End namespace gold.