1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009 Josh Coalson
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
23 #if defined _MSC_VER || defined __MINGW32__
24 #include <sys/utime.h> /* for utime() */
25 #include <io.h> /* for chmod(), _setmode(), unlink() */
26 #include <fcntl.h> /* for _O_BINARY */
28 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
29 #include <utime.h> /* for utime() */
31 #if defined __CYGWIN__ || defined __EMX__
32 #include <io.h> /* for setmode(), O_BINARY */
33 #include <fcntl.h> /* for _O_BINARY */
35 #include <sys/stat.h> /* for stat(), maybe chmod() */
36 #if defined _WIN32 && !defined __CYGWIN__
38 #include <unistd.h> /* for unlink() */
42 #include <string.h> /* for strrchr() */
43 #if defined _WIN32 && !defined __CYGWIN__
44 // for GetFileInformationByHandle() etc
48 #include "share/grabbag.h"
51 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
54 struct utimbuf srctime;
56 if(0 == stat(srcpath, &srcstat)) {
57 srctime.actime = srcstat.st_atime;
58 srctime.modtime = srcstat.st_mtime;
59 (void)chmod(destpath, srcstat.st_mode);
60 (void)utime(destpath, &srctime);
64 FLAC__off_t grabbag__file_get_filesize(const char *srcpath)
66 #if defined _MSC_VER || defined __MINGW32__
67 struct _stat64 srcstat;
69 if(0 == _stat64(srcpath, &srcstat))
73 if(0 == stat(srcpath, &srcstat))
75 return srcstat.st_size;
80 const char *grabbag__file_get_basename(const char *srcpath)
84 p = strrchr(srcpath, '/');
86 p = strrchr(srcpath, '\\');
93 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
97 if(0 == stat(filename, &stats)) {
98 #if !defined _MSC_VER && !defined __MINGW32__
100 stats.st_mode &= ~S_IWUSR;
101 stats.st_mode &= ~S_IWGRP;
102 stats.st_mode &= ~S_IWOTH;
105 stats.st_mode |= S_IWUSR;
109 stats.st_mode &= ~S_IWRITE;
111 stats.st_mode |= S_IWRITE;
113 if(0 != chmod(filename, stats.st_mode))
122 FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
124 #if defined _MSC_VER || defined __MINGW32__
126 * http://www.hydrogenaudio.org/forums/index.php?showtopic=49439&pid=444300&st=0
127 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileinformationbyhandle.asp
128 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/by_handle_file_information_str.asp
129 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfile.asp
130 * apparently both the files have to be open at the same time for the comparison to work
132 FLAC__bool same = false;
133 BY_HANDLE_FILE_INFORMATION info1, info2;
136 h1 = CreateFile(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
137 h2 = CreateFile(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
138 if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
140 ok &= GetFileInformationByHandle(h1, &info1);
141 ok &= GetFileInformationByHandle(h2, &info2);
144 info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
145 info1.nFileIndexHigh == info2.nFileIndexHigh &&
146 info1.nFileIndexLow == info2.nFileIndexLow
148 if(h1 != INVALID_HANDLE_VALUE)
150 if(h2 != INVALID_HANDLE_VALUE)
155 return f1 && f2 && stat(f1, &s1) == 0 && stat(f2, &s2) == 0 && s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
159 FLAC__bool grabbag__file_remove_file(const char *filename)
161 return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
164 FILE *grabbag__file_get_binary_stdin(void)
166 /* if something breaks here it is probably due to the presence or
167 * absence of an underscore before the identifiers 'setmode',
168 * 'fileno', and/or 'O_BINARY'; check your system header files.
170 #if defined _MSC_VER || defined __MINGW32__
171 _setmode(_fileno(stdin), _O_BINARY);
172 #elif defined __CYGWIN__
173 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
174 setmode(_fileno(stdin), _O_BINARY);
175 #elif defined __EMX__
176 setmode(fileno(stdin), O_BINARY);
182 FILE *grabbag__file_get_binary_stdout(void)
184 /* if something breaks here it is probably due to the presence or
185 * absence of an underscore before the identifiers 'setmode',
186 * 'fileno', and/or 'O_BINARY'; check your system header files.
188 #if defined _MSC_VER || defined __MINGW32__
189 _setmode(_fileno(stdout), _O_BINARY);
190 #elif defined __CYGWIN__
191 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
192 setmode(_fileno(stdout), _O_BINARY);
193 #elif defined __EMX__
194 setmode(fileno(stdout), O_BINARY);