/* BFD library -- caching of file descriptors.
Copyright 1990, 1991, 1992, 1993, 1994, 1996, 2000, 2001, 2002,
- 2003, 2004, 2005 Free Software Foundation, Inc.
+ 2003, 2004, 2005, 2007 Free Software Foundation, Inc.
Hacked by Steve Chamberlain of Cygnus Support (steve@cygnus.com).
-This file is part of BFD, the Binary File Descriptor library.
+ This file is part of BFD, the Binary File Descriptor library.
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 2 of the License, or
-(at your option) any later version.
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-GNU General Public License for more details.
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
-You should have received a copy of the GNU General Public License
-along with this program; if not, write to the Free Software
-Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA 02110-1301, USA. */
+ You should have received a copy of the GNU General Public License
+ along with this program; if not, write to the Free Software
+ Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
+ MA 02110-1301, USA. */
/*
SECTION
Caching functions
*/
-#include "bfd.h"
#include "sysdep.h"
+#include "bfd.h"
#include "libbfd.h"
#include "libiberty.h"
+/* In some cases we can optimize cache operation when reopening files.
+ For instance, a flush is entirely unnecessary if the file is already
+ closed, so a flush would use CACHE_NO_OPEN. Similarly, a seek using
+ SEEK_SET or SEEK_END need not first seek to the current position.
+ For stat we ignore seek errors, just in case the file has changed
+ while we weren't looking. If it has, then it's possible that the
+ file is shorter and we don't want a seek error to prevent us doing
+ the stat. */
+enum cache_flag {
+ CACHE_NORMAL = 0,
+ CACHE_NO_OPEN = 1,
+ CACHE_NO_SEEK = 2,
+ CACHE_NO_SEEK_ERROR = 4
+};
+
/* The maximum number of files which the cache will keep open at
one time. */
impunity, since it can't have changed since the last lookup;
otherwise, it has to perform the complicated lookup function. */
-#define bfd_cache_lookup(x) \
+#define bfd_cache_lookup(x, flag) \
((x) == bfd_last_cache \
? (FILE *) (bfd_last_cache->iostream) \
- : bfd_cache_lookup_worker (x))
+ : bfd_cache_lookup_worker (x, flag))
/* Called when the macro <<bfd_cache_lookup>> fails to find a
quick answer. Find a file descriptor for @var{abfd}. If
if it is unable to (re)open the @var{abfd}. */
static FILE *
-bfd_cache_lookup_worker (bfd *abfd)
+bfd_cache_lookup_worker (bfd *abfd, enum cache_flag flag)
{
bfd *orig_bfd = abfd;
if ((abfd->flags & BFD_IN_MEMORY) != 0)
return (FILE *) abfd->iostream;
}
+ if (flag & CACHE_NO_OPEN)
+ return NULL;
+
if (bfd_open_file (abfd) == NULL)
;
- else if (real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0)
+ else if (!(flag & CACHE_NO_SEEK)
+ && real_fseek ((FILE *) abfd->iostream, abfd->where, SEEK_SET) != 0
+ && !(flag & CACHE_NO_SEEK_ERROR))
bfd_set_error (bfd_error_system_call);
else
return (FILE *) abfd->iostream;
static file_ptr
cache_btell (struct bfd *abfd)
{
- FILE *f = bfd_cache_lookup (abfd);
+ FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
if (f == NULL)
- return -1;
+ return abfd->where;
return real_ftell (f);
}
static int
cache_bseek (struct bfd *abfd, file_ptr offset, int whence)
{
- FILE *f = bfd_cache_lookup (abfd);
+ FILE *f = bfd_cache_lookup (abfd, whence != SEEK_CUR ? CACHE_NO_SEEK : 0);
if (f == NULL)
return -1;
return real_fseek (f, offset, whence);
first octet in the file, NOT the beginning of the archive header. */
static file_ptr
-cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
+cache_bread_1 (struct bfd *abfd, void *buf, file_ptr nbytes)
{
FILE *f;
file_ptr nread;
if (nbytes == 0)
return 0;
- f = bfd_cache_lookup (abfd);
+ f = bfd_cache_lookup (abfd, 0);
if (f == NULL)
return 0;
return -1;
}
#endif
+ if (nread < nbytes)
+ /* This may or may not be an error, but in case the calling code
+ bails out because of it, set the right error code. */
+ bfd_set_error (bfd_error_file_truncated);
+ return nread;
+}
+
+static file_ptr
+cache_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
+{
+ file_ptr nread = 0;
+
+ /* Some filesystems are unable to handle reads that are too large
+ (for instance, NetApp shares with oplocks turned off). To avoid
+ hitting this limitation, we read the buffer in chunks of 8MB max. */
+ while (nread < nbytes)
+ {
+ const file_ptr max_chunk_size = 0x800000;
+ file_ptr chunk_size = nbytes - nread;
+ file_ptr chunk_nread;
+
+ if (chunk_size > max_chunk_size)
+ chunk_size = max_chunk_size;
+
+ chunk_nread = cache_bread_1 (abfd, buf + nread, chunk_size);
+
+ /* Update the nread count.
+
+ We just have to be careful of the case when cache_bread_1 returns
+ a negative count: If this is our first read, then set nread to
+ that negative count in order to return that negative value to the
+ caller. Otherwise, don't add it to our total count, or we would
+ end up returning a smaller number of bytes read than we actually
+ did. */
+ if (nread == 0 || chunk_nread > 0)
+ nread += chunk_nread;
+
+ if (chunk_nread < chunk_size)
+ break;
+ }
+
return nread;
}
cache_bwrite (struct bfd *abfd, const void *where, file_ptr nbytes)
{
file_ptr nwrite;
- FILE *f = bfd_cache_lookup (abfd);
+ FILE *f = bfd_cache_lookup (abfd, 0);
if (f == NULL)
return 0;
nwrite = fwrite (where, 1, nbytes, f);
cache_bflush (struct bfd *abfd)
{
int sts;
- FILE *f = bfd_cache_lookup (abfd);
+ FILE *f = bfd_cache_lookup (abfd, CACHE_NO_OPEN);
if (f == NULL)
- return -1;
+ return 0;
sts = fflush (f);
if (sts < 0)
bfd_set_error (bfd_error_system_call);
cache_bstat (struct bfd *abfd, struct stat *sb)
{
int sts;
- FILE *f = bfd_cache_lookup (abfd);
+ FILE *f = bfd_cache_lookup (abfd, CACHE_NO_SEEK_ERROR);
if (f == NULL)
return -1;
sts = fstat (fileno (f), sb);
{
case read_direction:
case no_direction:
- abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RB);
+ abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RB);
break;
case both_direction:
case write_direction:
if (abfd->opened_once)
{
- abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_RUB);
+ abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_RUB);
if (abfd->iostream == NULL)
- abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
+ abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
}
else
{
if (stat (abfd->filename, &s) == 0 && s.st_size != 0)
unlink_if_ordinary (abfd->filename);
#endif
- abfd->iostream = (PTR) fopen (abfd->filename, FOPEN_WUB);
+ abfd->iostream = (PTR) real_fopen (abfd->filename, FOPEN_WUB);
abfd->opened_once = TRUE;
}
break;