/src/util.c

https://code.google.com/ · C · 70 lines · 39 code · 6 blank · 25 comment · 16 complexity · 2278c49302882a2215e1bf66404826c2 MD5 · raw file

  1. /*
  2. $Id: util.c 231 2011-06-27 13:46:19Z marc.noirot $
  3. FLV Metadata updater
  4. Copyright (C) 2007-2012 Marc Noirot <marc.noirot AT gmail.com>
  5. This file is part of FLVMeta.
  6. FLVMeta is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10. FLVMeta 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
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with FLVMeta; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifdef WIN32
  19. # define WIN32_LEAN_AND_MEAN
  20. # include <windows.h>
  21. #else /* !WIN32 */
  22. # include <sys/types.h>
  23. # include <sys/stat.h>
  24. # include <unistd.h>
  25. #endif /* WIN32 */
  26. #include "util.h"
  27. int same_file(const char * file1, const char * file2) {
  28. #ifdef WIN32
  29. /* in Windows, we have to open the files and use GetFileInformationByHandle */
  30. HANDLE h1, h2;
  31. BY_HANDLE_FILE_INFORMATION info1, info2;
  32. h1 = CreateFile(file1, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  33. if (h1 == INVALID_HANDLE_VALUE) {
  34. return 0;
  35. }
  36. GetFileInformationByHandle(h1, &info1);
  37. CloseHandle(h1);
  38. h2 = CreateFile(file2, 0, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
  39. if (h2 == INVALID_HANDLE_VALUE) {
  40. return 0;
  41. }
  42. GetFileInformationByHandle(h2, &info2);
  43. CloseHandle(h2);
  44. return (info1.dwVolumeSerialNumber == info2.dwVolumeSerialNumber
  45. && info1.nFileIndexHigh == info2.nFileIndexHigh
  46. && info1.nFileIndexLow == info2.nFileIndexLow);
  47. #else /* !WIN32 */
  48. /* if not in Windows, we must stat each file and compare device and inode numbers */
  49. struct stat s1, s2;
  50. if (stat(file1, &s1) != 0) {
  51. return 0;
  52. }
  53. if (stat(file2, &s2) != 0) {
  54. return 0;
  55. }
  56. return (s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino);
  57. #endif /* WIN32 */
  58. }