2 * fontconfig/test/test-bz89617.c
4 * Copyright © 2000 Keith Packard
5 * Copyright © 2018 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.
36 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
37 #include <sys/types.h>
40 #include <fontconfig/fontconfig.h>
43 # define FC_DIR_SEPARATOR '\\'
44 # define FC_DIR_SEPARATOR_S "\\"
46 # define FC_DIR_SEPARATOR '/'
47 # define FC_DIR_SEPARATOR_S "/"
52 #define mkdir(path,mode) _mkdir(path)
56 #define fc_mkdtemp mkdtemp
59 fc_mkdtemp (char *template)
61 if (!mktemp (template) || mkdir (template, 0700))
69 mkdir_p (const char *dir)
74 if (strlen (dir) == 0)
76 parent = (char *) FcStrDirname ((const FcChar8 *) dir);
79 if (access (parent, F_OK) == 0)
80 ret = mkdir (dir, 0755) == 0 && chmod (dir, 0755) == 0;
81 else if (access (parent, F_OK) == -1)
82 ret = mkdir_p (parent) && (mkdir (dir, 0755) == 0) && chmod (dir, 0755) == 0;
91 unlink_dirs (const char *dir)
93 DIR *d = opendir (dir);
95 size_t len = strlen (dir);
98 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
104 while ((e = readdir (d)) != NULL)
108 if (strcmp (e->d_name, ".") == 0 ||
109 strcmp (e->d_name, "..") == 0)
111 l = strlen (e->d_name) + 1;
114 n = malloc (l + len + 1);
121 n[len] = FC_DIR_SEPARATOR;
122 strcpy (&n[len + 1], e->d_name);
123 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
124 if (e->d_type == DT_DIR)
126 if (stat (n, &statb) == -1)
128 fprintf (stderr, "E: %s\n", n);
132 if (S_ISDIR (statb.st_mode))
135 if (!unlink_dirs (n))
137 fprintf (stderr, "E: %s\n", n);
144 if (unlink (n) == -1)
146 fprintf (stderr, "E: %s\n", n);
156 if (rmdir (dir) == -1)
158 fprintf (stderr, "E: %s\n", dir);
168 FcChar8 *fontdir = NULL, *cachedir = NULL;
169 char *basedir, template[512] = "/tmp/bz106632-XXXXXX";
172 const FcChar8 *tconf = (const FcChar8 *) "<fontconfig>\n"
174 " <cachedir>%s</cachedir>\n"
181 fprintf (stderr, "D: Creating tmp dir\n");
182 basedir = fc_mkdtemp (template);
185 fprintf (stderr, "%s: %s\n", template, strerror (errno));
188 fontdir = FcStrBuildFilename ((const FcChar8 *) basedir, (const FcChar8 *) "fonts", NULL);
189 cachedir = FcStrBuildFilename ((const FcChar8 *) basedir, (const FcChar8 *) "cache", NULL);
190 fprintf (stderr, "D: Creating %s\n", fontdir);
191 mkdir_p ((const char *) fontdir);
192 fprintf (stderr, "D: Creating %s\n", cachedir);
193 mkdir_p ((const char *) cachedir);
195 fprintf (stderr, "D: Copying %s to %s\n", FONTFILE, fontdir);
196 snprintf (cmd, 512, "sleep 1; cp -a %s %s; sleep 1", FONTFILE, fontdir);
199 fprintf (stderr, "D: Loading a config\n");
200 snprintf (conf, 1024, (const char *) tconf, fontdir, cachedir);
201 config = FcConfigCreate ();
202 if (!FcConfigParseAndLoadFromMemory (config, (const FcChar8 *) conf, FcTrue))
204 printf ("E: Unable to load config\n");
208 if (!FcConfigBuildFonts (config))
210 printf ("E: unable to build fonts\n");
214 fprintf (stderr, "D: Obtaining fonts information\n");
215 pat = FcPatternCreate ();
216 fs = FcFontList (config, pat, NULL);
217 FcPatternDestroy (pat);
218 if (!fs || fs->nfont != 1)
220 printf ("E: Unexpected the number of fonts: %d\n", !fs ? -1 : fs->nfont);
224 FcFontSetDestroy (fs);
225 fprintf (stderr, "D: Removing %s\n", fontdir);
226 snprintf (cmd, 512, "sleep 1; rm -f %s%s*; sleep 1", fontdir, FC_DIR_SEPARATOR_S);
228 fprintf (stderr, "D: Reinitializing\n");
229 if (FcConfigUptoDate(config))
231 fprintf (stderr, "E: Config reports up-to-date\n");
235 if (!FcInitReinitialize ())
237 fprintf (stderr, "E: Unable to reinitialize\n");
241 if (FcConfigGetCurrent () == config)
243 fprintf (stderr, "E: config wasn't reloaded\n");
247 FcConfigDestroy (config);
249 config = FcConfigCreate ();
250 if (!FcConfigParseAndLoadFromMemory (config, (const FcChar8 *) conf, FcTrue))
252 printf ("E: Unable to load config again\n");
256 if (!FcConfigBuildFonts (config))
258 printf ("E: unable to build fonts again\n");
262 fprintf (stderr, "D: Obtaining fonts information again\n");
263 pat = FcPatternCreate ();
264 fs = FcFontList (config, pat, NULL);
265 FcPatternDestroy (pat);
266 if (!fs || fs->nfont != 0)
268 printf ("E: Unexpected the number of fonts: %d\n", !fs ? -1 : fs->nfont);
272 FcFontSetDestroy (fs);
273 fprintf (stderr, "D: Copying %s to %s\n", FONTFILE, fontdir);
274 snprintf (cmd, 512, "sleep 1; cp -a %s %s; sleep 1", FONTFILE, fontdir);
276 fprintf (stderr, "D: Reinitializing\n");
277 if (FcConfigUptoDate(config))
279 fprintf (stderr, "E: Config up-to-date after addition\n");
283 if (!FcInitReinitialize ())
285 fprintf (stderr, "E: Unable to reinitialize\n");
289 if (FcConfigGetCurrent () == config)
291 fprintf (stderr, "E: config wasn't reloaded\n");
295 FcConfigDestroy (config);
297 config = FcConfigCreate ();
298 if (!FcConfigParseAndLoadFromMemory (config, (const FcChar8 *) conf, FcTrue))
300 printf ("E: Unable to load config again\n");
304 if (!FcConfigBuildFonts (config))
306 printf ("E: unable to build fonts again\n");
310 fprintf (stderr, "D: Obtaining fonts information\n");
311 pat = FcPatternCreate ();
312 fs = FcFontList (config, pat, NULL);
313 FcPatternDestroy (pat);
314 if (!fs || fs->nfont != 1)
316 printf ("E: Unexpected the number of fonts: %d\n", !fs ? -1 : fs->nfont);
320 FcFontSetDestroy (fs);
321 FcConfigDestroy (config);
324 fprintf (stderr, "Cleaning up\n");
326 unlink_dirs (basedir);
330 FcStrFree (cachedir);