Blame glib/gmappedfile.c

Packit ae235b
/* GLIB - Library of useful routines for C programming
Packit ae235b
 * gmappedfile.c: Simplified wrapper around the mmap() function.
Packit ae235b
 *
Packit ae235b
 * Copyright 2005 Matthias Clasen
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General Public
Packit ae235b
 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <errno.h>
Packit ae235b
#include <sys/types.h> 
Packit ae235b
#include <sys/stat.h> 
Packit ae235b
#include <fcntl.h>
Packit ae235b
#ifdef HAVE_MMAP
Packit ae235b
#include <sys/mman.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "glibconfig.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <unistd.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
#include <windows.h>
Packit ae235b
#include <io.h>
Packit ae235b
Packit ae235b
#undef fstat
Packit ae235b
#define fstat(a,b) _fstati64(a,b)
Packit ae235b
#undef stat
Packit ae235b
#define stat _stati64
Packit ae235b
Packit ae235b
#ifndef S_ISREG
Packit ae235b
#define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#include "gconvert.h"
Packit ae235b
#include "gerror.h"
Packit ae235b
#include "gfileutils.h"
Packit ae235b
#include "gmappedfile.h"
Packit ae235b
#include "gmem.h"
Packit ae235b
#include "gmessages.h"
Packit ae235b
#include "gstdio.h"
Packit ae235b
#include "gstrfuncs.h"
Packit ae235b
#include "gatomic.h"
Packit ae235b
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
Packit ae235b
#ifndef _O_BINARY
Packit ae235b
#define _O_BINARY 0
Packit ae235b
#endif
Packit ae235b
Packit ae235b
#ifndef MAP_FAILED
Packit ae235b
#define MAP_FAILED ((void *) -1)
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * GMappedFile:
Packit ae235b
 *
Packit ae235b
 * The #GMappedFile represents a file mapping created with
Packit ae235b
 * g_mapped_file_new(). It has only private members and should
Packit ae235b
 * not be accessed directly.
Packit ae235b
 */
