2 * fontconfig/test/test-migration.c
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2013 Akira TAGOH
7 * Permission to use, copy, modify, distribute, and sell this software and its
8 * documentation for any purpose is hereby granted without fee, provided that
9 * the above copyright notice appear in all copies and that both that
10 * copyright notice and this permission notice appear in supporting
11 * documentation, and that the name of the author(s) not be used in
12 * advertising or publicity pertaining to distribution of the software without
13 * specific, written prior permission. The authors make no
14 * representations about the suitability of this software for any purpose. It
15 * is provided "as is" without express or implied warranty.
17 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
18 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
19 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
20 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
21 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
22 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
23 * PERFORMANCE OF THIS SOFTWARE.
29 #include <sys/types.h>
31 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
32 #include <sys/types.h>
36 #include <fontconfig/fontconfig.h>
39 #define fc_mkdtemp mkdtemp
42 fc_mkdtemp (char *template)
44 if (!mktemp (template) || mkdir (template, 0700))
52 mkdir_p(const char *dir)
57 if (strlen (dir) == 0)
59 parent = (char *) FcStrDirname ((const FcChar8 *)dir);
62 if (access (parent, F_OK) == 0)
63 ret = mkdir (dir, 0755) == 0 && chmod (dir, 0755) == 0;
64 else if (access (parent, F_OK) == -1)
65 ret = mkdir_p (parent) && (mkdir (dir, 0755) == 0) && chmod (dir, 0755) == 0;
74 unlink_dirs(const char *dir)
76 DIR *d = opendir (dir);
78 size_t len = strlen (dir);
81 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
87 while ((e = readdir(d)) != NULL)
91 if (strcmp (e->d_name, ".") == 0 ||
92 strcmp (e->d_name, "..") == 0)
94 l = strlen (e->d_name) + 1;
97 n = malloc (l + len + 1);
100 strcpy (&n[len + 1], e->d_name);
101 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
102 if (e->d_type == DT_DIR)
104 if (stat (n, &statb) == -1)
106 fprintf (stderr, "E: %s\n", n);
110 if (S_ISDIR (statb.st_mode))
113 if (!unlink_dirs (n))
115 fprintf (stderr, "E: %s\n", n);
122 if (unlink (n) == -1)
124 fprintf (stderr, "E: %s\n", n);
134 if (rmdir (dir) == -1)
136 fprintf (stderr, "E: %s\n", dir);
146 char template[32] = "fontconfig-XXXXXXXX";
147 char *tmp = fc_mkdtemp (template);
148 size_t len = strlen (tmp), xlen, dlen;
149 char xdg[256], confd[256], fn[256], nfn[256], ud[256], nud[256];
152 char *content = "<fontconfig></fontconfig>";
155 strcpy (&xdg[len], "/.config");
156 setenv ("HOME", tmp, 1);
157 setenv ("XDG_CONFIG_HOME", xdg, 1);
160 strcpy (&confd[xlen], "/fontconfig");
161 dlen = strlen (confd);
162 /* In case there are no configuration files nor directory */
164 if (access (confd, F_OK) == 0)
166 fprintf (stderr, "%s unexpectedly exists\n", confd);
170 if (!unlink_dirs (tmp))
172 fprintf (stderr, "Unable to clean up\n");
175 /* In case there are the user configuration file */
177 strcpy (&fn[len], "/.fonts.conf");
179 strcpy (&nfn[dlen], "/fonts.conf");
180 if (!mkdir_p (confd))
182 fprintf (stderr, "Unable to create a config dir: %s\n", confd);
185 if ((fp = fopen (fn, "wb")) == NULL)
187 fprintf (stderr, "Unable to create a config file: %s\n", fn);
190 fwrite (content, sizeof (char), strlen (content), fp);
193 if (access (nfn, F_OK) != 0)
195 fprintf (stderr, "migration failed for %s\n", nfn);
199 if (!unlink_dirs (tmp))
201 fprintf (stderr, "Unable to clean up\n");
204 /* In case there are the user configuration dir */
206 strcpy (&ud[len], "/.fonts.conf.d");
208 strcpy (&nud[dlen], "/conf.d");
211 fprintf (stderr, "Unable to create a config dir: %s\n", ud);
215 if (access (nud, F_OK) != 0)
217 fprintf (stderr, "migration failed for %s\n", nud);