PageRenderTime 25ms CodeModel.GetById 17ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 1ms

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