1 /* grabbag - Convenience lib for various routines common to several tools
2 * Copyright (C) 2002,2003,2004,2005,2006,2007 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 off_t grabbag__file_get_filesize(const char *srcpath)
68 if(0 == stat(srcpath, &srcstat))
69 return srcstat.st_size;
74 const char *grabbag__file_get_basename(const char *srcpath)
78 p = strrchr(srcpath, '/');
80 p = strrchr(srcpath, '\\');
87 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
91 if(0 == stat(filename, &stats)) {
92 #if !defined _MSC_VER && !defined __MINGW32__
94 stats.st_mode &= ~S_IWUSR;
95 stats.st_mode &= ~S_IWGRP;
96 stats.st_mode &= ~S_IWOTH;
99 stats.st_mode |= S_IWUSR;
103 stats.st_mode &= ~S_IWRITE;
105 stats.st_mode |= S_IWRITE;
107 if(0 != chmod(filename, stats.st_mode))
116 FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
118 #if defined _MSC_VER || defined __MINGW32__
120 * http://www.hydrogenaudio.org/forums/index.php?showtopic=49439&pid=444300&st=0
121 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/getfileinformationbyhandle.asp
122 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/by_handle_file_information_str.asp
123 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/fs/createfile.asp
124 * apparently both the files have to be open at the same time for the comparison to work
126 FLAC__bool same = false;
127 BY_HANDLE_FILE_INFORMATION info1, info2;
130 h1 = CreateFile(f1, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
131 h2 = CreateFile(f2, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
132 if(h1 == INVALID_HANDLE_VALUE || h2 == INVALID_HANDLE_VALUE)
134 ok &= GetFileInformationByHandle(h1, &info1);
135 ok &= GetFileInformationByHandle(h2, &info2);
138 info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
139 info1.nFileIndexHigh == info2.nFileIndexHigh &&
140 info1.nFileIndexLow == info2.nFileIndexLow
142 if(h1 != INVALID_HANDLE_VALUE)
144 if(h2 != INVALID_HANDLE_VALUE)
149 return f1 && f2 && stat(f1, &s1) == 0 && stat(f2, &s2) == 0 && s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
153 FLAC__bool grabbag__file_remove_file(const char *filename)
155 return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
158 FILE *grabbag__file_get_binary_stdin(void)
160 /* if something breaks here it is probably due to the presence or
161 * absence of an underscore before the identifiers 'setmode',
162 * 'fileno', and/or 'O_BINARY'; check your system header files.
164 #if defined _MSC_VER || defined __MINGW32__
165 _setmode(_fileno(stdin), _O_BINARY);
166 #elif defined __CYGWIN__
167 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
168 setmode(_fileno(stdin), _O_BINARY);
169 #elif defined __EMX__
170 setmode(fileno(stdin), O_BINARY);
176 FILE *grabbag__file_get_binary_stdout(void)
178 /* if something breaks here it is probably due to the presence or
179 * absence of an underscore before the identifiers 'setmode',
180 * 'fileno', and/or 'O_BINARY'; check your system header files.
182 #if defined _MSC_VER || defined __MINGW32__
183 _setmode(_fileno(stdout), _O_BINARY);
184 #elif defined __CYGWIN__
185 /* almost certainly not needed for any modern Cygwin, but let's be safe... */
186 setmode(_fileno(stdout), _O_BINARY);
187 #elif defined __EMX__
188 setmode(fileno(stdout), O_BINARY);