2 * Copyright © 2000 Keith Packard
3 * Copyright © 2005 Patrick Lam
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of the author(s) not be used in
10 * advertising or publicity pertaining to distribution of the software without
11 * specific, written prior permission. The authors make no
12 * representations about the suitability of this software for any purpose. It
13 * is provided "as is" without express or implied warranty.
15 * THE AUTHOR(S) DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
16 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
17 * EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY SPECIAL, INDIRECT OR
18 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
19 * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
21 * PERFORMANCE OF THIS SOFTWARE.
30 #include <sys/types.h>
36 #ifdef HAVE_SYS_STATFS_H
37 #include <sys/statfs.h>
39 #ifdef HAVE_SYS_PARAM_H
40 #include <sys/param.h>
42 #ifdef HAVE_SYS_MOUNT_H
43 #include <sys/mount.h>
51 typedef long long INT64;
52 #define EPOCH_OFFSET 11644473600ll
54 #define EPOCH_OFFSET 11644473600i64
55 typedef __int64 INT64;
58 /* Workaround for problems in the stat() in the Microsoft C library:
60 * 1) stat() uses FindFirstFile() to get the file
61 * attributes. Unfortunately this API doesn't return correct values
62 * for modification time of a directory until some time after a file
63 * or subdirectory has been added to the directory. (This causes
64 * run-test.sh to fail, for instance.) GetFileAttributesEx() is
65 * better, it returns the updated timestamp right away.
67 * 2) stat() does some strange things related to backward
68 * compatibility with the local time timestamps on FAT volumes and
69 * daylight saving time. This causes problems after the switches
70 * to/from daylight saving time. See
71 * http://bugzilla.gnome.org/show_bug.cgi?id=154968 , especially
72 * comment #30, and http://www.codeproject.com/datetime/dstbugs.asp .
73 * We don't need any of that, FAT and Win9x are as good as dead. So
74 * just use the UTC timestamps from NTFS, converted to the Unix epoch.
78 FcStat (const FcChar8 *file, struct stat *statb)
80 WIN32_FILE_ATTRIBUTE_DATA wfad;
81 char full_path_name[MAX_PATH];
85 if (!GetFileAttributesEx ((LPCSTR) file, GetFileExInfoStandard, &wfad))
90 /* Calculate a pseudo inode number as a hash of the full path name.
91 * Call GetLongPathName() to get the spelling of the path name as it
94 rc = GetFullPathName ((LPCSTR) file, sizeof (full_path_name), full_path_name, &basename);
95 if (rc == 0 || rc > sizeof (full_path_name))
98 rc = GetLongPathName (full_path_name, full_path_name, sizeof (full_path_name));
99 statb->st_ino = FcStringHash ((const FcChar8 *) full_path_name);
101 statb->st_mode = _S_IREAD | _S_IWRITE;
102 statb->st_mode |= (statb->st_mode >> 3) | (statb->st_mode >> 6);
104 if (wfad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
105 statb->st_mode |= _S_IFDIR;
107 statb->st_mode |= _S_IFREG;
110 statb->st_uid = statb->st_gid = 0;
113 if (wfad.nFileSizeHigh > 0)
115 statb->st_size = wfad.nFileSizeLow;
117 statb->st_atime = (*(INT64 *)&wfad.ftLastAccessTime)/10000000 - EPOCH_OFFSET;
118 statb->st_mtime = (*(INT64 *)&wfad.ftLastWriteTime)/10000000 - EPOCH_OFFSET;
119 statb->st_ctime = statb->st_mtime;
127 FcStat (const FcChar8 *file, struct stat *statb)
129 return stat ((char *) file, statb);
132 /* Adler-32 checksum implementation */
139 Adler32Init (struct Adler32 *ctx)
146 Adler32Update (struct Adler32 *ctx, const char *data, int data_len)
150 ctx->a = (ctx->a + *data++) % 65521;
151 ctx->b = (ctx->b + ctx->a) % 65521;
156 Adler32Finish (struct Adler32 *ctx)
158 return ctx->a + (ctx->b << 16);
161 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
162 /* dirent.d_type can be relied upon on FAT filesystem */
164 FcDirChecksumScandirFilter(const struct dirent *entry)
166 return entry->d_type != DT_DIR;
171 FcDirChecksumScandirSorter(const struct dirent **lhs, const struct dirent **rhs)
173 return strcmp((*lhs)->d_name, (*rhs)->d_name);
177 FcDirChecksum (const FcChar8 *dir, time_t *checksum)
180 struct dirent **files;
182 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
184 size_t len = strlen ((const char *)dir);
189 n = scandir ((const char *)dir, &files,
190 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
191 &FcDirChecksumScandirFilter,
195 &FcDirChecksumScandirSorter);
201 size_t dlen = strlen (files[n]->d_name);
204 #ifdef HAVE_STRUCT_DIRENT_D_TYPE
205 dtype = files[n]->d_type;
208 char f[PATH_MAX + 1];
210 memcpy (f, dir, len);
211 f[len] = FC_DIR_SEPARATOR;
212 memcpy (&f[len + 1], files[n]->d_name, dlen);
213 f[len + 1 + dlen] = 0;
214 if (lstat (f, &statb) < 0)
219 if (S_ISDIR (statb.st_mode))
222 dtype = statb.st_mode;
224 Adler32Update (&ctx, files[n]->d_name, dlen + 1);
225 Adler32Update (&ctx, (char *)&dtype, sizeof (int));
227 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
233 #ifndef HAVE_STRUCT_DIRENT_D_TYPE
238 *checksum = Adler32Finish (&ctx);
245 FcStatChecksum (const FcChar8 *file, struct stat *statb)
247 if (FcStat (file, statb) == -1)
251 /* We have a workaround of the broken stat() in FcStat() for Win32.
252 * No need to do something further more.
254 if (FcIsFsMtimeBroken (file))
256 if (FcDirChecksum (file, &statb->st_mtime) == -1)
265 FcFStatFs (int fd, FcStatFS *statb)
267 const char *p = NULL;
269 FcBool flag = FcFalse;
271 #if defined(HAVE_FSTATVFS) && (defined(HAVE_STRUCT_STATVFS_F_BASETYPE) || defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME))
274 memset (statb, 0, sizeof (FcStatFS));
276 if ((ret = fstatvfs (fd, &buf)) == 0)
278 # if defined(HAVE_STRUCT_STATVFS_F_BASETYPE)
280 # elif defined(HAVE_STRUCT_STATVFS_F_FSTYPENAME)
281 p = buf.f_fstypename;
284 #elif defined(HAVE_FSTATFS) && (defined(HAVE_STRUCT_STATFS_F_FLAGS) || defined(HAVE_STRUCT_STATFS_F_FSTYPENAME) || defined(__linux__))
287 memset (statb, 0, sizeof (FcStatFS));
289 if ((ret = fstatfs (fd, &buf)) == 0)
291 # if defined(HAVE_STRUCT_STATFS_F_FLAGS) && defined(MNT_LOCAL)
292 statb->is_remote_fs = !(buf.f_flags & MNT_LOCAL);
295 # if defined(HAVE_STRUCT_STATFS_F_FSTYPENAME)
296 p = buf.f_fstypename;
297 # elif defined(__linux__)
300 case 0x6969: /* nfs */
301 statb->is_remote_fs = FcTrue;
303 case 0x4d44: /* fat */
304 statb->is_mtime_broken = FcTrue;
312 # error "BUG: No way to figure out with fstatfs()"
318 if (!flag && strcmp (p, "nfs") == 0)
319 statb->is_remote_fs = FcTrue;
320 if (strcmp (p, "msdosfs") == 0 ||
321 strcmp (p, "pcfs") == 0)
322 statb->is_mtime_broken = FcTrue;
329 FcIsFsMmapSafe (int fd)
333 if (FcFStatFs (fd, &statb) < 0)
336 return !statb.is_remote_fs;
340 FcIsFsMtimeBroken (const FcChar8 *dir)
342 int fd = open ((const char *) dir, O_RDONLY);
347 int ret = FcFStatFs (fd, &statb);
353 return statb.is_mtime_broken;