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.
32 #include <sys/types.h>
34 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
35 #include <sys/types.h>
39 #include <fontconfig/fontconfig.h>
42 #define fc_mkdtemp mkdtemp
45 fc_mkdtemp (char *template)
47 if (!mktemp (template) || mkdir (template, 0700))
55 mkdir_p(const char *dir)
60 if (strlen (dir) == 0)
62 parent = (char *) FcStrDirname ((const FcChar8 *)dir);
65 if (access (parent, F_OK) == 0)
66 ret = mkdir (dir, 0755) == 0 && chmod (dir, 0755) == 0;
67 else if (access (parent, F_OK) == -1)
68 ret = mkdir_p (parent) && (mkdir (dir, 0755) == 0) && chmod (dir, 0755) == 0;
77 unlink_dirs(const char *dir)
79 DIR *d = opendir (dir);
81 size_t len = strlen (dir);
84 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
90 while ((e = readdir(d)) != NULL)
94 if (strcmp (e->d_name, ".") == 0 ||
95 strcmp (e->d_name, "..") == 0)
97 l = strlen (e->d_name) + 1;
100 n = malloc (l + len + 1);
103 strcpy (&n[len + 1], e->d_name);
104 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
105 if (e->d_type == DT_DIR)
107 if (stat (n, &statb) == -1)
109 fprintf (stderr, "E: %s\n", n);
113 if (S_ISDIR (statb.st_mode))
116 if (!unlink_dirs (n))
118 fprintf (stderr, "E: %s\n", n);
125 if (unlink (n) == -1)
127 fprintf (stderr, "E: %s\n", n);
137 if (rmdir (dir) == -1)
139 fprintf (stderr, "E: %s\n", dir);
149 char template[32] = "fontconfig-XXXXXXXX";
150 char *tmp = fc_mkdtemp (template);
151 size_t len = strlen (tmp), xlen, dlen;
152 char xdg[256], confd[256], fn[256], nfn[256], ud[256], nud[256];
155 char *content = "<fontconfig></fontconfig>";
158 strcpy (&xdg[len], "/.config");
159 setenv ("HOME", tmp, 1);
160 setenv ("XDG_CONFIG_HOME", xdg, 1);
163 strcpy (&confd[xlen], "/fontconfig");
164 dlen = strlen (confd);
165 /* In case there are no configuration files nor directory */
167 if (access (confd, F_OK) == 0)
169 fprintf (stderr, "%s unexpectedly exists\n", confd);
173 if (!unlink_dirs (tmp))
175 fprintf (stderr, "Unable to clean up\n");
178 /* In case there are the user configuration file */
180 strcpy (&fn[len], "/.fonts.conf");
182 strcpy (&nfn[dlen], "/fonts.conf");
183 if (!mkdir_p (confd))
185 fprintf (stderr, "Unable to create a config dir: %s\n", confd);
188 if ((fp = fopen (fn, "wb")) == NULL)
190 fprintf (stderr, "Unable to create a config file: %s\n", fn);
193 fwrite (content, sizeof (char), strlen (content), fp);
196 if (access (nfn, F_OK) != 0)
198 fprintf (stderr, "migration failed for %s\n", nfn);
202 if (!unlink_dirs (tmp))
204 fprintf (stderr, "Unable to clean up\n");
207 /* In case there are the user configuration dir */
209 strcpy (&ud[len], "/.fonts.conf.d");
211 strcpy (&nud[dlen], "/conf.d");
214 fprintf (stderr, "Unable to create a config dir: %s\n", ud);
218 if (access (nud, F_OK) != 0)
220 fprintf (stderr, "migration failed for %s\n", nud);