PageRenderTime 26ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/libs/libflac/flac-1.3.2/src/share/grabbag/file.c

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