Fix a couple of Windows 2Gig file size issues.
[platform/upstream/flac.git] / src / share / grabbag / file.c
1 /* grabbag - Convenience lib for various routines common to several tools
2  * Copyright (C) 2002,2003,2004,2005,2006,2007,2008,2009  Josh Coalson
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #if HAVE_CONFIG_H
20 #  include <config.h>
21 #endif
22
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 */
27 #else
28 #include <sys/types.h> /* some flavors of BSD (like OS X) require this to get time_t */
29 #include <utime.h> /* for utime() */
30 #endif
31 #if defined __CYGWIN__ || 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
50
51 void grabbag__file_copy_metadata(const char *srcpath, const char *destpath)
52 {
53         struct stat srcstat;
54         struct utimbuf srctime;
55
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);
61         }
62 }
63
64 FLAC__off_t grabbag__file_get_filesize(const char *srcpath)
65 {
66 #if defined _MSC_VER || defined __MINGW32__
67         struct _stat64 srcstat;
68
69         if(0 == _stat64(srcpath, &srcstat))
70 #else
71         struct stat srcstat;
72
73         if(0 == stat(srcpath, &srcstat))
74 #endif
75                 return srcstat.st_size;
76         else
77                 return -1;
78 }
79
80 const char *grabbag__file_get_basename(const char *srcpath)
81 {
82         const char *p;
83
84         p = strrchr(srcpath, '/');
85         if(0 == p) {
86                 p = strrchr(srcpath, '\\');
87                 if(0 == p)
88                         return srcpath;
89         }
90         return ++p;
91 }
92
93 FLAC__bool grabbag__file_change_stats(const char *filename, FLAC__bool read_only)
94 {
95         struct stat stats;
96
97         if(0 == stat(filename, &stats)) {
98 #if !defined _MSC_VER && !defined __MINGW32__
99                 if(read_only) {
100                         stats.st_mode &= ~S_IWUSR;
101                         stats.st_mode &= ~S_IWGRP;
102                         stats.st_mode &= ~S_IWOTH;
103                 }
104                 else {
105                         stats.st_mode |= S_IWUSR;
106                 }
107 #else
108                 if(read_only)
109                         stats.st_mode &= ~S_IWRITE;
110                 else
111                         stats.st_mode |= S_IWRITE;
112 #endif
113                 if(0 != chmod(filename, stats.st_mode))
114                         return false;
115         }
116         else
117                 return false;
118
119         return true;
120 }
121
122 FLAC__bool grabbag__file_are_same(const char *f1, const char *f2)
123 {
124 #if defined _MSC_VER || defined __MINGW32__
125         /* see
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
131          */
132         FLAC__bool same = false;
133         BY_HANDLE_FILE_INFORMATION info1, info2;
134         HANDLE h1, h2;
135         BOOL ok = 1;
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)
139                 ok = 0;
140         ok &= GetFileInformationByHandle(h1, &info1);
141         ok &= GetFileInformationByHandle(h2, &info2);
142         if(ok)
143                 same =
144                         info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber &&
145                         info1.nFileIndexHigh == info2.nFileIndexHigh &&
146                         info1.nFileIndexLow == info2.nFileIndexLow
147                 ;
148         if(h1 != INVALID_HANDLE_VALUE)
149                 CloseHandle(h1);
150         if(h2 != INVALID_HANDLE_VALUE)
151                 CloseHandle(h2);
152         return same;
153 #else
154         struct stat s1, s2;
155         return f1 && f2 && stat(f1, &s1) == 0 && stat(f2, &s2) == 0 && s1.st_ino == s2.st_ino && s1.st_dev == s2.st_dev;
156 #endif
157 }
158
159 FLAC__bool grabbag__file_remove_file(const char *filename)
160 {
161         return grabbag__file_change_stats(filename, /*read_only=*/false) && 0 == unlink(filename);
162 }
163
164 FILE *grabbag__file_get_binary_stdin(void)
165 {
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.
169          */
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);
177 #endif
178
179         return stdin;
180 }
181
182 FILE *grabbag__file_get_binary_stdout(void)
183 {
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.
187          */
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);
195 #endif
196
197         return stdout;
198 }