forked from Ramaguru-Forks/ssdeep
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-file-size.c
227 lines (178 loc) · 4.32 KB
/
find-file-size.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Fuzzy Hashing by Jesse Kornblum
// Copyright (C) 2012 Kyrus
// Copyright (C) 2008 ManTech International Corporation
//
// $Id: find-file-size.c 144 2012-04-24 14:59:33Z jessekornblum $
//
#include "main.h"
// Prototypes
off_t find_file_size(FILE *f);
off_t find_dev_size(int fd, int blk_size);
#ifndef _WIN32
// Return the size, in bytes of an open file stream. On error, return 0
#if defined (__LINUX__)
off_t find_file_size(FILE *f)
{
off_t num_sectors = 0, sector_size = 0;
int fd = fileno(f);
struct stat sb;
if (fstat(fd,&sb))
return 0;
if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
return sb.st_size;
#ifdef HAVE_SYS_IOCTL_H
#ifdef HAVE_SYS_MOUNT_H
if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))
{
#if defined(_IO) && defined(BLKGETSIZE)
if (ioctl(fd, BLKGETSIZE, &num_sectors))
{
return 0;
}
#else
// If we can't run the ioctl call, we can't do anything here
return 0;
#endif // ifdefined _IO && BLKGETSIZE
#if defined(_IO) && defined(BLKSSZGET)
if (ioctl(fd, BLKSSZGET, §or_size))
{
return 0;
}
if (0 == sector_size)
sector_size = 512;
#else
sector_size = 512;
#endif // ifdef _IO && BLKSSZGET
return (num_sectors * sector_size);
}
#endif // #ifdef HAVE_SYS_MOUNT_H
#endif // #ifdef HAVE_SYS_IOCTL_H
return 0;
}
#elif defined (__APPLE__)
off_t find_file_size(FILE *f) {
struct stat info;
off_t total = 0;
off_t original = ftello(f);
int fd = fileno(f);
uint32_t blocksize = 0;
uint64_t blockcount = 0;
// I'd prefer not to use fstat as it will follow symbolic links. We don't
// follow symbolic links. That being said, all symbolic links *should*
// have been caught before we got here.
if (fstat(fd, &info))
{
return 0;
}
#ifdef HAVE_SYS_IOCTL_H
// Block devices, like /dev/hda, don't return a normal filesize.
// If we are working with a block device, we have to ask the operating
// system to tell us the true size of the device.
//
// This isn't the recommended way to do check for block devices,
// but using S_ISBLK(info.stmode) wasn't working.
if (info.st_mode & S_IFBLK)
{
// Get the block size
if (ioctl(fd, DKIOCGETBLOCKSIZE,&blocksize) < 0)
{
return 0;
}
// Get the number of blocks
if (ioctl(fd, DKIOCGETBLOCKCOUNT, &blockcount) < 0)
{
}
total = blocksize * blockcount;
}
#endif // ifdef HAVE_IOCTL_H
else
{
if ((fseeko(f,0,SEEK_END)))
return 0;
total = ftello(f);
if ((fseeko(f,original,SEEK_SET)))
return 0;
}
return (total - original);
}
#else // ifdef __APPLE__
// This is code for general UNIX systems
// (e.g. NetBSD, FreeBSD, OpenBSD, etc)
static off_t
midpoint (off_t a, off_t b, long blksize)
{
off_t aprime = a / blksize;
off_t bprime = b / blksize;
off_t c, cprime;
cprime = (bprime - aprime) / 2 + aprime;
c = cprime * blksize;
return c;
}
off_t find_dev_size(int fd, int blk_size)
{
off_t curr = 0, amount = 0;
void *buf;
if (blk_size == 0)
return 0;
buf = malloc(blk_size);
for (;;) {
ssize_t nread;
lseek(fd, curr, SEEK_SET);
nread = read(fd, buf, blk_size);
if (nread < blk_size)
{
if (nread <= 0)
{
if (curr == amount)
{
free(buf);
lseek(fd, 0, SEEK_SET);
return amount;
}
curr = midpoint(amount, curr, blk_size);
}
else
{ // 0 < nread < blk_size
free(buf);
lseek(fd, 0, SEEK_SET);
return amount + nread;
}
}
else
{
amount = curr + blk_size;
curr = amount * 2;
}
}
free(buf);
lseek(fd, 0, SEEK_SET);
return amount;
}
off_t find_file_size(FILE *f)
{
int fd = fileno(f);
struct stat sb;
if (fstat(fd,&sb))
return 0;
if (S_ISREG(sb.st_mode) || S_ISDIR(sb.st_mode))
return sb.st_size;
else if (S_ISCHR(sb.st_mode) || S_ISBLK(sb.st_mode))
return find_dev_size(fd,sb.st_blksize);
return 0;
}
#endif // ifdef __LINUX__
#endif // ifndef _WIN32
#if defined(_WIN32)
off_t find_file_size(FILE *f)
{
off_t total = 0, original = ftello(f);
// Windows does not support running fstat on block devices,
// so there's no point in mucking about with them.
if ((fseeko(f,0,SEEK_END)))
return 0;
total = ftello(f);
if ((fseeko(f,original,SEEK_SET)))
return 0;
return total;
}
#endif // ifdef _WIN32