Many hyperlinks are disabled.
Use anonymous login
to enable hyperlinks.
Overview
Comment: | Changed the way "file_sync" works to a less latency-driven approach. |
---|---|
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
223caea2dc39ed80da8d9f1214f3602b |
User & Date: | rkeene 2006-01-30 04:43:13 |
Context
2006-03-11
| ||
22:02 | Changed file_sync to only lseek() when needed Added strsep() replacement code Modified build process to create Win32 binaries on release Added some comments to the sample configuration file Added stub for new configuration option "NotifyServer" Made backuppcd-passwd prompt for a password to be read froms stdin check-in: 33d0a0970f user: rkeene tags: trunk | |
2006-01-30
| ||
04:43 | Changed the way "file_sync" works to a less latency-driven approach. check-in: 223caea2dc user: rkeene tags: trunk | |
2006-01-23
| ||
09:59 | Changed version to BackupPCd 0.1.2 check-in: 4e2c1a856d user: rkeene tags: trunk | |
Changes
Changes to tools/file_sync.c.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | #include "compat.h" #include "md4.h" #include "net.h" #ifndef O_LARGEFILE #define O_LARGEFILE 0 #endif uint64_t net_bytesout = 0; uint64_t net_bytesin = 0; extern char *optarg; extern int optind, opterr, optopt; static int print_help(int ret) { | > > > > > > > > > > > | | 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 | #include "compat.h" #include "md4.h" #include "net.h" #ifdef HAVE_SIGNAL_H #include <signal.h> #endif #ifndef HAVE_LIMITS_H #include <limits.h> #endif #ifndef INT_MAX #define INT_MAX (0x7fffffff) #endif #ifndef O_LARGEFILE #define O_LARGEFILE 0 #endif uint64_t net_bytesout = 0; uint64_t net_bytesin = 0; extern char *optarg; extern int optind, opterr, optopt; static int print_help(int ret) { fprintf(stderr, "Usage: file_sync -t <host> <port> <file> [<blocksize>]\n"); fprintf(stderr, "Usage: file_sync -r <port> <file>\n"); return(ret); } #if 0 static void print_hash(FILE *fp, const unsigned char *hash) { int i; |
︙ | ︙ | |||
78 79 80 81 82 83 84 | if (bytestowrite >= INT_MAX) { bytestowrite = INT_MAX - 1; } write_ret = write(fd, buf, bytestowrite); if (write_ret < 0) { | > | > < < < < < < < < < < < < < < < < < < < < < < < | 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 | if (bytestowrite >= INT_MAX) { bytestowrite = INT_MAX - 1; } write_ret = write(fd, buf, bytestowrite); if (write_ret < 0) { if (retval == 0) { retval = -1; } break; } if (write_ret == 0) { break; } count -= write_ret; buf += write_ret; retval += write_ret; } return(retval); } static ssize_t read_large_net(int fd, void *buf, size_t count) { ssize_t retval; retval = read_large(fd, buf, count); |
︙ | ︙ | |||
140 141 142 143 144 145 146 | if (retval > 0) { net_bytesout += retval; } return(retval); } | | | | > < > > > > > > > > > > > > < < | | > | | | > > > > > > > > > > > > > > > > > > > > > > > > > > | > > > > > | | | | | | | | > > | | > > > > > | | > > | 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 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 | if (retval > 0) { net_bytesout += retval; } return(retval); } int sync_transmit(const char *host, int port, const char *file, uint32_t blocksize) { rsaref_MD4_CTX mdctx; unsigned char md4buf[16]; uint64_t filesize; uint32_t blockok_size, blockidx; uint8_t *blockok, blockok_val; ssize_t read_ret, write_ret; char *buf; off_t lseek_ret; int retval = 0; int sockfd; int fd; int i; fd = open(file, O_RDONLY | O_LARGEFILE); if (fd < 0) { CHECKPOINT; return(-1); } lseek_ret = lseek(fd, 0, SEEK_END); if (lseek_ret < 0) { close(fd); CHECKPOINT; return(-1); } filesize = lseek_ret; blockok_size = filesize / (8 * blocksize); if ((filesize % (8 * blocksize)) != 0) { blockok_size++; } blockok = malloc(blockok_size); if (!blockok) { close(fd); CHECKPOINT; return(-1); } lseek(fd, 0, SEEK_SET); sockfd = net_connect_tcp(host, port); if (sockfd < 0) { close(fd); CHECKPOINT; return(-1); } buf = calloc(blocksize, 1); if (!buf) { close(fd); close(sockfd); CHECKPOINT; return(-1); } filesize = htonll(filesize); blocksize = htonl(blocksize); write_ret = write_large_net(sockfd, &filesize, sizeof(filesize)); if (write_ret != sizeof(filesize)) { close(fd); close(sockfd); CHECKPOINT; return(-1); } write_ret = write_large_net(sockfd, &blocksize, sizeof(blocksize)); if (write_ret != sizeof(blocksize)) { close(fd); close(sockfd); CHECKPOINT; return(-1); } filesize = ntohll(filesize); blocksize = ntohl(blocksize); while (1) { read_ret = read_large(fd, buf, blocksize); if (read_ret < 0) { retval = -1; CHECKPOINT; break; } if (read_ret == 0) { CHECKPOINT; break; } rsaref_MD4Init(&mdctx); rsaref_MD4Update(&mdctx, buf, read_ret); rsaref_MD4Final(md4buf, &mdctx); write_ret = write_large_net(sockfd, md4buf, sizeof(md4buf)); if (write_ret != sizeof(md4buf)) { retval = -1; CHECKPOINT; break; } } if (retval != -1) { read_ret = read_large_net(sockfd, blockok, blockok_size); if (read_ret != blockok_size) { retval = -1; CHECKPOINT; } } if (retval != -1) { for (blockidx = 0; blockidx < blockok_size; blockidx++) { for (i = 0; i < 8; i++) { blockok_val = blockok[blockidx] & (1<<i); if (!blockok_val) { lseek_ret = lseek(fd, (blockidx * 8 + i) * blocksize, SEEK_SET); if (lseek_ret != ((blockidx * 8 + i) * blocksize)) { retval = -1; CHECKPOINT; break; } if (lseek_ret >= filesize) { continue; } read_ret = read_large(fd, buf, blocksize); if (read_ret != blocksize) { if ((lseek_ret + read_ret) != filesize || read_ret < 0) { SPOTVAR_I(blockidx); SPOTVAR_LLU(lseek_ret); SPOTVAR_LLU(read_ret); SPOTVAR_LLU(filesize); retval = -1; CHECKPOINT; break; } } write_ret = write_large_net(sockfd, buf, read_ret); if (write_ret != read_ret) { SPOTVAR_LLU(write_ret); SPOTVAR_LLU(read_ret); retval = -1; CHECKPOINT; break; } } } if (retval == -1) { break; } } } close(fd); close(sockfd); return(retval); } int sync_receive(int port, const char *file) { rsaref_MD4_CTX mdctx; unsigned char md4buf[16], check_md4buf[16]; uint64_t filesize, filesize_s; uint32_t blocksize; uint32_t blockok_size, blockidx = 0; uint8_t *blockok, blockok_val; ssize_t cur_read_ret, read_ret, write_ret; size_t bytestowrite; off_t lseek_ret; char *buf; int sockfd, master_sockfd; int retval = 0; int fd; int skipbytes; int i = 0; fd = open(file, O_RDWR | O_CREAT | O_LARGEFILE, 0600); if (fd < 0) { CHECKPOINT; return(-1); } |
︙ | ︙ | |||
292 293 294 295 296 297 298 | if (sockfd < 0) { close(fd); close(master_sockfd); CHECKPOINT; return(-1); } | | | | > > > > > > > > > > > > > > | 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 | if (sockfd < 0) { close(fd); close(master_sockfd); CHECKPOINT; return(-1); } read_ret = read_large_net(sockfd, &filesize, sizeof(filesize)); if (read_ret != sizeof(filesize)) { close(fd); close(sockfd); close(master_sockfd); CHECKPOINT; return(-1); } read_ret = read_large_net(sockfd, &blocksize, sizeof(blocksize)); if (read_ret != sizeof(blocksize)) { close(fd); close(sockfd); close(master_sockfd); CHECKPOINT; return(-1); } filesize_s = filesize = ntohll(filesize); blocksize = ntohl(blocksize); blockok_size = filesize / (8 * blocksize); if ((filesize % (8 * blocksize)) != 0) { blockok_size++; } blockok = malloc(blockok_size); if (!blockok) { close(fd); close(sockfd); close(master_sockfd); CHECKPOINT; return(-1); } buf = calloc(blocksize, 1); if (!buf) { close(fd); close(sockfd); close(master_sockfd); CHECKPOINT; |
︙ | ︙ | |||
347 348 349 350 351 352 353 | skipbytes = 0; } rsaref_MD4Init(&mdctx); rsaref_MD4Update(&mdctx, buf, read_ret); rsaref_MD4Final(md4buf, &mdctx); | | | | | > | > | > > > > | < > | | | | | < < | | > > > > | | | | | | | > > > | > | > > | > > | | | | | | | | | | | | | | > > > | > | > > | > > > > > > | | 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 | skipbytes = 0; } rsaref_MD4Init(&mdctx); rsaref_MD4Update(&mdctx, buf, read_ret); rsaref_MD4Final(md4buf, &mdctx); read_ret = read_large_net(sockfd, check_md4buf, sizeof(check_md4buf)); if (read_ret != sizeof(check_md4buf)) { retval = -1; CHECKPOINT; break; } if (memcmp(check_md4buf, md4buf, sizeof(md4buf)) == 0) { blockok_val = 1; } else { blockok_val = 0; } if (i == 0 || i == 8) { if (i == 8) { blockidx++; } i = 0; blockok[blockidx] = 0; } blockok[blockidx] |= (blockok_val<<i); i++; filesize -= bytestowrite; } if (retval != -1) { write_ret = write_large_net(sockfd, blockok, blockok_size); if (write_ret != blockok_size) { retval = -1; } } if (retval != -1) { for (blockidx = 0; blockidx < blockok_size; blockidx++) { for (i = 0; i < 8; i++) { blockok_val = blockok[blockidx] & (1<<i); if (!blockok_val) { lseek_ret = lseek(fd, (blockidx * 8 + i) * blocksize, SEEK_SET); if (lseek_ret != ((blockidx * 8 + i) * blocksize)) { retval = -1; CHECKPOINT; break; } if (lseek_ret >= filesize_s) { continue; } read_ret = read_large_net(sockfd, buf, blocksize); if (read_ret != blocksize) { if ((lseek_ret + read_ret) != filesize_s || read_ret < 0) { SPOTVAR_I(blockidx); SPOTVAR_LLU(lseek_ret); SPOTVAR_LLU(read_ret); SPOTVAR_LLU(filesize_s); retval = -1; CHECKPOINT; break; } } write_ret = write_large(fd, buf, read_ret); if (write_ret != read_ret) { retval = -1; CHECKPOINT; break; } } } if (retval == -1) { break; } } } close(fd); close(sockfd); close(master_sockfd); return(retval); } int main(int argc, char **argv) { char *mode, *host, *port_str, *file, *blocksize_str; uint32_t blocksize; int func_ret; int retval = EXIT_SUCCESS; int port; if (argc < 2) { return(print_help(EXIT_FAILURE)); } #ifdef HAVE_SIGNAL signal(SIGPIPE, SIG_IGN); #endif mode = argv[1]; if (strcmp(mode, "-t") == 0) { if (argc < 5 || argc > 6) { return(print_help(EXIT_FAILURE)); } host = argv[2]; port_str = argv[3]; file = argv[4]; if (argc == 6) { blocksize_str = argv[5]; blocksize = strtoul(blocksize_str, NULL, 0); } else { blocksize = 256 * 1024; } port = strtoul(port_str, NULL, 10); func_ret = sync_transmit(host, port, file, blocksize); if (func_ret < 0) { fprintf(stderr, "Failed.\n"); retval = 1; } } else if (strcmp(mode, "-r") == 0) { if (argc != 4) { return(print_help(EXIT_FAILURE)); |
︙ | ︙ |