+
+/* Returns the size of the named file. If the file does not
+ exist, or if it is not a real file, then a suitable non-fatal
+ error message is printed and (off_t) -1 is returned. */
+
+off_t
+get_file_size (const char * file_name)
+{
+ struct stat statbuf;
+
+ if (file_name == NULL)
+ return (off_t) -1;
+
+ if (stat (file_name, &statbuf) < 0)
+ {
+ if (errno == ENOENT)
+ non_fatal (_("'%s': No such file"), file_name);
+ else
+ non_fatal (_("Warning: could not locate '%s'. reason: %s"),
+ file_name, strerror (errno));
+ }
+ else if (S_ISDIR (statbuf.st_mode))
+ non_fatal (_("Warning: '%s' is a directory"), file_name);
+ else if (! S_ISREG (statbuf.st_mode))
+ non_fatal (_("Warning: '%s' is not an ordinary file"), file_name);
+ else if (statbuf.st_size < 0)
+ non_fatal (_("Warning: '%s' has negative size, probably it is too large"),
+ file_name);
+ else
+ return statbuf.st_size;
+
+ return (off_t) -1;
+}
+
+/* Return the filename in a static buffer. */
+
+const char *
+bfd_get_archive_filename (const bfd *abfd)
+{
+ static size_t curr = 0;
+ static char *buf;
+ size_t needed;
+
+ assert (abfd != NULL);
+
+ if (abfd->my_archive == NULL
+ || bfd_is_thin_archive (abfd->my_archive))
+ return bfd_get_filename (abfd);
+
+ needed = (strlen (bfd_get_filename (abfd->my_archive))
+ + strlen (bfd_get_filename (abfd)) + 3);
+ if (needed > curr)
+ {
+ if (curr)
+ free (buf);
+ curr = needed + (needed >> 1);
+ buf = (char *) xmalloc (curr);
+ }
+ sprintf (buf, "%s(%s)", bfd_get_filename (abfd->my_archive),
+ bfd_get_filename (abfd));
+ return buf;
+}
+
+/* Returns TRUE iff PATHNAME, a filename of an archive member,
+ is valid for writing. For security reasons absolute paths
+ and paths containing /../ are not allowed. See PR 17533. */
+
+bfd_boolean
+is_valid_archive_path (char const * pathname)
+{
+ const char * n = pathname;
+
+ if (IS_ABSOLUTE_PATH (n))
+ return FALSE;
+
+ while (*n)
+ {
+ if (*n == '.' && *++n == '.' && ( ! *++n || IS_DIR_SEPARATOR (*n)))
+ return FALSE;
+
+ while (*n && ! IS_DIR_SEPARATOR (*n))
+ n++;
+ while (IS_DIR_SEPARATOR (*n))
+ n++;
+ }
+
+ return TRUE;
+}