|
Packit |
352660 |
/*
|
|
Packit |
352660 |
* fontconfig/test/test-migration.c
|
|
Packit |
352660 |
*
|
|
Packit |
352660 |
* Copyright © 2000 Keith Packard
|
|
Packit |
352660 |
* Copyright © 2013 Akira TAGOH
|
|
Packit |
352660 |
*
|
|
Packit |
352660 |
* Permission to use, copy, modify, distribute, and sell this software and its
|
|
Packit |
352660 |
* documentation for any purpose is hereby granted without fee, provided that
|
|
Packit |
352660 |
* the above copyright notice appear in all copies and that both that
|
|
Packit |
352660 |
* copyright notice and this permission notice appear in supporting
|
|
Packit |
352660 |
* documentation, and that the name of the author(s) not be used in
|
|
Packit |
352660 |
* advertising or publicity pertaining to distribution of the software without
|
|
Packit |
352660 |
* specific, written prior permission. The authors make no
|
|
Packit |
352660 |
* representations about the suitability of this software for any purpose. It
|
|
Packit |
352660 |
* is provided "as is" without express or implied warranty.
|
|
Packit |
352660 |
*
|
|
Packit |
352660 |
* THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
|
|
Packit |
352660 |
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
|
|
Packit |
352660 |
* EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
|
|
Packit |
352660 |
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
|
|
Packit |
352660 |
* DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
|
|
Packit |
352660 |
* TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
Packit |
352660 |
* PERFORMANCE OF THIS SOFTWARE.
|
|
Packit |
352660 |
*/
|
|
Packit |
352660 |
#ifdef HAVE_CONFIG_H
|
|
Packit |
352660 |
#include "config.h"
|
|
Packit |
352660 |
#endif
|
|
Packit |
352660 |
#include <stdio.h>
|
|
Packit |
352660 |
#include <stdlib.h>
|
|
Packit |
352660 |
#include <string.h>
|
|
Packit |
352660 |
#include <unistd.h>
|
|
Packit |
352660 |
#include <sys/types.h>
|
|
Packit |
352660 |
#include <dirent.h>
|
|
Packit |
352660 |
#ifndef HAVE_STRUCT_DIRENT_D_TYPE
|
|
Packit |
352660 |
#include <sys/types.h>
|
|
Packit |
352660 |
#include <sys/stat.h>
|
|
Packit |
352660 |
#include <unistd.h>
|
|
Packit |
352660 |
#endif
|
|
Packit |
352660 |
#include <fontconfig/fontconfig.h>
|
|
Packit |
352660 |
|
|
Packit |
352660 |
#ifdef HAVE_MKDTEMP
|
|
Packit |
352660 |
#define fc_mkdtemp mkdtemp
|
|
Packit |
352660 |
#else
|
|
Packit |
352660 |
char *
|
|
Packit |
352660 |
fc_mkdtemp (char *template)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
if (!mktemp (template) || mkdir (template, 0700))
|
|
Packit |
352660 |
return NULL;
|
|
Packit |
352660 |
|
|
Packit |
352660 |
return template;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
#endif
|
|
Packit |
352660 |
|
|
Packit |
352660 |
FcBool
|
|
Packit |
352660 |
mkdir_p(const char *dir)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
char *parent;
|
|
Packit |
352660 |
FcBool ret;
|
|
Packit |
352660 |
|
|
Packit |
352660 |
if (strlen (dir) == 0)
|
|
Packit |
352660 |
return FcFalse;
|
|
Packit |
352660 |
parent = (char *) FcStrDirname ((const FcChar8 *)dir);
|
|
Packit |
352660 |
if (!parent)
|
|
Packit |
352660 |
return FcFalse;
|
|
Packit |
352660 |
if (access (parent, F_OK) == 0)
|
|
Packit |
352660 |
ret = mkdir (dir, 0755) == 0 && chmod (dir, 0755) == 0;
|
|
Packit |
352660 |
else if (access (parent, F_OK) == -1)
|
|
Packit |
352660 |
ret = mkdir_p (parent) && (mkdir (dir, 0755) == 0) && chmod (dir, 0755) == 0;
|
|
Packit |
352660 |
else
|
|
Packit |
352660 |
ret = FcFalse;
|
|
Packit |
352660 |
free (parent);
|
|
Packit |
352660 |
|
|
Packit |
352660 |
return ret;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
|
|
Packit |
352660 |
FcBool
|
|
Packit |
352660 |
unlink_dirs(const char *dir)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
DIR *d = opendir (dir);
|
|
Packit |
352660 |
struct dirent *e;
|
|
Packit |
352660 |
size_t len = strlen (dir);
|
|
Packit |
352660 |
char *n = NULL;
|
|
Packit |
352660 |
FcBool ret = FcTrue;
|
|
Packit |
352660 |
#ifndef HAVE_STRUCT_DIRENT_D_TYPE
|
|
Packit |
352660 |
struct stat statb;
|
|
Packit |
352660 |
#endif
|
|
Packit |
352660 |
|
|
Packit |
352660 |
if (!d)
|
|
Packit |
352660 |
return FcFalse;
|
|
Packit |
352660 |
while ((e = readdir(d)) != NULL)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
size_t l;
|
|
Packit |
352660 |
|
|
Packit |
352660 |
if (strcmp (e->d_name, ".") == 0 ||
|
|
Packit |
352660 |
strcmp (e->d_name, "..") == 0)
|
|
Packit |
352660 |
continue;
|
|
Packit |
352660 |
l = strlen (e->d_name) + 1;
|
|
Packit |
352660 |
if (n)
|
|
Packit |
352660 |
free (n);
|
|
Packit |
352660 |
n = malloc (l + len + 1);
|
|
Packit |
352660 |
strcpy (n, dir);
|
|
Packit |
352660 |
n[len] = '/';
|
|
Packit |
352660 |
strcpy (&n[len + 1], e->d_name);
|
|
Packit |
352660 |
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
|
|
Packit |
352660 |
if (e->d_type == DT_DIR)
|
|
Packit |
352660 |
#else
|
|
Packit |
352660 |
if (stat (n, &statb) == -1)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "E: %s\n", n);
|
|
Packit |
352660 |
ret = FcFalse;
|
|
Packit |
352660 |
break;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
if (S_ISDIR (statb.st_mode))
|
|
Packit |
352660 |
#endif
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
if (!unlink_dirs (n))
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "E: %s\n", n);
|
|
Packit |
352660 |
ret = FcFalse;
|
|
Packit |
352660 |
break;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
else
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
if (unlink (n) == -1)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "E: %s\n", n);
|
|
Packit |
352660 |
ret = FcFalse;
|
|
Packit |
352660 |
break;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
if (n)
|
|
Packit |
352660 |
free (n);
|
|
Packit |
352660 |
closedir (d);
|
|
Packit |
352660 |
|
|
Packit |
352660 |
if (rmdir (dir) == -1)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "E: %s\n", dir);
|
|
Packit |
352660 |
return FcFalse;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
|
|
Packit |
352660 |
return ret;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
|
|
Packit |
352660 |
int
|
|
Packit |
352660 |
main(void)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
char template[32] = "fontconfig-XXXXXXXX";
|
|
Packit |
352660 |
char *tmp = fc_mkdtemp (template);
|
|
Packit |
352660 |
size_t len = strlen (tmp), xlen, dlen;
|
|
Packit |
352660 |
char xdg[256], confd[256], fn[256], nfn[256], ud[256], nud[256];
|
|
Packit |
352660 |
int ret = -1;
|
|
Packit |
352660 |
FILE *fp;
|
|
Packit |
352660 |
char *content = "<fontconfig></fontconfig>";
|
|
Packit |
352660 |
|
|
Packit |
352660 |
strcpy (xdg, tmp);
|
|
Packit |
352660 |
strcpy (&xdg[len], "/.config");
|
|
Packit |
352660 |
setenv ("HOME", tmp, 1);
|
|
Packit |
352660 |
setenv ("XDG_CONFIG_HOME", xdg, 1);
|
|
Packit |
352660 |
xlen = strlen (xdg);
|
|
Packit |
352660 |
strcpy (confd, xdg);
|
|
Packit |
352660 |
strcpy (&confd[xlen], "/fontconfig");
|
|
Packit |
352660 |
dlen = strlen (confd);
|
|
Packit |
352660 |
/* In case there are no configuration files nor directory */
|
|
Packit |
352660 |
FcInit ();
|
|
Packit |
352660 |
if (access (confd, F_OK) == 0)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "%s unexpectedly exists\n", confd);
|
|
Packit |
352660 |
goto bail;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
FcFini ();
|
|
Packit |
352660 |
if (!unlink_dirs (tmp))
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "Unable to clean up\n");
|
|
Packit |
352660 |
goto bail;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
/* In case there are the user configuration file */
|
|
Packit |
352660 |
strcpy (fn, tmp);
|
|
Packit |
352660 |
strcpy (&fn[len], "/.fonts.conf");
|
|
Packit |
352660 |
strcpy (nfn, confd);
|
|
Packit |
352660 |
strcpy (&nfn[dlen], "/fonts.conf");
|
|
Packit |
352660 |
if (!mkdir_p (confd))
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "Unable to create a config dir: %s\n", confd);
|
|
Packit |
352660 |
goto bail;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
if ((fp = fopen (fn, "wb")) == NULL)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "Unable to create a config file: %s\n", fn);
|
|
Packit |
352660 |
goto bail;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
fwrite (content, sizeof (char), strlen (content), fp);
|
|
Packit |
352660 |
fclose (fp);
|
|
Packit |
352660 |
FcInit ();
|
|
Packit |
352660 |
if (access (nfn, F_OK) != 0)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "migration failed for %s\n", nfn);
|
|
Packit |
352660 |
goto bail;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
FcFini ();
|
|
Packit |
352660 |
if (!unlink_dirs (tmp))
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "Unable to clean up\n");
|
|
Packit |
352660 |
goto bail;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
/* In case there are the user configuration dir */
|
|
Packit |
352660 |
strcpy (ud, tmp);
|
|
Packit |
352660 |
strcpy (&ud[len], "/.fonts.conf.d");
|
|
Packit |
352660 |
strcpy (nud, confd);
|
|
Packit |
352660 |
strcpy (&nud[dlen], "/conf.d");
|
|
Packit |
352660 |
if (!mkdir_p (ud))
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "Unable to create a config dir: %s\n", ud);
|
|
Packit |
352660 |
goto bail;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
FcInit ();
|
|
Packit |
352660 |
if (access (nud, F_OK) != 0)
|
|
Packit |
352660 |
{
|
|
Packit |
352660 |
fprintf (stderr, "migration failed for %s\n", nud);
|
|
Packit |
352660 |
goto bail;
|
|
Packit |
352660 |
}
|
|
Packit |
352660 |
FcFini ();
|
|
Packit |
352660 |
|
|
Packit |
352660 |
ret = 0;
|
|
Packit |
352660 |
bail:
|
|
Packit |
352660 |
unlink_dirs (tmp);
|
|
Packit |
352660 |
|
|
Packit |
352660 |
return ret;
|
|
Packit |
352660 |
}
|