mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2024-12-27 16:06:16 +01:00
GMappedFile: Add API to create from an existing file descriptor
At Tracker we want to mmap files using O_NOATIME. With GMappedFile this is at the moment impossible. For that reason I added the constructor new_from_fd to the GMappedFile type. https://bugzilla.gnome.org/show_bug.cgi?id=659754
This commit is contained in:
parent
24652730a9
commit
ca154c399b
@ -636,6 +636,7 @@ g_timeout_add_seconds_full
|
|||||||
g_timeout_source_new
|
g_timeout_source_new
|
||||||
g_timeout_source_new_seconds
|
g_timeout_source_new_seconds
|
||||||
g_mapped_file_new
|
g_mapped_file_new
|
||||||
|
g_mapped_file_new_from_fd
|
||||||
g_mapped_file_get_length
|
g_mapped_file_get_length
|
||||||
g_mapped_file_get_contents
|
g_mapped_file_get_contents
|
||||||
g_mapped_file_ref
|
g_mapped_file_ref
|
||||||
|
@ -103,62 +103,15 @@ g_mapped_file_destroy (GMappedFile *file)
|
|||||||
g_slice_free (GMappedFile, file);
|
g_slice_free (GMappedFile, file);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
static GMappedFile*
|
||||||
* g_mapped_file_new:
|
mapped_file_new_from_fd (int fd,
|
||||||
* @filename: The path of the file to load, in the GLib filename encoding
|
gboolean writable,
|
||||||
* @writable: whether the mapping should be writable
|
const gchar *filename,
|
||||||
* @error: return location for a #GError, or %NULL
|
GError **error)
|
||||||
*
|
|
||||||
* Maps a file into memory. On UNIX, this is using the mmap() function.
|
|
||||||
*
|
|
||||||
* If @writable is %TRUE, the mapped buffer may be modified, otherwise
|
|
||||||
* it is an error to modify the mapped buffer. Modifications to the buffer
|
|
||||||
* are not visible to other processes mapping the same file, and are not
|
|
||||||
* written back to the file.
|
|
||||||
*
|
|
||||||
* Note that modifications of the underlying file might affect the contents
|
|
||||||
* of the #GMappedFile. Therefore, mapping should only be used if the file
|
|
||||||
* will not be modified, or if all modifications of the file are done
|
|
||||||
* atomically (e.g. using g_file_set_contents()).
|
|
||||||
*
|
|
||||||
* If @filename is the name of an empty, regular file, the function
|
|
||||||
* will successfully return an empty #GMappedFile. In other cases of
|
|
||||||
* size 0 (e.g. device files such as /dev/null), @error will be set
|
|
||||||
* to the #GFileError value #G_FILE_ERROR_INVAL.
|
|
||||||
*
|
|
||||||
* Return value: a newly allocated #GMappedFile which must be unref'd
|
|
||||||
* with g_mapped_file_unref(), or %NULL if the mapping failed.
|
|
||||||
*
|
|
||||||
* Since: 2.8
|
|
||||||
*/
|
|
||||||
GMappedFile *
|
|
||||||
g_mapped_file_new (const gchar *filename,
|
|
||||||
gboolean writable,
|
|
||||||
GError **error)
|
|
||||||
{
|
{
|
||||||
GMappedFile *file;
|
GMappedFile *file;
|
||||||
int fd;
|
|
||||||
struct stat st;
|
struct stat st;
|
||||||
|
|
||||||
g_return_val_if_fail (filename != NULL, NULL);
|
|
||||||
g_return_val_if_fail (!error || *error == NULL, NULL);
|
|
||||||
|
|
||||||
fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
|
|
||||||
if (fd == -1)
|
|
||||||
{
|
|
||||||
int save_errno = errno;
|
|
||||||
gchar *display_filename = g_filename_display_name (filename);
|
|
||||||
|
|
||||||
g_set_error (error,
|
|
||||||
G_FILE_ERROR,
|
|
||||||
g_file_error_from_errno (save_errno),
|
|
||||||
_("Failed to open file '%s': open() failed: %s"),
|
|
||||||
display_filename,
|
|
||||||
g_strerror (save_errno));
|
|
||||||
g_free (display_filename);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
|
|
||||||
file = g_slice_new0 (GMappedFile);
|
file = g_slice_new0 (GMappedFile);
|
||||||
file->ref_count = 1;
|
file->ref_count = 1;
|
||||||
file->free_func = g_mapped_file_destroy;
|
file->free_func = g_mapped_file_destroy;
|
||||||
@ -166,13 +119,16 @@ g_mapped_file_new (const gchar *filename,
|
|||||||
if (fstat (fd, &st) == -1)
|
if (fstat (fd, &st) == -1)
|
||||||
{
|
{
|
||||||
int save_errno = errno;
|
int save_errno = errno;
|
||||||
gchar *display_filename = g_filename_display_name (filename);
|
gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
|
||||||
|
|
||||||
g_set_error (error,
|
g_set_error (error,
|
||||||
G_FILE_ERROR,
|
G_FILE_ERROR,
|
||||||
g_file_error_from_errno (save_errno),
|
g_file_error_from_errno (save_errno),
|
||||||
_("Failed to get attributes of file '%s': fstat() failed: %s"),
|
_("Failed to get attributes of file '%s%s%s%s': fstat() failed: %s"),
|
||||||
display_filename,
|
display_filename ? display_filename : "fd",
|
||||||
|
display_filename ? "' " : "",
|
||||||
|
display_filename ? display_filename : "",
|
||||||
|
display_filename ? "'" : "",
|
||||||
g_strerror (save_errno));
|
g_strerror (save_errno));
|
||||||
g_free (display_filename);
|
g_free (display_filename);
|
||||||
goto out;
|
goto out;
|
||||||
@ -186,7 +142,6 @@ g_mapped_file_new (const gchar *filename,
|
|||||||
{
|
{
|
||||||
file->length = 0;
|
file->length = 0;
|
||||||
file->contents = NULL;
|
file->contents = NULL;
|
||||||
close (fd);
|
|
||||||
return file;
|
return file;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -230,28 +185,123 @@ g_mapped_file_new (const gchar *filename,
|
|||||||
if (file->contents == MAP_FAILED)
|
if (file->contents == MAP_FAILED)
|
||||||
{
|
{
|
||||||
int save_errno = errno;
|
int save_errno = errno;
|
||||||
gchar *display_filename = g_filename_display_name (filename);
|
gchar *display_filename = filename ? g_filename_display_name (filename) : NULL;
|
||||||
|
|
||||||
g_set_error (error,
|
g_set_error (error,
|
||||||
G_FILE_ERROR,
|
G_FILE_ERROR,
|
||||||
g_file_error_from_errno (save_errno),
|
g_file_error_from_errno (save_errno),
|
||||||
_("Failed to map file '%s': mmap() failed: %s"),
|
_("Failed to map %s%s%s%s: mmap() failed: %s"),
|
||||||
display_filename,
|
display_filename ? display_filename : "fd",
|
||||||
|
display_filename ? "' " : "",
|
||||||
|
display_filename ? display_filename : "",
|
||||||
|
display_filename ? "'" : "",
|
||||||
g_strerror (save_errno));
|
g_strerror (save_errno));
|
||||||
g_free (display_filename);
|
g_free (display_filename);
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
close (fd);
|
|
||||||
return file;
|
return file;
|
||||||
|
|
||||||
out:
|
out:
|
||||||
close (fd);
|
|
||||||
g_slice_free (GMappedFile, file);
|
g_slice_free (GMappedFile, file);
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_mapped_file_new:
|
||||||
|
* @filename: The path of the file to load, in the GLib filename encoding
|
||||||
|
* @writable: whether the mapping should be writable
|
||||||
|
* @error: return location for a #GError, or %NULL
|
||||||
|
*
|
||||||
|
* Maps a file into memory. On UNIX, this is using the mmap() function.
|
||||||
|
*
|
||||||
|
* If @writable is %TRUE, the mapped buffer may be modified, otherwise
|
||||||
|
* it is an error to modify the mapped buffer. Modifications to the buffer
|
||||||
|
* are not visible to other processes mapping the same file, and are not
|
||||||
|
* written back to the file.
|
||||||
|
*
|
||||||
|
* Note that modifications of the underlying file might affect the contents
|
||||||
|
* of the #GMappedFile. Therefore, mapping should only be used if the file
|
||||||
|
* will not be modified, or if all modifications of the file are done
|
||||||
|
* atomically (e.g. using g_file_set_contents()).
|
||||||
|
*
|
||||||
|
* If @filename is the name of an empty, regular file, the function
|
||||||
|
* will successfully return an empty #GMappedFile. In other cases of
|
||||||
|
* size 0 (e.g. device files such as /dev/null), @error will be set
|
||||||
|
* to the #GFileError value #G_FILE_ERROR_INVAL.
|
||||||
|
*
|
||||||
|
* Return value: a newly allocated #GMappedFile which must be unref'd
|
||||||
|
* with g_mapped_file_unref(), or %NULL if the mapping failed.
|
||||||
|
*
|
||||||
|
* Since: 2.8
|
||||||
|
*/
|
||||||
|
GMappedFile *
|
||||||
|
g_mapped_file_new (const gchar *filename,
|
||||||
|
gboolean writable,
|
||||||
|
GError **error)
|
||||||
|
{
|
||||||
|
GMappedFile *file;
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
g_return_val_if_fail (filename != NULL, NULL);
|
||||||
|
g_return_val_if_fail (!error || *error == NULL, NULL);
|
||||||
|
|
||||||
|
fd = g_open (filename, (writable ? O_RDWR : O_RDONLY) | _O_BINARY, 0);
|
||||||
|
if (fd == -1)
|
||||||
|
{
|
||||||
|
int save_errno = errno;
|
||||||
|
gchar *display_filename = g_filename_display_name (filename);
|
||||||
|
|
||||||
|
g_set_error (error,
|
||||||
|
G_FILE_ERROR,
|
||||||
|
g_file_error_from_errno (save_errno),
|
||||||
|
_("Failed to open file '%s': open() failed: %s"),
|
||||||
|
display_filename,
|
||||||
|
g_strerror (save_errno));
|
||||||
|
g_free (display_filename);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
file = mapped_file_new_from_fd (fd, writable, filename, error);
|
||||||
|
|
||||||
|
close (fd);
|
||||||
|
|
||||||
|
return file;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* g_mapped_file_new_from_fd:
|
||||||
|
* @fd: The file descriptor of the file to load
|
||||||
|
* @writable: whether the mapping should be writable
|
||||||
|
* @error: return location for a #GError, or %NULL
|
||||||
|
*
|
||||||
|
* Maps a file into memory. On UNIX, this is using the mmap() function.
|
||||||
|
*
|
||||||
|
* If @writable is %TRUE, the mapped buffer may be modified, otherwise
|
||||||
|
* it is an error to modify the mapped buffer. Modifications to the buffer
|
||||||
|
* are not visible to other processes mapping the same file, and are not
|
||||||
|
* written back to the file.
|
||||||
|
*
|
||||||
|
* Note that modifications of the underlying file might affect the contents
|
||||||
|
* of the #GMappedFile. Therefore, mapping should only be used if the file
|
||||||
|
* will not be modified, or if all modifications of the file are done
|
||||||
|
* atomically (e.g. using g_file_set_contents()).
|
||||||
|
*
|
||||||
|
* Return value: a newly allocated #GMappedFile which must be unref'd
|
||||||
|
* with g_mapped_file_unref(), or %NULL if the mapping failed.
|
||||||
|
*
|
||||||
|
* Since: 2.30
|
||||||
|
*/
|
||||||
|
GMappedFile *
|
||||||
|
g_mapped_file_new_from_fd (gint fd,
|
||||||
|
gboolean writable,
|
||||||
|
GError **error)
|
||||||
|
{
|
||||||
|
return mapped_file_new_from_fd (fd, writable, NULL, error);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* g_mapped_file_get_length:
|
* g_mapped_file_get_length:
|
||||||
* @file: a #GMappedFile
|
* @file: a #GMappedFile
|
||||||
|
@ -35,6 +35,9 @@ typedef struct _GMappedFile GMappedFile;
|
|||||||
GMappedFile *g_mapped_file_new (const gchar *filename,
|
GMappedFile *g_mapped_file_new (const gchar *filename,
|
||||||
gboolean writable,
|
gboolean writable,
|
||||||
GError **error) G_GNUC_MALLOC;
|
GError **error) G_GNUC_MALLOC;
|
||||||
|
GMappedFile *g_mapped_file_new_from_fd (gint fd,
|
||||||
|
gboolean writable,
|
||||||
|
GError **error) G_GNUC_MALLOC;
|
||||||
gsize g_mapped_file_get_length (GMappedFile *file);
|
gsize g_mapped_file_get_length (GMappedFile *file);
|
||||||
gchar *g_mapped_file_get_contents (GMappedFile *file);
|
gchar *g_mapped_file_get_contents (GMappedFile *file);
|
||||||
GMappedFile *g_mapped_file_ref (GMappedFile *file);
|
GMappedFile *g_mapped_file_ref (GMappedFile *file);
|
||||||
|
@ -4,6 +4,9 @@
|
|||||||
#ifdef HAVE_UNISTD_H
|
#ifdef HAVE_UNISTD_H
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#endif
|
#endif
|
||||||
|
#include <sys/stat.h>
|
||||||
|
#include <sys/types.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
static void
|
static void
|
||||||
test_basic (void)
|
test_basic (void)
|
||||||
@ -98,6 +101,50 @@ test_writable (void)
|
|||||||
g_mapped_file_free (file);
|
g_mapped_file_free (file);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void
|
||||||
|
test_writable_fd (void)
|
||||||
|
{
|
||||||
|
GMappedFile *file;
|
||||||
|
GError *error;
|
||||||
|
gchar *contents;
|
||||||
|
const gchar *old = "MMMMMMMMMMMMMMMMMMMMMMMMM";
|
||||||
|
const gchar *new = "abcdefghijklmnopqrstuvxyz";
|
||||||
|
int fd;
|
||||||
|
|
||||||
|
if (access (SRCDIR "/4096-random-bytes", W_OK) != 0)
|
||||||
|
{
|
||||||
|
g_test_message ("Skipping writable mapping test");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
error = NULL;
|
||||||
|
fd = open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
|
||||||
|
g_assert (fd != -1);
|
||||||
|
file = g_mapped_file_new_from_fd (fd, TRUE, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
|
||||||
|
contents = g_mapped_file_get_contents (file);
|
||||||
|
g_assert (strncmp (contents, old, strlen (old)) == 0);
|
||||||
|
|
||||||
|
memcpy (contents, new, strlen (new));
|
||||||
|
g_assert (strncmp (contents, new, strlen (new)) == 0);
|
||||||
|
|
||||||
|
g_mapped_file_free (file);
|
||||||
|
close (fd);
|
||||||
|
|
||||||
|
error = NULL;
|
||||||
|
fd = open (SRCDIR "/4096-random-bytes", O_RDWR, 0);
|
||||||
|
g_assert (fd != -1);
|
||||||
|
file = g_mapped_file_new_from_fd (fd, TRUE, &error);
|
||||||
|
g_assert_no_error (error);
|
||||||
|
|
||||||
|
contents = g_mapped_file_get_contents (file);
|
||||||
|
g_assert (strncmp (contents, old, strlen (old)) == 0);
|
||||||
|
|
||||||
|
g_mapped_file_free (file);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
main (int argc, char *argv[])
|
main (int argc, char *argv[])
|
||||||
{
|
{
|
||||||
@ -108,6 +155,7 @@ main (int argc, char *argv[])
|
|||||||
g_test_add_func ("/mappedfile/device", test_device);
|
g_test_add_func ("/mappedfile/device", test_device);
|
||||||
g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
|
g_test_add_func ("/mappedfile/nonexisting", test_nonexisting);
|
||||||
g_test_add_func ("/mappedfile/writable", test_writable);
|
g_test_add_func ("/mappedfile/writable", test_writable);
|
||||||
|
g_test_add_func ("/mappedfile/writable_fd", test_writable_fd);
|
||||||
|
|
||||||
return g_test_run ();
|
return g_test_run ();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user