Commit cd68aa0a authored by David Zeuthen's avatar David Zeuthen

split utility bits into a private statically linked library

getting closer...

$ grep glib *.c
polkit-authorization.c:#include <glib.h>
polkit-authorization-db.c:#include <glib.h>
polkit-authorization-db-dummy.c:#include <glib.h>
polkit-config.c:#include <glib.h>
polkit-context.c:#include <glib.h>
polkit-sysdeps.c:#include <glib.h>
parent 47e17031
......@@ -34,24 +34,19 @@ AC_ARG_ENABLE(tests, AS_HELP_STRING([--enable-tests],[enable unit test code]),en
AC_ARG_ENABLE(gcov, AS_HELP_STRING([--enable-gcov],[compile with coverage profiling instrumentation (gcc only)]),enable_gcov=$enableval,enable_gcov=no)
AC_ARG_ENABLE(man-pages, [ --enable-man-pages build manual pages],enable_man_pages=$enableval,enable_man_pages=yes)
AM_CONDITIONAL(KIT_BUILD_TESTS, test x$enable_tests = xyes)
AM_CONDITIONAL(POLKIT_BUILD_TESTS, test x$enable_tests = xyes)
if test x$enable_tests = xyes; then
AC_DEFINE(KIT_BUILD_TESTS,1,[Build test code])
AC_DEFINE(POLKIT_BUILD_TESTS,1,[Build test code])
fi
if test x$enable_gcov = xyes; then
## so that config.h changes when you toggle gcov support
AC_DEFINE_UNQUOTED(KIT_GCOV_ENABLED, 1, [Defined if gcov is enabled to force a rebuild due to config.h changing])
AC_DEFINE_UNQUOTED(POLKIT_GCOV_ENABLED, 1, [Defined if gcov is enabled to force a rebuild due to config.h changing])
AC_MSG_CHECKING([for gcc 3.3 version of gcov file format])
have_gcc33_gcov=no
AC_RUN_IFELSE( [AC_LANG_PROGRAM( , [[ if (__GNUC__ >=3 && __GNUC_MINOR__ >= 3) exit (0); else exit (1); ]])],
have_gcc33_gcov=yes)
if test x$have_gcc33_gcov = xyes ; then
AC_DEFINE_UNQUOTED(POLKIT_HAVE_GCC33_GCOV, 1, [Defined if we have gcc 3.3 and thus the new gcov format])
fi
AC_MSG_RESULT($have_gcc33_gcov)
fi
AM_CONDITIONAL(KIT_GCOV_ENABLED, test x$enable_gcov = xyes)
AM_CONDITIONAL(POLKIT_GCOV_ENABLED, test x$enable_gcov = xyes)
......@@ -454,6 +449,7 @@ data/polkit.pc
data/polkit-dbus.pc
data/polkit-grant.pc
src/Makefile
src/kit/Makefile
src/polkit/Makefile
src/polkit-dbus/Makefile
src/polkit-grant/Makefile
......
......@@ -19,8 +19,8 @@ SCAN_OPTIONS=--ignore-headers=config.h
DOC_SOURCE_DIR=../src
# Used for dependencies
HFILE_GLOB=$(top_srcdir)/src/polkit*/*.h
CFILE_GLOB=$(top_srcdir)/src/polkit*/*.c
HFILE_GLOB=$(top_srcdir)/src/*/*.h
CFILE_GLOB=$(top_srcdir)/src/*/*.c
# Headers to ignore
IGNORE_HFILES= \
......
......@@ -68,6 +68,24 @@
<xi:include href="spec/polkit-spec-configuration.xml" />
</reference>
<reference id="ref-internal">
<title>Internal API Reference</title>
<partintro>
<para>
This part presents the class and function reference for the
internal support classes, data structures and functions. These
symbols are not exported.
</para>
</partintro>
<xi:include href="xml/kit.xml"/>
<xi:include href="xml/kit-memory.xml"/>
<xi:include href="xml/kit-string.xml"/>
<xi:include href="xml/kit-message.xml"/>
<xi:include href="xml/kit-list.xml"/>
<xi:include href="xml/kit-hash.xml"/>
<xi:include href="xml/kit-file.xml"/>
</reference>
<reference id="ref-core">
<title>Core API Reference</title>
<partintro>
......@@ -84,9 +102,6 @@
</partintro>
<xi:include href="xml/polkit-types.xml"/>
<xi:include href="xml/polkit-sysdeps.xml"/>
<xi:include href="xml/polkit-memory.xml"/>
<xi:include href="xml/polkit-list.xml"/>
<xi:include href="xml/polkit-hash.xml"/>
<xi:include href="xml/polkit-error.xml"/>
<xi:include href="xml/polkit-result.xml"/>
<xi:include href="xml/polkit-action.xml"/>
......
SUBDIRS = polkit polkit-dbus polkit-grant
SUBDIRS = kit polkit polkit-dbus polkit-grant
clean-local :
rm -f *~
## Process this file with automake to produce Makefile.in
NULL =
INCLUDES = \
-I$(top_builddir)/src -I$(top_srcdir)/src \
-DPACKAGE_LIBEXEC_DIR=\""$(libexecdir)"\" \
-DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\" \
-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
-DPACKAGE_BIN_DIR=\""$(bindir)"\" \
-DPACKAGE_LOCALSTATE_DIR=\""$(localstatedir)"\" \
-DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \
-DPACKAGE_LIB_DIR=\""$(libdir)"\" \
-D_POSIX_PTHREAD_SEMANTICS -D_REENTRANT \
-DKIT_COMPILATION \
@GLIB_CFLAGS@
noinst_LTLIBRARIES=libkit.la
libkit_la_SOURCES = \
kit.h \
kit-memory.h kit-memory.c \
kit-string.h kit-string.c \
kit-list.h kit-list.c \
kit-hash.h kit-hash.c \
kit-file.h kit-file.c \
kit-message.h kit-message.c \
$(NULL)
## note that TESTS has special meaning (stuff to use in make check)
## so if adding tests not to be run in make check, don't add them to
## TESTS
if KIT_BUILD_TESTS
TESTS_ENVIRONMENT=
TESTS=kit-test
if KIT_GCOV_ENABLED
clean-gcov:
rm -f *.gcov .libs/*.gcda
.PHONY: coverage-report.txt
coverage-report.txt :
$(top_srcdir)/test/create-coverage-report.sh kit $(filter %.c,$(libkit_la_SOURCES)) > coverage-report.txt
check-coverage : clean-gcov all check coverage-report.txt
cat coverage-report.txt
else
coverage-report.txt:
@echo "Need to reconfigure with --enable-gcov"
check-coverage:
@echo "Need to reconfigure with --enable-gcov"
endif
else
TESTS=
endif
## we use noinst_PROGRAMS not check_PROGRAMS so that we build
## even when not doing "make check"
noinst_PROGRAMS=$(TESTS)
kit_test_SOURCES= \
kit-test.h kit-test.c
kit_test_LDADD=$(top_builddir)/src/kit/libkit.la
kit_test_LDFLAGS=
#@R_DYNAMIC_LDFLAG@
clean-local :
rm -f *~ *.bb *.bbg *.da *.gcov .libs/*.da .libs/*.bbg
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/***************************************************************************
*
* kit-file.c : File utilities
*
* Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
*
* Licensed under the Academic Free License version 2.1
*
* 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 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <kit/kit.h>
#include "kit-test.h"
/**
* SECTION:kit-file
* @title: File utilities
* @short_description: File utilities
*
* Various file utilities.
**/
#define BUF_SIZE 4096
/**
* kit_file_get_contents:
* @path: path to file
* @out_contents: Return location for allocated memory. Free with kit_free().
* @out_contents_size: Return location for size of the file.
*
* Reads an entire file into allocated memory.
*
* Returns: #TRUE if the file was read into memory; #FALSE if an error
* occured and errno will be set.
*/
kit_bool_t
kit_file_get_contents (const char *path, char **out_contents, size_t *out_contents_size)
{
int fd;
kit_bool_t ret;
ssize_t num_read;
char *p;
char *q;
size_t total_allocated;
size_t total_size;
char buf[BUF_SIZE];
kit_return_val_if_fail (path != NULL, FALSE);
kit_return_val_if_fail (out_contents != NULL, FALSE);
kit_return_val_if_fail (out_contents_size != NULL, FALSE);
fd = -1;
ret = FALSE;
*out_contents = NULL;
fd = open (path, O_RDONLY);
if (fd == 0)
goto out;
p = kit_malloc (BUF_SIZE);
if (p == NULL) {
errno = ENOMEM;
goto out;
}
total_allocated = BUF_SIZE;
total_size = 0;
do {
again:
num_read = read (fd, buf, BUF_SIZE);
if (num_read == -1) {
if (errno == EINTR)
goto again;
else
goto out;
}
if (total_size + num_read > total_allocated) {
total_allocated += BUF_SIZE;
q = kit_realloc (p, total_allocated);
if (q == NULL) {
errno = ENOMEM;
goto out;
}
p = q;
}
memcpy (p + total_size, buf, num_read);
total_size += num_read;
} while (num_read > 0);
/* add terminating zero */
if (total_size + 1 > total_allocated) {
total_allocated += BUF_SIZE;
q = kit_realloc (p, total_allocated);
if (q == NULL) {
errno = ENOMEM;
goto out;
}
p = q;
}
p[total_size] = '\0';
*out_contents = p;
*out_contents_size = total_size;
ret = TRUE;
out:
if (fd >= 0) {
again2:
if (close (fd) != 0) {
if (errno == EINTR)
goto again2;
else
ret = FALSE;
}
}
if (!ret) {
kit_free (p);
*out_contents = NULL;
}
return ret;
}
static kit_bool_t
_write_to_fd (int fd, const char *str, ssize_t str_len)
{
kit_bool_t ret;
ssize_t written;
ret = FALSE;
written = 0;
while (written < str_len) {
ssize_t ret;
ret = write (fd, str + written, str_len - written);
if (ret < 0) {
if (errno == EAGAIN || errno == EINTR) {
continue;
} else {
goto out;
}
}
written += ret;
}
ret = TRUE;
out:
return ret;
}
/**
* kit_file_set_contents:
* @path: path to file
* @mode: mode for file
* @contents: contents to set
* @contents_size: size of contents
*
* Writes all of contents to a file named @path, with good error
* checking. If a file called @path already exists it will be
* overwritten. This write is atomic in the sense that it is first
* written to a temporary file which is then renamed to the final
* name.
*
* If the file already exists hard links to @path will break. Also
* since the file is recreated, existing permissions, access control
* lists, metadata etc. may be lost. If @path is a symbolic link, the
* link itself will be replaced, not the linked file.
*
* Returns: #TRUE if contents were set; #FALSE if an error occured and
* errno will be set
*/
kit_bool_t
kit_file_set_contents (const char *path, mode_t mode, const char *contents, size_t contents_size)
{
int fd;
char *path_tmp;
kit_bool_t ret;
path_tmp = NULL;
ret = FALSE;
kit_return_val_if_fail ((contents == NULL && contents_size == 0) || (contents != NULL), FALSE);
kit_return_val_if_fail (path != NULL, FALSE);
path_tmp = kit_strdup_printf ("%s.XXXXXX", path);
if (path_tmp == NULL) {
errno = ENOMEM;
goto out;
}
fd = mkstemp (path_tmp);
if (fd < 0) {
kit_warning ("Cannot create file '%s': %m", path_tmp);
goto out;
}
if (fchmod (fd, mode) != 0) {
kit_warning ("Cannot change mode for '%s' to 0%o: %m", path_tmp, mode);
close (fd);
unlink (path_tmp);
goto out;
}
if (contents_size > 0) {
if (!_write_to_fd (fd, contents, contents_size)) {
kit_warning ("Cannot write to file %s: %m", path_tmp);
close (fd);
if (unlink (path_tmp) != 0) {
kit_warning ("Cannot unlink %s: %m", path_tmp);
}
goto out;
}
}
close (fd);
if (rename (path_tmp, path) != 0) {
kit_warning ("Cannot rename %s to %s: %m", path_tmp, path);
if (unlink (path_tmp) != 0) {
kit_warning ("Cannot unlink %s: %m", path_tmp);
}
goto out;
}
ret = TRUE;
out:
if (path_tmp != NULL)
kit_free (path_tmp);
return ret;
}
#ifdef KIT_BUILD_TESTS
static kit_bool_t
_run_test (void)
{
char path[] = "/tmp/kit-test";
char *buf;
size_t buf_size;
char *p;
size_t s;
unsigned int n;
buf_size = 3 * BUF_SIZE;
if ((buf = kit_malloc (buf_size)) == NULL)
goto out;
for (n = 0; n < buf_size; n++)
buf[n] = n;
if (!kit_file_set_contents (path, 0400, buf, buf_size)) {
kit_assert (errno == ENOMEM);
} else {
if (!kit_file_get_contents (path, &p, &s)) {
kit_assert (errno == ENOMEM);
} else {
kit_assert (s == buf_size && memcmp (p, buf, buf_size) == 0);
kit_free (p);
}
kit_assert (unlink (path) == 0);
kit_assert (!kit_file_get_contents (path, &p, &s));
}
kit_free (buf);
out:
return TRUE;
}
KitTest _test_file = {
"kit_file",
NULL,
NULL,
_run_test
};
#endif /* KIT_BUILD_TESTS */
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/***************************************************************************
*
* kit-file.h : File utilities
*
* Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
*
* Licensed under the Academic Free License version 2.1
*
* 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 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 St, Fifth Floor, Boston, MA 02110-1301 USA
*
**************************************************************************/
#if !defined (KIT_COMPILATION) && !defined(_KIT_INSIDE_KIT_H)
#error "Only <kit/kit.h> can be included directly, this file may disappear or change contents."
#endif
#ifndef KIT_FILE_H
#define KIT_FILE_H
#include <kit/kit.h>
KIT_BEGIN_DECLS
kit_bool_t kit_file_get_contents (const char *path, char **out_contents, size_t *out_contents_size);
kit_bool_t kit_file_set_contents (const char *path, mode_t mode, const char *contents, size_t contents_size);
KIT_END_DECLS
#endif /* KIT_FILE_H */
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */
/***************************************************************************
*
* polkit-hash.h : Hash tables
* kit-hash.h : Hash tables
*
* Copyright (C) 2007 David Zeuthen, <david@fubar.dk>
*
......@@ -23,42 +23,43 @@
*
**************************************************************************/
#if !defined (POLKIT_COMPILATION) && !defined(_POLKIT_INSIDE_POLKIT_H)
#error "Only <polkit/polkit.h> can be included directly, this file may disappear or change contents."
#if !defined (KIT_COMPILATION) && !defined(_KIT_INSIDE_KIT_H)
#error "Only <kit/kit.h> can be included directly, this file may disappear or change contents."
#endif
#ifndef POLKIT_HASH_H
#define POLKIT_HASH_H
#ifndef KIT_HASH_H
#define KIT_HASH_H
#include <polkit/polkit-types.h>
#include <stdint.h>
#include <kit/kit.h>
POLKIT_BEGIN_DECLS
KIT_BEGIN_DECLS
struct _PolKitHash;
typedef struct _PolKitHash PolKitHash;
struct _KitHash;
typedef struct _KitHash KitHash;
/**
* PolKitHashFunc:
* KitHashFunc:
* @key: a key
*
* The function is passed a key and should return a hash value. The
* functions polkit_hash_direct_hash_func() and
* polkit_hash_str_hash_func() provide hash functions which can be
* functions kit_hash_direct_hash_func() and
* kit_hash_str_hash_func() provide hash functions which can be
* used when the key is a pointer and an char* respectively.
*
* Returns: the hash value corresponding to the key
*
* Since: 0.7
*/
typedef polkit_uint32_t (*PolKitHashFunc) (const void *key);
typedef uint32_t (*KitHashFunc) (const void *key);
/**
* PolKitEqualFunc:
* KitEqualFunc:
* @key1: first key
* @key2: second key
*
* Determines if two keys are equal. The functions
* polkit_hash_direct_equal_func() and polkit_hash_str_equal_func()
* kit_hash_direct_equal_func() and kit_hash_str_equal_func()
* provide equality functions which can be used when the key is a
* pointer and an char* respectively.
*
......@@ -66,10 +67,10 @@ typedef polkit_uint32_t (*PolKitHashFunc) (const void *key);
*
* Since: 0.7
*/
typedef polkit_bool_t (*PolKitEqualFunc) (const void *key1, const void *key2);
typedef kit_bool_t (*KitEqualFunc) (const void *key1, const void *key2);
/**
* PolKitFreeFunc:
* KitFreeFunc:
* @p: pointer
*
* Specifies the type of function which is called when a data element
......@@ -79,69 +80,69 @@ typedef polkit_bool_t (*PolKitEqualFunc) (const void *key1, const void *key2);
*
* Since: 0.7
*/
typedef void (*PolKitFreeFunc) (void *p);
typedef void (*KitFreeFunc) (void *p);
/**
* PolKitCopyFunc:
* KitCopyFunc:
* @p: pointer
*
* Specifies the type of function which is called when a data element
* is to be cloned or reffed. It is passed the pointer to the data
* element and should return a new pointer to a reffed or cloned
* object. The function polkit_hash_str_copy() or any of the object
* object. The function kit_hash_str_copy() or any of the object
* ref functions can be passed here.
*
* Returns: A copy or ref of the object in question
*
* Since: 0.7
*/
typedef void *(*PolKitCopyFunc) (const void *p);
typedef void *(*KitCopyFunc) (const void *p);
/**
* PolKitHashForeachFunc:
* KitHashForeachFunc:
* @hash: the hash table
* @key: key
* @value: value
* @user_data: user data passed to polkit_hash_foreach()
* @user_data: user data passed to kit_hash_foreach()
*
* Type signature for callback function used in polkit_hash_foreach().
* Type signature for callback function used in kit_hash_foreach().
*
* Returns: Return #TRUE to short-circuit, e.g. stop the iteration.
*
* Since: 0.7
*/
typedef polkit_bool_t (*PolKitHashForeachFunc) (PolKitHash *hash,
void *key,
void *value,
void *user_data);
typedef kit_bool_t (*KitHashForeachFunc) (KitHash *hash,
void *key,
void *value,
void *user_data);
PolKitHash *polkit_hash_new (PolKitHashFunc hash_func,
PolKitEqualFunc key_equal_func,
PolKitCopyFunc key_copy_func,
PolKitCopyFunc value_copy_func,
PolKitFreeFunc key_destroy_func,
PolKitFreeFunc value_destroy_func);
KitHash *kit_hash_new (KitHashFunc hash_func,
KitEqualFunc key_equal_func,
KitCopyFunc key_copy_func,
KitCopyFunc value_copy_func,
KitFreeFunc key_destroy_func,
KitFreeFunc value_destroy_func);
PolKitHash *polkit_hash_ref (PolKitHash *hash);
void polkit_hash_unref (PolKitHash *hash);
KitHash *kit_hash_ref (KitHash *hash);
void kit_hash_unref (KitHash *hash);
polkit_bool_t polkit_hash_insert (PolKitHash *hash, void *key, void *value);
kit_bool_t kit_hash_insert (KitHash *hash, void *key, void *value);
void *polkit_hash_lookup (PolKitHash *hash, void *key, polkit_bool_t *found);
void *kit_hash_lookup (KitHash *hash, void *key, kit_bool_t *found);
polkit_bool_t polkit_hash_foreach (PolKitHash *hash, PolKitHashForeachFunc cb, void *user_data);
kit_bool_t kit_hash_foreach (KitHash *hash, KitHashForeachFunc cb, void *user_data);
polkit_uint32_t polkit_hash_direct_hash_func (const void *key);
polkit_bool_t polkit_hash_direct_equal_func (const void *v1, const void *v2);
uint32_t kit_hash_direct_hash_func (const void *key);
kit_bool_t kit_hash_direct_equal_func (const void *v1, const void *v2);
polkit_uint32_t polkit_hash_str_hash_func (const void *key);