c1daed938e5d5bc8b63b848ef318f5327903134e
[platform/upstream/fontconfig.git] / src / fcatomic.c
1 /*
2  * fontconfig/src/fcatomic.c
3  *
4  * Copyright © 2002 Keith Packard
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of the author(s) not be used in
11  * advertising or publicity pertaining to distribution of the software without
12  * specific, written prior permission.  The authors make no
13  * representations about the suitability of this software for any purpose.  It
14  * is provided "as is" without express or implied warranty.
15  *
16  * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
17  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
18  * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
19  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
20  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
21  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
22  * PERFORMANCE OF THIS SOFTWARE.
23  */
24
25 /*
26  * fcatomic.c
27  *
28  * Lock cache and configuration files for atomic update
29  *
30  * Uses only regular filesystem calls so it should
31  * work even in the absense of functioning file locking
32  *
33  * On Unix, four files are used:
34  *      file        - the data file accessed by other apps.
35  *      new         - a new version of the data file while it's being written
36  *      lck         - the lock file
37  *      tmp         - a temporary file made unique with mkstemp
38  *
39  *  Here's how it works:
40  *      Create 'tmp' and store our PID in it
41  *      Attempt to link it to 'lck'
42  *      Unlink 'tmp'
43  *      If the link succeeded, the lock is held
44  *
45  * On Windows, where there are no links, no tmp file is used, and lck
46  * is a directory that's mkdir'ed. If the mkdir succeeds, the lock is
47  * held.
48  */
49
50 #include "fcint.h"
51 #include <sys/types.h>
52 #include <sys/stat.h>
53 #include <unistd.h>
54 #include <stdlib.h>
55 #include <time.h>
56
57 #ifdef _WIN32
58 #include <direct.h>
59 #define mkdir(path,mode) _mkdir(path)
60 #endif
61
62 #define NEW_NAME        ".NEW"
63 #define LCK_NAME        ".LCK"
64 #define TMP_NAME        ".TMP-XXXXXX"
65
66 FcAtomic *
67 FcAtomicCreate (const FcChar8   *file)
68 {
69     int     file_len = strlen ((char *) file);
70     int     new_len = file_len + sizeof (NEW_NAME);
71     int     lck_len = file_len + sizeof (LCK_NAME);
72     int     tmp_len = file_len + sizeof (TMP_NAME);
73     int     total_len = (sizeof (FcAtomic) +
74                          file_len + 1 +
75                          new_len + 1 +
76                          lck_len + 1 +
77                          tmp_len + 1);
78     FcAtomic    *atomic = malloc (total_len);
79     if (!atomic)
80         return 0;
81
82     atomic->file = (FcChar8 *) (atomic + 1);
83     strcpy ((char *) atomic->file, (char *) file);
84
85     atomic->new = atomic->file + file_len + 1;
86     strcpy ((char *) atomic->new, (char *) file);
87     strcat ((char *) atomic->new, NEW_NAME);
88
89     atomic->lck = atomic->new + new_len + 1;
90     strcpy ((char *) atomic->lck, (char *) file);
91     strcat ((char *) atomic->lck, LCK_NAME);
92
93     atomic->tmp = atomic->lck + lck_len + 1;
94
95     return atomic;
96 }
97
98 FcBool
99 FcAtomicLock (FcAtomic *atomic)
100 {
101     int         ret;
102     struct stat lck_stat;
103
104 #ifdef HAVE_LINK
105     int         fd = -1;
106     FILE        *f = 0;
107     FcBool      no_link = FcFalse;
108
109     strcpy ((char *) atomic->tmp, (char *) atomic->file);
110     strcat ((char *) atomic->tmp, TMP_NAME);
111     fd = FcMakeTempfile ((char *) atomic->tmp);
112     if (fd < 0)
113         return FcFalse;
114     f = fdopen (fd, "w");
115     if (!f)
116     {
117         close (fd);
118         unlink ((char *) atomic->tmp);
119         return FcFalse;
120     }
121     ret = fprintf (f, "%ld\n", (long)getpid());
122     if (ret <= 0)
123     {
124         fclose (f);
125         unlink ((char *) atomic->tmp);
126         return FcFalse;
127     }
128     if (fclose (f) == EOF)
129     {
130         unlink ((char *) atomic->tmp);
131         return FcFalse;
132     }
133     ret = link ((char *) atomic->tmp, (char *) atomic->lck);
134     if (ret < 0 && errno == EPERM)
135     {
136         /* the filesystem where atomic->lck points to may not supports
137          * the hard link. so better try to fallback
138          */
139         ret = mkdir ((char *) atomic->lck, 0600);
140         no_link = FcTrue;
141     }
142     (void) unlink ((char *) atomic->tmp);
143 #else
144     ret = mkdir ((char *) atomic->lck, 0600);
145 #endif
146     if (ret < 0)
147     {
148         /*
149          * If the file is around and old (> 10 minutes),
150          * assume the lock is stale.  This assumes that any
151          * machines sharing the same filesystem will have clocks
152          * reasonably close to each other.
153          */
154         if (FcStat (atomic->lck, &lck_stat) >= 0)
155         {
156             time_t  now = time (0);
157             if ((long int) (now - lck_stat.st_mtime) > 10 * 60)
158             {
159 #ifdef HAVE_LINK
160                 if (no_link)
161                 {
162                     if (rmdir ((char *) atomic->lck) == 0)
163                         return FcAtomicLock (atomic);
164                 }
165                 else
166                 {
167                     if (unlink ((char *) atomic->lck) == 0)
168                         return FcAtomicLock (atomic);
169                 }
170 #else
171                 if (rmdir ((char *) atomic->lck) == 0)
172                     return FcAtomicLock (atomic);
173 #endif
174             }
175         }
176         return FcFalse;
177     }
178     (void) unlink ((char *) atomic->new);
179     return FcTrue;
180 }
181
182 FcChar8 *
183 FcAtomicNewFile (FcAtomic *atomic)
184 {
185     return atomic->new;
186 }
187
188 FcChar8 *
189 FcAtomicOrigFile (FcAtomic *atomic)
190 {
191     return atomic->file;
192 }
193
194 FcBool
195 FcAtomicReplaceOrig (FcAtomic *atomic)
196 {
197 #ifdef _WIN32
198     unlink ((const char *) atomic->file);
199 #endif
200     if (rename ((char *) atomic->new, (char *) atomic->file) < 0)
201         return FcFalse;
202     return FcTrue;
203 }
204
205 void
206 FcAtomicDeleteNew (FcAtomic *atomic)
207 {
208     unlink ((char *) atomic->new);
209 }
210
211 void
212 FcAtomicUnlock (FcAtomic *atomic)
213 {
214 #ifdef HAVE_LINK
215     if (unlink ((char *) atomic->lck) == -1)
216         rmdir ((char *) atomic->lck);
217 #else
218     rmdir ((char *) atomic->lck);
219 #endif
220 }
221
222 void
223 FcAtomicDestroy (FcAtomic *atomic)
224 {
225     free (atomic);
226 }
227 #define __fcatomic__
228 #include "fcaliastail.h"
229 #undef __fcatomic__