aboutsummaryrefslogtreecommitdiff
path: root/fontconfig/test/test-migration.c
blob: f127e276ec8a464453cb48c7e7d2513c3a01a8d0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#ifndef HAVE_STRUCT_DIRENT_D_TYPE
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include <fontconfig/fontconfig.h>

#ifdef HAVE_MKDTEMP
#define fc_mkdtemp	mkdtemp
#else
char *
fc_mkdtemp (char *template)
{
    if (!mktemp (template) || mkdir (template, 0700))
	return NULL;

    return template;
}
#endif

FcBool
mkdir_p(const char *dir)
{
    char *parent;
    FcBool ret;

    if (strlen (dir) == 0)
	return FcFalse;
    parent = (char *) FcStrDirname (dir);
    if (!parent)
	return FcFalse;
    if (access (parent, F_OK) == 0)
	ret = mkdir (dir, 0755) == 0 && chmod (dir, 0755) == 0;
    else if (access (parent, F_OK) == -1)
	ret = mkdir_p (parent) && (mkdir (dir, 0755) == 0) && chmod (dir, 0755) == 0;
    else
	ret = FcFalse;
    free (parent);

    return ret;
}

FcBool
unlink_dirs(const char *dir)
{
    DIR *d = opendir (dir);
    struct dirent *e;
    size_t len = strlen (dir);
    char *n = NULL;
    FcBool ret = FcTrue;
#ifndef HAVE_STRUCT_DIRENT_D_TYPE
    struct stat statb;
#endif

    if (!d)
	return FcFalse;
    while ((e = readdir(d)) != NULL)
    {
	size_t l;

	if (strcmp (e->d_name, ".") == 0 ||
	    strcmp (e->d_name, "..") == 0)
	    continue;
	l = strlen (e->d_name) + 1;
	if (n)
	    free (n);
	n = malloc (l + len + 1);
	strcpy (n, dir);
	n[len] = '/';
	strcpy (&n[len + 1], e->d_name);
#ifdef HAVE_STRUCT_DIRENT_D_TYPE
	if (e->d_type == DT_DIR)
#else
	if (stat (n, &statb) == -1)
	{
		fprintf (stderr, "E: %s\n", n);
		ret = FcFalse;
		break;
	}
	if (S_ISDIR (statb.st_mode))
#endif
	{
	    if (!unlink_dirs (n))
	    {
		fprintf (stderr, "E: %s\n", n);
		ret = FcFalse;
		break;
	    }
	}
	else
	{
	    if (unlink (n) == -1)
	    {
		fprintf (stderr, "E: %s\n", n);
		ret = FcFalse;
		break;
	    }
	}
    }
    if (n)
	free (n);
    closedir (d);

    if (rmdir (dir) == -1)
    {
	fprintf (stderr, "E: %s\n", dir);
	return FcFalse;
    }

    return ret;
}

int
main(void)
{
    char template[32] = "fontconfig-XXXXXXXX";
    char *tmp = fc_mkdtemp (template);
    size_t len = strlen (tmp), xlen, dlen;
    char xdg[256], confd[256], fn[256], nfn[256], ud[256], nud[256];
    int ret = -1;
    FILE *fp;
    char *content = "<fontconfig></fontconfig>";

    strcpy (xdg, tmp);
    strcpy (&xdg[len], "/.config");
    setenv ("HOME", tmp, 1);
    setenv ("XDG_CONFIG_HOME", xdg, 1);
    xlen = strlen (xdg);
    strcpy (confd, xdg);
    strcpy (&confd[xlen], "/fontconfig");
    dlen = strlen (confd);
    /* In case there are no configuration files nor directory */
    FcInit ();
    if (access (confd, F_OK) == 0)
    {
	fprintf (stderr, "%s unexpectedly exists\n", confd);
	goto bail;
    }
    FcFini ();
    if (!unlink_dirs (tmp))
    {
	fprintf (stderr, "Unable to clean up\n");
	goto bail;
    }
    /* In case there are the user configuration file */
    strcpy (fn, tmp);
    strcpy (&fn[len], "/.fonts.conf");
    strcpy (nfn, confd);
    strcpy (&nfn[dlen], "/fonts.conf");
    if (!mkdir_p (confd))
    {
	fprintf (stderr, "Unable to create a config dir: %s\n", confd);
	goto bail;
    }
    if ((fp = fopen (fn, "wb")) == NULL)
    {
	fprintf (stderr, "Unable to create a config file: %s\n", fn);
	goto bail;
    }
    fwrite (content, sizeof (char), strlen (content), fp);
    fclose (fp);
    FcInit ();
    if (access (nfn, F_OK) != 0)
    {
	fprintf (stderr, "migration failed for %s\n", nfn);
	goto bail;
    }
    FcFini ();
    if (!unlink_dirs (tmp))
    {
	fprintf (stderr, "Unable to clean up\n");
	goto bail;
    }
    /* In case there are the user configuration dir */
    strcpy (ud, tmp);
    strcpy (&ud[len], "/.fonts.conf.d");
    strcpy (nud, confd);
    strcpy (&nud[dlen], "/conf.d");
    if (!mkdir_p (ud))
    {
	fprintf (stderr, "Unable to create a config dir: %s\n", ud);
	goto bail;
    }
    FcInit ();
    if (access (nud, F_OK) != 0)
    {
	fprintf (stderr, "migration failed for %s\n", nud);
	goto bail;
    }
    FcFini ();

    ret = 0;
bail:
    unlink_dirs (tmp);

    return ret;
}