Merge branch 'gpu_unai_plugin_update'
[pcsx_rearmed.git] / deps / flac-1.3.2 / src / share / grabbag / file.c
1 /* grabbag - Convenience lib for various routines common to several tools
2  * Copyright (C) 2002-2009  Josh Coalson
3  * Copyright (C) 2011-2016  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 #include <utime.h> /* for utime() */
31 #endif
32 #if defined __CYGWIN__ || defined __EMX__
33 #include <io.h> /* for setmode(), O_BINARY */
34 #include <fcntl.h> /* for _O_BINARY */
35 #endif
36 #include <sys/stat.h> /* for stat(), maybe chmod() */
37 #if defined _WIN32 && !defined __CYGWIN__
38 #else
39 #include <unistd.h> /* for unlink() */
40 #endif
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <string.h> /* for strrchr() */
44 #if defined _WIN32 && !defined __CYGWIN__
45 // for GetFileInformationByHandle() etc
46 #include <windows.h>
47 #include <winbase.h>
48 #endif
49 #include "share/grabbag.h"
50
51
52 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
53 {
54         struct flac_stat_s srcstat;
55         struct utimbuf srctime;
56
57         if(0 == flac_stat(srcpath, &srcstat)) {
58                 srctime.actime = srcstat.st_atime;
59                 srctime.modtime = srcstat.st_mtime;
60                 (void)flac_chmod(destpath, srcstat.st_mode);
61 #if !defined _3DS
62                 (void)flac_utime(destpath, &srctime);
63 #endif
64         }
65 }
66
67 FLAC__off_t grabbag__file_get_filesize(const char *srcpath)
68 {
69         struct flac_stat_s srcstat;
70
71         if(0 == flac_stat(srcpath, &srcstat))
72                 return srcstat.st_size;
73         else
74                 return -1;
75 }
76
77 const char *grabbag__file_get_basename(const char *srcpath)
78 {
79         const char *p;
80
81         p = strrchr(srcpath, '/');
82         if(0 == p) {
83                 p = strrchr(srcpath, '\\');
84                 if(0 == p)
85                         return srcpath;
86         }
87         return ++p;
88 }
89
90 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
91 {
92         struct flac_stat_s stats;
93
94         if(0 == flac_stat(filename, &stats)) {
95 #if !defined _MSC_VER && !defined __MINGW32__
96                 if(read_only) {
97                         stats.st_mode &= ~S_IWUSR;
98                         stats.st_mode &= ~S_IWGRP;
99                         stats.st_mode &= ~S_IWOTH;
100                 }
101                 else {
102                         stats.st_mode |= S_IWUSR;
103                 }
104 #else
105                 if(read_only)
106                         stats.st_mode &= ~S_IWRITE;
107                 else
108                         stats.st_mode |= S_IWRITE;
109 #endif
110                 if(0 != flac_chmod(filename, stats.st_mode))
111                         return false;
112         }
113         else
114                 return false;
115
116         return true;
117 }
118
119 FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
120 {
121 #if defined _MSC_VER || defined __MINGW32__
122         /* see
123          * http://www.hydrogenaudio.org/forums/index.php?showtopic=49439&pid=444300&st=0
124          *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileinformationbyhandle.asp
125          *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/by_handle_file_information_str.asp
126          *  http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfile.asp
127          * apparently both the files have to be open at the same time for the comparison to work
128          */
129         FLAC__bool same = false;
130         BY_HANDLE_FILE_INFORMATION info1, info2;
131         HANDLE h1, h2;
132         BOOL ok = 1;
133         h1 = CreateFile_utf8(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
134         h2 = CreateFile_utf8(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
135         if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
136                 ok = 0;
137         ok &= GetFileInformationByHandle(h1, &info1);
138         ok &= GetFileInformationByHandle(h2, &info2);
139         if(ok)
140                 same =
141                         info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
142                         info1.nFileIndexHigh == info2.nFileIndexHigh &&
143                         info1.nFileIndexLow == info2.nFileIndexLow
144                 ;
145         if(h1 != INVALID_HANDLE_VALUE)
146                 CloseHandle(h1);
147         if(h2 != INVALID_HANDLE_VALUE)
148                 CloseHandle(h2);
149         return same;
150 #else
151         struct flac_stat_s s1, s2;
152         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;
153 #endif
154 }
155
156 FLAC__bool grabbag__file_remove_file(const char *filename)
157 {
158         return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == flac_unlink(filename);
159 }
160
161 FILE *grabbag__file_get_binary_stdin(void)
162 {
163         /* if something breaks here it is probably due to the presence or
164          * absence of an underscore before the identifiers 'setmode',
165          * 'fileno', and/or 'O_BINARY'; check your system header files.
166          */
167 #if defined _MSC_VER || defined __MINGW32__
168         _setmode(_fileno(stdin), _O_BINARY);
169 #elif defined __CYGWIN__
170         /* almost certainly not needed for any modern Cygwin, but let's be safe... */
171         setmode(_fileno(stdin), _O_BINARY);
172 #elif defined __EMX__
173         setmode(fileno(stdin), O_BINARY);
174 #endif
175
176         return stdin;
177 }
178
179 FILE *grabbag__file_get_binary_stdout(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(stdout), _O_BINARY);
187 #elif defined __CYGWIN__
188         /* almost certainly not needed for any modern Cygwin, but let's be safe... */
189         setmode(_fileno(stdout), _O_BINARY);
190 #elif defined __EMX__
191         setmode(fileno(stdout), O_BINARY);
192 #endif
193
194         return stdout;
195 }