1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002-2009 Josh Coalson
3 * Copyright (C) 2011-2023 Xiph.Org Foundation
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #ifdef HAVE_CONFIG_H
21 # include <config.h>
22 #endif
23
24 #if defined _MSC_VER || defined __MINGW32__
25 #include <sys/utime.h> /* for utime() */
26 #include <io.h> /* for chmod(), _setmode(), unlink() */
27 #include <fcntl.h> /* for _O_BINARY */
28 #else
29 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
30 #endif
31 #if defined __EMX__
32 #include <io.h> /* for setmode(), O_BINARY */
33 #include <fcntl.h> /* for _O_BINARY */
34 #endif
35 #include <sys/stat.h> /* for stat(), maybe chmod() */
36 #if defined _WIN32 && !defined __CYGWIN__
37 #else
38 #include <unistd.h> /* for unlink() */
39 #endif
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h> /* for strrchr() */
43 #if defined _WIN32 && !defined __CYGWIN__
44 // for GetFileInformationByHandle() etc
45 #include <windows.h>
46 #include <winbase.h>
47 #endif
48 #include "share/grabbag.h"
49 #include "share/compat.h"
50
51
grabbag__file_copy_metadata(const char * srcpath,const char * destpath)52 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
53 {
54 struct flac_stat_s srcstat;
55
56 if(0 == flac_stat(srcpath, &srcstat)) {
57 #if defined(_POSIX_C_SOURCE) && (_POSIX_C_SOURCE >= 200809L) && !defined(_WIN32)
58 struct timespec srctime[2] = {};
59 srctime[0].tv_sec = srcstat.st_atime;
60 srctime[1].tv_sec = srcstat.st_mtime;
61 #else
62 struct utimbuf srctime;
63 srctime.actime = srcstat.st_atime;
64 srctime.modtime = srcstat.st_mtime;
65 #endif
66 (void)flac_chmod(destpath, srcstat.st_mode);
67 (void)flac_utime(destpath, &srctime);
68 }
69 }
70
grabbag__file_get_filesize(const char * srcpath)71 FLAC__off_t grabbag__file_get_filesize(const char *srcpath)
72 {
73 struct flac_stat_s srcstat;
74
75 if(0 == flac_stat(srcpath, &srcstat))
76 return srcstat.st_size;
77 else
78 return -1;
79 }
80
grabbag__file_get_basename(const char * srcpath)81 const char *grabbag__file_get_basename(const char *srcpath)
82 {
83 const char *p;
84
85 p = strrchr(srcpath, '/');
86 if(0 == p) {
87 #if defined _WIN32 && !defined __CYGWIN__
88 p = strrchr(srcpath, '\\');
89 if(0 == p)
90 #endif
91 return srcpath;
92 }
93 return ++p;
94 }
95
grabbag__file_change_stats(const char * filename,FLAC__bool read_only)96 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
97 {
98 struct flac_stat_s stats;
99
100 if(0 == flac_stat(filename, &stats)) {
101 #if !defined _MSC_VER && !defined __MINGW32__
102 if(read_only) {
103 stats.st_mode &= ~S_IWUSR;
104 stats.st_mode &= ~S_IWGRP;
105 stats.st_mode &= ~S_IWOTH;
106 }
107 else {
108 stats.st_mode |= S_IWUSR;
109 }
110 #else
111 if(read_only)
112 stats.st_mode &= ~S_IWRITE;
113 else
114 stats.st_mode |= S_IWRITE;
115 #endif
116 if(0 != flac_chmod(filename, stats.st_mode))
117 return false;
118 }
119 else
120 return false;
121
122 return true;
123 }
124
grabbag__file_are_same(const char * f1,const char * f2)125 FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
126 {
127 #if defined _WIN32 && !defined __CYGWIN__
128 #if !defined(WINAPI_FAMILY_PARTITION)
129 #define WINAPI_FAMILY_PARTITION(x) x
130 #define WINAPI_PARTITION_DESKTOP 1
131 #endif
132 /* see
133 * http://www.hydrogenaudio.org/forums/index.php?showtopic=49439&pid=444300&st=0
134 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileinformationbyhandle.asp
135 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/by_handle_file_information_str.asp
136 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfile.asp
137 * apparently both the files have to be open at the same time for the comparison to work
138 */
139 FLAC__bool same = false;
140 BY_HANDLE_FILE_INFORMATION info1, info2;
141 HANDLE h1, h2;
142 BOOL ok = 1;
143 h1 = CreateFile_utf8(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
144 h2 = CreateFile_utf8(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
145 if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
146 ok = 0;
147 #if WINAPI_FAMILY_PARTITION(WINAPI_PARTITION_DESKTOP)
148 ok &= GetFileInformationByHandle(h1, &info1);
149 ok &= GetFileInformationByHandle(h2, &info2);
150 if(ok)
151 same =
152 info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
153 info1.nFileIndexHigh == info2.nFileIndexHigh &&
154 info1.nFileIndexLow == info2.nFileIndexLow
155 ;
156 #else // !WINAPI_PARTITION_DESKTOP
157 FILE_ID_INFO id_info1, id_info2;
158 same = GetFileInformationByHandleEx(h1, FileIdInfo, &id_info1, sizeof (id_info1)) &&
159 GetFileInformationByHandleEx(h2, FileIdInfo, &id_info2, sizeof (id_info2)) &&
160 id_info1.VolumeSerialNumber == id_info2.VolumeSerialNumber &&
161 memcmp(&id_info1.FileId, &id_info2.FileId, sizeof(id_info1.FileId)) == 0;
162 #endif // !WINAPI_PARTITION_DESKTOP
163 if(h1 != INVALID_HANDLE_VALUE)
164 CloseHandle(h1);
165 if(h2 != INVALID_HANDLE_VALUE)
166 CloseHandle(h2);
167 return same;
168 #else
169 struct flac_stat_s s1, s2;
170 return f1 && f2 && flac_stat(f1, &s1) == 0 && flac_stat(f2, &s2) == 0 && s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
171 #endif
172 }
173
grabbag__file_remove_file(const char * filename)174 FLAC__bool grabbag__file_remove_file(const char *filename)
175 {
176 return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == flac_unlink(filename);
177 }
178
grabbag__file_get_binary_stdin(void)179 FILE *grabbag__file_get_binary_stdin(void)
180 {
181 /* if something breaks here it is probably due to the presence or
182 * absence of an underscore before the identifiers 'setmode',
183 * 'fileno', and/or 'O_BINARY'; check your system header files.
184 */
185 #if defined _MSC_VER || defined __MINGW32__
186 _setmode(_fileno(stdin), _O_BINARY);
187 #elif defined __EMX__
188 setmode(fileno(stdin), O_BINARY);
189 #endif
190
191 return stdin;
192 }
193
grabbag__file_get_binary_stdout(void)194 FILE *grabbag__file_get_binary_stdout(void)
195 {
196 /* if something breaks here it is probably due to the presence or
197 * absence of an underscore before the identifiers 'setmode',
198 * 'fileno', and/or 'O_BINARY'; check your system header files.
199 */
200 #if defined _MSC_VER || defined __MINGW32__
201 _setmode(_fileno(stdout), _O_BINARY);
202 #elif defined __EMX__
203 setmode(fileno(stdout), O_BINARY);
204 #endif
205
206 return stdout;
207 }
208