1 | /* |
---|
2 | * This file Copyright (C) 2007-2008 Charles Kerr <charles@rebelbase.com> |
---|
3 | * |
---|
4 | * This file is licensed by the GPL version 2. Works owned by the |
---|
5 | * Transmission project are granted a special exemption to clause 2(b) |
---|
6 | * so that the bulk of its code can remain under the MIT license. |
---|
7 | * This exemption does not extend to derived works not owned by |
---|
8 | * the Transmission project. |
---|
9 | * |
---|
10 | * $Id: inout.c 7338 2008-12-10 03:45:57Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <assert.h> |
---|
14 | #include <errno.h> |
---|
15 | #include <stdlib.h> /* realloc */ |
---|
16 | #include <string.h> /* memcmp */ |
---|
17 | |
---|
18 | #include <sys/types.h> |
---|
19 | #include <sys/stat.h> |
---|
20 | #include <unistd.h> |
---|
21 | |
---|
22 | #include <openssl/sha.h> |
---|
23 | |
---|
24 | #include "transmission.h" |
---|
25 | #include "crypto.h" |
---|
26 | #include "fdlimit.h" |
---|
27 | #include "inout.h" |
---|
28 | #include "platform.h" |
---|
29 | #include "stats.h" |
---|
30 | #include "torrent.h" |
---|
31 | #include "utils.h" |
---|
32 | |
---|
33 | #ifdef TR_EMBEDDED |
---|
34 | #define TR_HASH_BUFSIZE (16*1024) |
---|
35 | #else |
---|
36 | #define TR_HASH_BUFSIZE (64*1024) |
---|
37 | #endif |
---|
38 | |
---|
39 | /**** |
---|
40 | ***** Low-level IO functions |
---|
41 | ****/ |
---|
42 | |
---|
43 | #ifdef WIN32 |
---|
44 | #if defined(read) |
---|
45 | #undef read |
---|
46 | #endif |
---|
47 | #define read _read |
---|
48 | |
---|
49 | #if defined(write) |
---|
50 | #undef write |
---|
51 | #endif |
---|
52 | #define write _write |
---|
53 | #endif |
---|
54 | |
---|
55 | enum { TR_IO_READ, TR_IO_WRITE }; |
---|
56 | |
---|
57 | static int64_t |
---|
58 | tr_lseek( int fd, int64_t offset, int whence ) |
---|
59 | { |
---|
60 | #if defined(_LARGEFILE_SOURCE) |
---|
61 | return lseek64( fd, (off64_t)offset, whence ); |
---|
62 | #elif defined(WIN32) |
---|
63 | return _lseeki64( fd, offset, whence ); |
---|
64 | #else |
---|
65 | return lseek( fd, (off_t)offset, whence ); |
---|
66 | #endif |
---|
67 | } |
---|
68 | |
---|
69 | /* returns 0 on success, or an errno on failure */ |
---|
70 | static int |
---|
71 | readOrWriteBytes( const tr_torrent * tor, |
---|
72 | int ioMode, |
---|
73 | tr_file_index_t fileIndex, |
---|
74 | uint64_t fileOffset, |
---|
75 | void * buf, |
---|
76 | size_t buflen ) |
---|
77 | { |
---|
78 | const tr_info * info = &tor->info; |
---|
79 | const tr_file * file = &info->files[fileIndex]; |
---|
80 | |
---|
81 | typedef size_t ( *iofunc )( int, void *, size_t ); |
---|
82 | iofunc func = ioMode == |
---|
83 | TR_IO_READ ? (iofunc)read : (iofunc)write; |
---|
84 | char * path; |
---|
85 | struct stat sb; |
---|
86 | int fd = -1; |
---|
87 | int err; |
---|
88 | int fileExists; |
---|
89 | |
---|
90 | assert( tor->downloadDir && *tor->downloadDir ); |
---|
91 | assert( fileIndex < info->fileCount ); |
---|
92 | assert( !file->length || ( fileOffset < file->length ) ); |
---|
93 | assert( fileOffset + buflen <= file->length ); |
---|
94 | |
---|
95 | path = tr_buildPath( tor->downloadDir, file->name, NULL ); |
---|
96 | fileExists = !stat( path, &sb ); |
---|
97 | tr_free( path ); |
---|
98 | |
---|
99 | if( !file->length ) |
---|
100 | return 0; |
---|
101 | |
---|
102 | if( ( ioMode == TR_IO_READ ) && !fileExists ) /* does file exist? */ |
---|
103 | err = errno; |
---|
104 | else if( ( fd = tr_fdFileCheckout ( tor->downloadDir, file->name, ioMode == TR_IO_WRITE, !file->dnd, file->length ) ) < 0 ) |
---|
105 | err = errno; |
---|
106 | else if( tr_lseek( fd, (int64_t)fileOffset, SEEK_SET ) == -1 ) |
---|
107 | err = errno; |
---|
108 | else if( func( fd, buf, buflen ) != buflen ) |
---|
109 | err = errno; |
---|
110 | else |
---|
111 | err = 0; |
---|
112 | |
---|
113 | if( ( !err ) && ( !fileExists ) && ( ioMode == TR_IO_WRITE ) ) |
---|
114 | tr_statsFileCreated( tor->session ); |
---|
115 | |
---|
116 | if( fd >= 0 ) |
---|
117 | tr_fdFileReturn( fd ); |
---|
118 | |
---|
119 | return err; |
---|
120 | } |
---|
121 | |
---|
122 | static int |
---|
123 | compareOffsetToFile( const void * a, |
---|
124 | const void * b ) |
---|
125 | { |
---|
126 | const uint64_t offset = *(const uint64_t*)a; |
---|
127 | const tr_file * file = b; |
---|
128 | |
---|
129 | if( offset < file->offset ) return -1; |
---|
130 | if( offset >= file->offset + file->length ) return 1; |
---|
131 | return 0; |
---|
132 | } |
---|
133 | |
---|
134 | void |
---|
135 | tr_ioFindFileLocation( const tr_torrent * tor, |
---|
136 | tr_piece_index_t pieceIndex, |
---|
137 | uint32_t pieceOffset, |
---|
138 | tr_file_index_t * fileIndex, |
---|
139 | uint64_t * fileOffset ) |
---|
140 | { |
---|
141 | const uint64_t offset = tr_pieceOffset( tor, pieceIndex, pieceOffset, 0 ); |
---|
142 | const tr_file * file; |
---|
143 | |
---|
144 | file = bsearch( &offset, |
---|
145 | tor->info.files, tor->info.fileCount, sizeof( tr_file ), |
---|
146 | compareOffsetToFile ); |
---|
147 | |
---|
148 | *fileIndex = file - tor->info.files; |
---|
149 | *fileOffset = offset - file->offset; |
---|
150 | |
---|
151 | assert( *fileIndex < tor->info.fileCount ); |
---|
152 | assert( *fileOffset < file->length ); |
---|
153 | assert( tor->info.files[*fileIndex].offset + *fileOffset == offset ); |
---|
154 | } |
---|
155 | |
---|
156 | /* returns 0 on success, or an errno on failure */ |
---|
157 | static int |
---|
158 | readOrWritePiece( const tr_torrent * tor, |
---|
159 | int ioMode, |
---|
160 | tr_piece_index_t pieceIndex, |
---|
161 | uint32_t pieceOffset, |
---|
162 | uint8_t * buf, |
---|
163 | size_t buflen ) |
---|
164 | { |
---|
165 | int err = 0; |
---|
166 | tr_file_index_t fileIndex; |
---|
167 | uint64_t fileOffset; |
---|
168 | const tr_info * info = &tor->info; |
---|
169 | |
---|
170 | if( pieceIndex >= tor->info.pieceCount ) |
---|
171 | return EINVAL; |
---|
172 | if( pieceOffset + buflen > tr_torPieceCountBytes( tor, pieceIndex ) ) |
---|
173 | return EINVAL; |
---|
174 | |
---|
175 | tr_ioFindFileLocation( tor, pieceIndex, pieceOffset, |
---|
176 | &fileIndex, &fileOffset ); |
---|
177 | |
---|
178 | while( buflen && !err ) |
---|
179 | { |
---|
180 | const tr_file * file = &info->files[fileIndex]; |
---|
181 | const uint64_t bytesThisPass = MIN( buflen, file->length - fileOffset ); |
---|
182 | |
---|
183 | err = readOrWriteBytes( tor, ioMode, fileIndex, fileOffset, buf, bytesThisPass ); |
---|
184 | buf += bytesThisPass; |
---|
185 | buflen -= bytesThisPass; |
---|
186 | ++fileIndex; |
---|
187 | fileOffset = 0; |
---|
188 | } |
---|
189 | |
---|
190 | return err; |
---|
191 | } |
---|
192 | |
---|
193 | int |
---|
194 | tr_ioRead( const tr_torrent * tor, |
---|
195 | tr_piece_index_t pieceIndex, |
---|
196 | uint32_t begin, |
---|
197 | uint32_t len, |
---|
198 | uint8_t * buf ) |
---|
199 | { |
---|
200 | return readOrWritePiece( tor, TR_IO_READ, pieceIndex, begin, buf, len ); |
---|
201 | } |
---|
202 | |
---|
203 | int |
---|
204 | tr_ioWrite( const tr_torrent * tor, |
---|
205 | tr_piece_index_t pieceIndex, |
---|
206 | uint32_t begin, |
---|
207 | uint32_t len, |
---|
208 | const uint8_t * buf ) |
---|
209 | { |
---|
210 | return readOrWritePiece( tor, TR_IO_WRITE, pieceIndex, begin, |
---|
211 | (uint8_t*)buf, |
---|
212 | len ); |
---|
213 | } |
---|
214 | |
---|
215 | /**** |
---|
216 | ***** |
---|
217 | ****/ |
---|
218 | |
---|
219 | static int |
---|
220 | recalculateHash( const tr_torrent * tor, |
---|
221 | tr_piece_index_t pieceIndex, |
---|
222 | uint8_t * setme ) |
---|
223 | { |
---|
224 | static uint8_t * buf = NULL; |
---|
225 | size_t bytesLeft; |
---|
226 | uint32_t offset = 0; |
---|
227 | int success = TRUE; |
---|
228 | SHA_CTX sha; |
---|
229 | |
---|
230 | if( buf == NULL ) |
---|
231 | buf = tr_new( uint8_t, TR_HASH_BUFSIZE ); |
---|
232 | |
---|
233 | assert( tor ); |
---|
234 | assert( setme ); |
---|
235 | assert( pieceIndex < tor->info.pieceCount ); |
---|
236 | |
---|
237 | SHA1_Init( &sha ); |
---|
238 | bytesLeft = tr_torPieceCountBytes( tor, pieceIndex ); |
---|
239 | |
---|
240 | while( bytesLeft ) |
---|
241 | { |
---|
242 | const int len = MIN( bytesLeft, TR_HASH_BUFSIZE ); |
---|
243 | success = !tr_ioRead( tor, pieceIndex, offset, len, buf ); |
---|
244 | if( !success ) |
---|
245 | break; |
---|
246 | SHA1_Update( &sha, buf, len ); |
---|
247 | offset += len; |
---|
248 | bytesLeft -= len; |
---|
249 | } |
---|
250 | |
---|
251 | if( success ) |
---|
252 | SHA1_Final( setme, &sha ); |
---|
253 | |
---|
254 | return success; |
---|
255 | } |
---|
256 | |
---|
257 | int |
---|
258 | tr_ioTestPiece( const tr_torrent * tor, |
---|
259 | int pieceIndex ) |
---|
260 | { |
---|
261 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
262 | const int recalculated = recalculateHash( tor, pieceIndex, hash ); |
---|
263 | return recalculated && !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH ); |
---|
264 | } |
---|
265 | |
---|