Packit ae235b
Packit ae235b
struct _GMappedFile
Packit ae235b
{
Packit ae235b
  gchar *contents;
Packit ae235b
  gsize  length;
Packit ae235b
  gpointer free_func;
Packit ae235b
  int    ref_count;
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  HANDLE mapping;
Packit ae235b
#endif
Packit ae235b
};
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_mapped_file_destroy (GMappedFile *file)
Packit ae235b
{
Packit ae235b
  if (file->length)
Packit ae235b
    {
Packit ae235b
#ifdef HAVE_MMAP
Packit ae235b
      munmap (file->contents, file->length);
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
      UnmapViewOfFile (file->contents);
Packit ae235b
      CloseHandle (file->mapping);
Packit ae235b
#endif
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_slice_free (GMappedFile, file);
Packit ae235b
}
Packit ae235b
Packit ae235b
static GMappedFile*
Packit ae235b
mapped_file_new_from_fd (int           fd,
Packit ae235b
			 gboolean      writable,
Packit ae235b
                         const gchar  *filename,
Packit ae235b
                         GError      **error)
Packit ae235b
{
Packit ae235b
  GMappedFile *file;
Packit ae235b
  struct stat st;
Packit ae235b
Packit ae235b
  file = g_slice_new0 (GMappedFile);
Packit ae235b
  file->ref_count = 1;
Packit ae235b
  file->free_func = g_mapped_file_destroy;
Packit ae235b
Packit ae235b
  if (fstat (fd, &st) == -1)
Packit ae235b
    {
Packit ae235b
      int save_errno = errno;
Packit ae235b
      gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
Packit ae235b
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_FILE_ERROR,
Packit ae235b
                   g_file_error_from_errno (save_errno),
Packit ae235b
                   _("Failed to get attributes of file ā€œ%s%s%s%sā€: fstat() failed: %s"),
Packit ae235b
		   display_filename ? display_filename : "fd",
Packit ae235b
		   display_filename ? "' " : "",
Packit ae235b
		   display_filename ? display_filename : "",
Packit ae235b
		   display_filename ? "'" : "",
Packit ae235b
		   g_strerror (save_errno));
Packit ae235b
      g_free (display_filename);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  /* mmap() on size 0 will fail with EINVAL, so we avoid calling mmap()
Packit ae235b
   * in that case -- but only if we have a regular file; we still want
Packit ae235b
   * attempts to mmap a character device to fail, for example.
Packit ae235b
   */
Packit ae235b
  if (st.st_size == 0 && S_ISREG (st.st_mode))
Packit ae235b
    {
Packit ae235b
      file->length = 0;
Packit ae235b
      file->contents = NULL;
Packit ae235b
      return file;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  file->contents = MAP_FAILED;
Packit ae235b
Packit ae235b
#ifdef HAVE_MMAP
Packit ae235b
  if (st.st_size > G_MAXSIZE)
Packit ae235b
    {
Packit ae235b
      errno = EINVAL;
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {      
Packit ae235b
      file->length = (gsize) st.st_size;
Packit ae235b
      file->contents = (gchar *) mmap (NULL,  file->length,
Packit ae235b
				       writable ? PROT_READ|PROT_WRITE : PROT_READ,
Packit ae235b
				       MAP_PRIVATE, fd, 0);
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
#ifdef G_OS_WIN32
Packit ae235b
  file->length = st.st_size;
Packit ae235b
  file->mapping = CreateFileMapping ((HANDLE) _get_osfhandle (fd), NULL,
Packit ae235b
				     writable ? PAGE_WRITECOPY : PAGE_READONLY,
Packit ae235b
				     0, 0,
Packit ae235b
				     NULL);
Packit ae235b
  if (file->mapping != NULL)
Packit ae235b
    {
Packit ae235b
      file->contents = MapViewOfFile (file->mapping,
Packit ae235b
				      writable ? FILE_MAP_COPY : FILE_MAP_READ,
Packit ae235b
				      0, 0,
Packit ae235b
				      0);
Packit ae235b
      if (file->contents == NULL)
Packit ae235b
	{
Packit ae235b
	  file->contents = MAP_FAILED;
Packit ae235b
	  CloseHandle (file->mapping);
Packit ae235b
	  file->mapping = NULL;
Packit ae235b
	}
Packit ae235b
    }
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  
Packit ae235b
  if (file->contents == MAP_FAILED)
Packit ae235b
    {
Packit ae235b
      int save_errno = errno;
Packit ae235b
      gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
Packit ae235b
Packit ae235b
      g_set_error (error,
Packit ae235b
		   G_FILE_ERROR,
Packit ae235b
		   g_file_error_from_errno (save_errno),
Packit ae235b
		   _("Failed to map %s%s%s%s: mmap() failed: %s"),
Packit ae235b
		   display_filename ? display_filename : "fd",
Packit ae235b
		   display_filename ? "' " : "",
Packit ae235b
		   display_filename ? display_filename : "",
Packit ae235b
		   display_filename ? "'" : "",
Packit ae235b
		   g_strerror (save_errno));
Packit ae235b
      g_free (display_filename);
Packit ae235b
      goto out;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  return file;
Packit ae235b
Packit ae235b
 out:
Packit ae235b
  g_slice_free (GMappedFile, file);
Packit ae235b
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mapped_file_new:
Packit ae235b
 * @filename: (type filename): The path of the file to load, in the GLib
Packit ae235b
 *     filename encoding
Packit ae235b
 * @writable: whether the mapping should be writable
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Maps a file into memory. On UNIX, this is using the mmap() function.
Packit ae235b
 *
Packit ae235b
 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
Packit ae235b
 * it is an error to modify the mapped buffer. Modifications to the buffer
Packit ae235b
 * are not visible to other processes mapping the same file, and are not
Packit ae235b
 * written back to the file.
Packit ae235b
 *
Packit ae235b
 * Note that modifications of the underlying file might affect the contents
Packit ae235b
 * of the #GMappedFile. Therefore, mapping should only be used if the file
Packit ae235b
 * will not be modified, or if all modifications of the file are done
Packit ae235b
 * atomically (e.g. using g_file_set_contents()).
Packit ae235b
 *
Packit ae235b
 * If @filename is the name of an empty, regular file, the function
Packit ae235b
 * will successfully return an empty #GMappedFile. In other cases of
Packit ae235b
 * size 0 (e.g. device files such as /dev/null), @error will be set
Packit ae235b
 * to the #GFileError value #G_FILE_ERROR_INVAL.
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GMappedFile which must be unref'd
Packit ae235b
 *    with g_mapped_file_unref(), or %NULL if the mapping failed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
GMappedFile *
Packit ae235b
g_mapped_file_new (const gchar  *filename,
Packit ae235b
		   gboolean      writable,
Packit ae235b
		   GError      **error)
Packit ae235b
{
Packit ae235b
  GMappedFile *file;
Packit ae235b
  int fd;
Packit ae235b
Packit ae235b
  g_return_val_if_fail (filename != NULL, NULL);
Packit ae235b
  g_return_val_if_fail (!error || *error == NULL, NULL);
Packit ae235b
Packit ae235b
  fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
Packit ae235b
  if (fd == -1)
Packit ae235b
    {
Packit ae235b
      int save_errno = errno;
Packit ae235b
      gchar *display_filename = g_filename_display_name (filename);
Packit ae235b
Packit ae235b
      g_set_error (error,
Packit ae235b
                   G_FILE_ERROR,
Packit ae235b
                   g_file_error_from_errno (save_errno),
Packit ae235b
                   _("Failed to open file ā€œ%sā€: open() failed: %s"),
Packit ae235b
                   display_filename,
Packit ae235b
		   g_strerror (save_errno));
Packit ae235b
      g_free (display_filename);
Packit ae235b
      return NULL;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  file = mapped_file_new_from_fd (fd, writable, filename, error);
Packit ae235b
Packit ae235b
  close (fd);
Packit ae235b
Packit ae235b
  return file;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mapped_file_new_from_fd:
Packit ae235b
 * @fd: The file descriptor of the file to load
Packit ae235b
 * @writable: whether the mapping should be writable
Packit ae235b
 * @error: return location for a #GError, or %NULL
Packit ae235b
 *
Packit ae235b
 * Maps a file into memory. On UNIX, this is using the mmap() function.
Packit ae235b
 *
Packit ae235b
 * If @writable is %TRUE, the mapped buffer may be modified, otherwise
Packit ae235b
 * it is an error to modify the mapped buffer. Modifications to the buffer
Packit ae235b
 * are not visible to other processes mapping the same file, and are not
Packit ae235b
 * written back to the file.
Packit ae235b
 *
Packit ae235b
 * Note that modifications of the underlying file might affect the contents
Packit ae235b
 * of the #GMappedFile. Therefore, mapping should only be used if the file
Packit ae235b
 * will not be modified, or if all modifications of the file are done
Packit ae235b
 * atomically (e.g. using g_file_set_contents()).
Packit ae235b
 *
Packit ae235b
 * Returns: a newly allocated #GMappedFile which must be unref'd
Packit ae235b
 *    with g_mapped_file_unref(), or %NULL if the mapping failed.
Packit ae235b
 *
Packit ae235b
 * Since: 2.32
Packit ae235b
 */
Packit ae235b
GMappedFile *
Packit ae235b
g_mapped_file_new_from_fd (gint          fd,
Packit ae235b
			   gboolean      writable,
Packit ae235b
			   GError      **error)
Packit ae235b
{
Packit ae235b
  return mapped_file_new_from_fd (fd, writable, NULL, error);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mapped_file_get_length:
Packit ae235b
 * @file: a #GMappedFile
Packit ae235b
 *
Packit ae235b
 * Returns the length of the contents of a #GMappedFile.
Packit ae235b
 *
Packit ae235b
 * Returns: the length of the contents of @file.
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
gsize
Packit ae235b
g_mapped_file_get_length (GMappedFile *file)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (file != NULL, 0);
Packit ae235b
Packit ae235b
  return file->length;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mapped_file_get_contents:
Packit ae235b
 * @file: a #GMappedFile
Packit ae235b
 *
Packit ae235b
 * Returns the contents of a #GMappedFile. 
Packit ae235b
 *
Packit ae235b
 * Note that the contents may not be zero-terminated,
Packit ae235b
 * even if the #GMappedFile is backed by a text file.
Packit ae235b
 *
Packit ae235b
 * If the file is empty then %NULL is returned.
Packit ae235b
 *
Packit ae235b
 * Returns: the contents of @file, or %NULL.
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 */
Packit ae235b
gchar *
Packit ae235b
g_mapped_file_get_contents (GMappedFile *file)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (file != NULL, NULL);
Packit ae235b
Packit ae235b
  return file->contents;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mapped_file_free:
Packit ae235b
 * @file: a #GMappedFile
Packit ae235b
 *
Packit ae235b
 * This call existed before #GMappedFile had refcounting and is currently
Packit ae235b
 * exactly the same as g_mapped_file_unref().
Packit ae235b
 *
Packit ae235b
 * Since: 2.8
Packit ae235b
 * Deprecated:2.22: Use g_mapped_file_unref() instead.
Packit ae235b
 */
Packit ae235b
void
Packit ae235b
g_mapped_file_free (GMappedFile *file)
Packit ae235b
{
Packit ae235b
  g_mapped_file_unref (file);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mapped_file_ref:
Packit ae235b
 * @file: a #GMappedFile
Packit ae235b
 *
Packit ae235b
 * Increments the reference count of @file by one.  It is safe to call
Packit ae235b
 * this function from any thread.
Packit ae235b
 *
Packit ae235b
 * Returns: the passed in #GMappedFile.
Packit ae235b
 *
Packit ae235b
 * Since: 2.22
Packit ae235b
 **/
Packit ae235b
GMappedFile *
Packit ae235b
g_mapped_file_ref (GMappedFile *file)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (file != NULL, NULL);
Packit ae235b
Packit ae235b
  g_atomic_int_inc (&file->ref_count);
Packit ae235b
Packit ae235b
  return file;
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mapped_file_unref:
Packit ae235b
 * @file: a #GMappedFile
Packit ae235b
 *
Packit ae235b
 * Decrements the reference count of @file by one.  If the reference count
Packit ae235b
 * drops to 0, unmaps the buffer of @file and frees it.
Packit ae235b
 *
Packit ae235b
 * It is safe to call this function from any thread.
Packit ae235b
 *
Packit ae235b
 * Since 2.22
Packit ae235b
 **/
Packit ae235b
void
Packit ae235b
g_mapped_file_unref (GMappedFile *file)
Packit ae235b
{
Packit ae235b
  g_return_if_fail (file != NULL);
Packit ae235b
Packit ae235b
  if (g_atomic_int_dec_and_test (&file->ref_count))
Packit ae235b
    g_mapped_file_destroy (file);
Packit ae235b
}
Packit ae235b
Packit ae235b
/**
Packit ae235b
 * g_mapped_file_get_bytes:
Packit ae235b
 * @file: a #GMappedFile
Packit ae235b
 *
Packit ae235b
 * Creates a new #GBytes which references the data mapped from @file.
Packit ae235b
 * The mapped contents of the file must not be modified after creating this
Packit ae235b
 * bytes object, because a #GBytes should be immutable.
Packit ae235b
 *
Packit ae235b
 * Returns: (transfer full): A newly allocated #GBytes referencing data
Packit ae235b
 *     from @file
Packit ae235b
 *
Packit ae235b
 * Since: 2.34
Packit ae235b
 **/
Packit ae235b
GBytes *
Packit ae235b
g_mapped_file_get_bytes (GMappedFile *file)
Packit ae235b
{
Packit ae235b
  g_return_val_if_fail (file != NULL, NULL);
Packit ae235b
Packit ae235b
  return g_bytes_new_with_free_func (file->contents,
Packit ae235b
				     file->length,
Packit ae235b
				     (GDestroyNotify) g_mapped_file_unref,
Packit ae235b
				     g_mapped_file_ref (file));
Packit ae235b
}