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 7142 2008-11-23 17:27:39Z 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 | /**** |
---|
34 | ***** Low-level IO functions |
---|
35 | ****/ |
---|
36 | |
---|
37 | #ifdef WIN32 |
---|
38 | #if defined(read) |
---|
39 | #undef read |
---|
40 | #endif |
---|
41 | #define read _read |
---|
42 | |
---|
43 | #if defined(write) |
---|
44 | #undef write |
---|
45 | #endif |
---|
46 | #define write _write |
---|
47 | #endif |
---|
48 | |
---|
49 | enum { TR_IO_READ, TR_IO_WRITE }; |
---|
50 | |
---|
51 | static int64_t |
---|
52 | tr_lseek( int fd, int64_t offset, int whence ) |
---|
53 | { |
---|
54 | #if defined(_LARGEFILE_SOURCE) |
---|
55 | return lseek64( fd, (off64_t)offset, whence ); |
---|
56 | #elif defined(WIN32) |
---|
57 | return _lseeki64( fd, offset, whence ); |
---|
58 | #else |
---|
59 | return lseek( fd, (off_t)offset, whence ); |
---|
60 | #endif |
---|
61 | } |
---|
62 | |
---|
63 | /* returns 0 on success, or an errno on failure */ |
---|
64 | static int |
---|
65 | readOrWriteBytes( const tr_torrent * tor, |
---|
66 | int ioMode, |
---|
67 | tr_file_index_t fileIndex, |
---|
68 | uint64_t fileOffset, |
---|
69 | void * buf, |
---|
70 | size_t buflen ) |
---|
71 | { |
---|
72 | const tr_info * info = &tor->info; |
---|
73 | const tr_file * file = &info->files[fileIndex]; |
---|
74 | |
---|
75 | typedef size_t ( *iofunc )( int, void *, size_t ); |
---|
76 | iofunc func = ioMode == |
---|
77 | TR_IO_READ ? (iofunc)read : (iofunc)write; |
---|
78 | char * path; |
---|
79 | struct stat sb; |
---|
80 | int fd = -1; |
---|
81 | int err; |
---|
82 | int fileExists; |
---|
83 | |
---|
84 | assert( fileIndex < info->fileCount ); |
---|
85 | assert( !file->length || ( fileOffset < file->length ) ); |
---|
86 | assert( fileOffset + buflen <= file->length ); |
---|
87 | |
---|
88 | path = tr_buildPath( tor->downloadDir, file->name, NULL ); |
---|
89 | fileExists = !stat( path, &sb ); |
---|
90 | tr_free( path ); |
---|
91 | |
---|
92 | if( !file->length ) |
---|
93 | return 0; |
---|
94 | |
---|
95 | if( ( ioMode == TR_IO_READ ) && !fileExists ) /* does file exist? */ |
---|
96 | err = errno; |
---|
97 | else if( ( fd = tr_fdFileCheckout ( tor->downloadDir, file->name, ioMode == TR_IO_WRITE, !file->dnd, file->length ) ) < 0 ) |
---|
98 | err = errno; |
---|
99 | else if( tr_lseek( fd, (int64_t)fileOffset, SEEK_SET ) == -1 ) |
---|
100 | err = errno; |
---|
101 | else if( func( fd, buf, buflen ) != buflen ) |
---|
102 | err = errno; |
---|
103 | else |
---|
104 | err = 0; |
---|
105 | |
---|
106 | if( ( !err ) && ( !fileExists ) && ( ioMode == TR_IO_WRITE ) ) |
---|
107 | tr_statsFileCreated( tor->session ); |
---|
108 | |
---|
109 | if( fd >= 0 ) |
---|
110 | tr_fdFileReturn( fd ); |
---|
111 | |
---|
112 | return err; |
---|
113 | } |
---|
114 | |
---|
115 | static int |
---|
116 | compareOffsetToFile( const void * a, |
---|
117 | const void * b ) |
---|
118 | { |
---|
119 | const uint64_t offset = *(const uint64_t*)a; |
---|
120 | const tr_file * file = b; |
---|
121 | |
---|
122 | if( offset < file->offset ) return -1; |
---|
123 | if( offset >= file->offset + file->length ) return 1; |
---|
124 | return 0; |
---|
125 | } |
---|
126 | |
---|
127 | void |
---|
128 | tr_ioFindFileLocation( const tr_torrent * tor, |
---|
129 | tr_piece_index_t pieceIndex, |
---|
130 | uint32_t pieceOffset, |
---|
131 | tr_file_index_t * fileIndex, |
---|
132 | uint64_t * fileOffset ) |
---|
133 | { |
---|
134 | const uint64_t offset = tr_pieceOffset( tor, pieceIndex, pieceOffset, 0 ); |
---|
135 | const tr_file * file; |
---|
136 | |
---|
137 | file = bsearch( &offset, |
---|
138 | tor->info.files, tor->info.fileCount, sizeof( tr_file ), |
---|
139 | compareOffsetToFile ); |
---|
140 | |
---|
141 | *fileIndex = file - tor->info.files; |
---|
142 | *fileOffset = offset - file->offset; |
---|
143 | |
---|
144 | assert( *fileIndex < tor->info.fileCount ); |
---|
145 | assert( *fileOffset < file->length ); |
---|
146 | assert( tor->info.files[*fileIndex].offset + *fileOffset == offset ); |
---|
147 | } |
---|
148 | |
---|
149 | /* returns 0 on success, or an errno on failure */ |
---|
150 | static int |
---|
151 | readOrWritePiece( const tr_torrent * tor, |
---|
152 | int ioMode, |
---|
153 | tr_piece_index_t pieceIndex, |
---|
154 | uint32_t pieceOffset, |
---|
155 | uint8_t * buf, |
---|
156 | size_t buflen ) |
---|
157 | { |
---|
158 | int err = 0; |
---|
159 | tr_file_index_t fileIndex; |
---|
160 | uint64_t fileOffset; |
---|
161 | const tr_info * info = &tor->info; |
---|
162 | |
---|
163 | if( pieceIndex >= tor->info.pieceCount ) |
---|
164 | return EINVAL; |
---|
165 | if( pieceOffset + buflen > tr_torPieceCountBytes( tor, pieceIndex ) ) |
---|
166 | return EINVAL; |
---|
167 | |
---|
168 | tr_ioFindFileLocation( tor, pieceIndex, pieceOffset, |
---|
169 | &fileIndex, &fileOffset ); |
---|
170 | |
---|
171 | while( buflen && !err ) |
---|
172 | { |
---|
173 | const tr_file * file = &info->files[fileIndex]; |
---|
174 | const uint64_t bytesThisPass = MIN( buflen, |
---|
175 | file->length - fileOffset ); |
---|
176 | |
---|
177 | err = readOrWriteBytes( tor, ioMode, |
---|
178 | fileIndex, fileOffset, buf, bytesThisPass ); |
---|
179 | buf += bytesThisPass; |
---|
180 | buflen -= bytesThisPass; |
---|
181 | ++fileIndex; |
---|
182 | fileOffset = 0; |
---|
183 | } |
---|
184 | |
---|
185 | return err; |
---|
186 | } |
---|
187 | |
---|
188 | int |
---|
189 | tr_ioRead( const tr_torrent * tor, |
---|
190 | tr_piece_index_t pieceIndex, |
---|
191 | uint32_t begin, |
---|
192 | uint32_t len, |
---|
193 | uint8_t * buf ) |
---|
194 | { |
---|
195 | return readOrWritePiece( tor, TR_IO_READ, pieceIndex, begin, buf, len ); |
---|
196 | } |
---|
197 | |
---|
198 | int |
---|
199 | tr_ioWrite( const tr_torrent * tor, |
---|
200 | tr_piece_index_t pieceIndex, |
---|
201 | uint32_t begin, |
---|
202 | uint32_t len, |
---|
203 | const uint8_t * buf ) |
---|
204 | { |
---|
205 | return readOrWritePiece( tor, TR_IO_WRITE, pieceIndex, begin, |
---|
206 | (uint8_t*)buf, |
---|
207 | len ); |
---|
208 | } |
---|
209 | |
---|
210 | /**** |
---|
211 | ***** |
---|
212 | ****/ |
---|
213 | |
---|
214 | static int |
---|
215 | recalculateHash( const tr_torrent * tor, |
---|
216 | tr_piece_index_t pieceIndex, |
---|
217 | uint8_t * setme ) |
---|
218 | { |
---|
219 | size_t bytesLeft; |
---|
220 | uint32_t offset = 0; |
---|
221 | int success = TRUE; |
---|
222 | SHA_CTX sha; |
---|
223 | |
---|
224 | assert( tor ); |
---|
225 | assert( setme ); |
---|
226 | assert( pieceIndex < tor->info.pieceCount ); |
---|
227 | |
---|
228 | SHA1_Init( &sha ); |
---|
229 | bytesLeft = tr_torPieceCountBytes( tor, pieceIndex ); |
---|
230 | |
---|
231 | while( bytesLeft ) |
---|
232 | { |
---|
233 | uint8_t buf[8192]; |
---|
234 | const int len = MIN( bytesLeft, sizeof( buf ) ); |
---|
235 | success = !tr_ioRead( tor, pieceIndex, offset, len, buf ); |
---|
236 | if( !success ) |
---|
237 | break; |
---|
238 | SHA1_Update( &sha, buf, len ); |
---|
239 | offset += len; |
---|
240 | bytesLeft -= len; |
---|
241 | } |
---|
242 | |
---|
243 | if( success ) |
---|
244 | SHA1_Final( setme, &sha ); |
---|
245 | |
---|
246 | return success; |
---|
247 | } |
---|
248 | |
---|
249 | int |
---|
250 | tr_ioTestPiece( const tr_torrent * tor, |
---|
251 | int pieceIndex ) |
---|
252 | { |
---|
253 | uint8_t hash[SHA_DIGEST_LENGTH]; |
---|
254 | const int recalculated = recalculateHash( tor, pieceIndex, hash ); |
---|
255 | return recalculated && !memcmp( hash, tor->info.pieces[pieceIndex].hash, SHA_DIGEST_LENGTH ); |
---|
256 | } |
---|
257 | |
---|