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.
31 #include <sys/types.h>
36 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
37 #include <sys/types.h>
40 #include <fontconfig/fontconfig.h>
43 #define fc_mkdtemp mkdtemp
46 fc_mkdtemp (char *template)
48 if (!mktemp (template) || mkdir (template, 0700))
56 mkdir_p(const char *dir)
61 if (strlen (dir) == 0)
63 parent = (char *) FcStrDirname ((const FcChar8 *)dir);
66 if (access (parent, F_OK) == 0)
67 ret = mkdir (dir, 0755) == 0 && chmod (dir, 0755) == 0;
68 else if (access (parent, F_OK) == -1)
69 ret = mkdir_p (parent) && (mkdir (dir, 0755) == 0) && chmod (dir, 0755) == 0;
78 unlink_dirs(const char *dir)
80 DIR *d = opendir (dir);
82 size_t len = strlen (dir);
85 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
91 while ((e = readdir(d)) != NULL)
95 if (strcmp (e->d_name, ".") == 0 ||
96 strcmp (e->d_name, "..") == 0)
98 l = strlen (e->d_name) + 1;
101 n = malloc (l + len + 1);
104 strcpy (&n[len + 1], e->d_name);
105 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
106 if (e->d_type == DT_DIR)
108 if (stat (n, &statb) == -1)
110 fprintf (stderr, "E: %s\n", n);
114 if (S_ISDIR (statb.st_mode))
117 if (!unlink_dirs (n))
119 fprintf (stderr, "E: %s\n", n);
126 if (unlink (n) == -1)
128 fprintf (stderr, "E: %s\n", n);
138 if (rmdir (dir) == -1)
140 fprintf (stderr, "E: %s\n", dir);
150 char template[32] = "fontconfig-XXXXXXXX";
151 char *tmp = fc_mkdtemp (template);
152 size_t len = strlen (tmp), xlen, dlen;
153 char xdg[256], confd[256], fn[256], nfn[256], ud[256], nud[256];
156 char *content = "<fontconfig></fontconfig>";
159 strcpy (&xdg[len], "/.config");
160 setenv ("HOME", tmp, 1);
161 setenv ("XDG_CONFIG_HOME", xdg, 1);
164 strcpy (&confd[xlen], "/fontconfig");
165 dlen = strlen (confd);
166 /* In case there are no configuration files nor directory */
168 if (access (confd, F_OK) == 0)
170 fprintf (stderr, "%s unexpectedly exists\n", confd);
174 if (!unlink_dirs (tmp))
176 fprintf (stderr, "Unable to clean up\n");
179 /* In case there are the user configuration file */
181 strcpy (&fn[len], "/.fonts.conf");
183 strcpy (&nfn[dlen], "/fonts.conf");
184 if (!mkdir_p (confd))
186 fprintf (stderr, "Unable to create a config dir: %s\n", confd);
189 if ((fp = fopen (fn, "wb")) == NULL)
191 fprintf (stderr, "Unable to create a config file: %s\n", fn);
194 fwrite (content, sizeof (char), strlen (content), fp);
197 if (access (nfn, F_OK) != 0)
199 fprintf (stderr, "migration failed for %s\n", nfn);
203 if (!unlink_dirs (tmp))
205 fprintf (stderr, "Unable to clean up\n");
208 /* In case there are the user configuration dir */
210 strcpy (&ud[len], "/.fonts.conf.d");
212 strcpy (&nud[dlen], "/conf.d");
215 fprintf (stderr, "Unable to create a config dir: %s\n", ud);
219 if (access (nud, F_OK) != 0)
221 fprintf (stderr, "migration failed for %s\n", nud);