File: pcopy.c

package info (click to toggle)
pcopy 1.5-1
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 160 kB
  • ctags: 74
  • sloc: ansic: 402; sh: 186; makefile: 77
file content (558 lines) | stat: -rw-r--r-- 9,913 bytes parent folder | download | duplicates (2)
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
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
325
326
327
328
329
330
331
332
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
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
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
/*
** pcopy.c
**
** A multithreaded (raw) disk copying program.
**
** Copyright (c) 1997-2003 Peter Eriksson <pen@lysator.liu.se>
**
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include "config.h"

#define _LARGEFILE64_SOURCE 1

#ifdef HAVE_LIBTHREAD
#  define _REENTRANT
#  include <thread.h>
#endif

#ifdef HAVE_LIBPTHREAD
/* #  define _POSIX_C_SOURCE 199506L */
#  include <pthread.h>
#endif


#include <stdio.h>
#include <assert.h>
#include <errno.h>

#include <sys/types.h>
#include <unistd.h>

#include <stdlib.h>
#include <fcntl.h>


char *srcdev=NULL;
char *dstdev=NULL;
int freq = 5;
time_t last = 0;


#ifndef HAVE_LIBPTHREAD
#ifdef HAVE_LIBTHREAD

/* Partial pthreads emulation using UI threads */
#define pthread_t	thread_t
#define pthread_mutex_t	mutex_t
#define pthread_cond_t	cond_t

#define pthread_create(tp,at,st,arg) 	thr_create(NULL,0,st,arg,0,tp)
#define pthread_join(tid,sp) 		thr_join(tid,NULL,sp)
				       
#define pthread_mutex_init(mtx,ma)	mutex_init(mtx, USYNC_THREAD, NULL)
#define pthread_cond_init(cv,ca)	cond_init(cv, USYNC_THREAD, NULL)

#define pthread_mutex_lock(mtx)		mutex_lock(mtx)
#define pthread_mutex_unlock(mtx)	mutex_unlock(mtx)

#define pthread_cond_wait(cv,mtx)	cond_wait(cv,mtx)
#define pthread_cond_signal(cv)		cond_signal(cv)

#else

#error Must have Pthreads or UI Threads!

#endif
#endif

#if SIZEOF_LONG == 8
#  define lseek64 lseek
#  define off64_t unsigned long
#else
#  ifndef HAVE_LSEEK64
#    ifdef HAVE_LLSEEK
#      define lseek64 llseek
#      define off64_t offset_t
#    else
/* No lseek64 or llseek function - use 32bit version */
#      define lseek64 lseek
#      define off64_t unsigned long
#    endif
#  endif
#endif


int buffer_size = 1024*1024;
int queue_size = 8;
int dot_mode = 0;
time_t start;
int silent = 0;


typedef struct buffer
{
    off64_t pos;
    int len;
    int size;
    char *buf;
} buffer_t;


typedef struct queue
{
    pthread_mutex_t mutex;
    pthread_cond_t more;
    pthread_cond_t less;
    
    int occupied;
    int nextin;
    int nextout;
    int bsize;

    void **buf;
} queue_t;


int read_fd;
int write_fd;
off64_t read_pos;
off64_t write_pos;

queue_t *write_q;
queue_t *free_q;


int no_write = 0;



int x_pread64(off64_t pos, char *buf, int len)
{
    int res;
    int attempt;
    

    attempt = 0;
    do
    {
	if (read_pos != pos)
	    if (lseek64(read_fd, read_pos = pos, SEEK_SET) < 0)
		return -1;
	
	res = read(read_fd, buf, len);
    } while (res < 0 && attempt++ < 4);

    if (res > 0)
	read_pos += res;
    
    return res;
}

int x_pwrite64(off64_t pos, char *buf, int len)
{
    int res;


    if (no_write)
    {
	res = len;
	write_pos = pos + len;
    }
    else
    {
	if (write_pos =! pos)
	    if (lseek64(write_fd, write_pos = pos, SEEK_SET) < 0)
		return -1;
	
	res = write(write_fd, buf, len);
	if (res > 0)
	    write_pos += res;
    }
    
    return res;
}


queue_t *q_create(int size)
{
    queue_t *qp;

    qp = (queue_t *) malloc(sizeof(*qp));

    pthread_mutex_init(&qp->mutex, NULL);
    pthread_cond_init(&qp->more, NULL);
    pthread_cond_init(&qp->less, NULL);

    qp->bsize = size;
    qp->occupied = 0;
    qp->nextin = 0;
    qp->nextout = 0;

    qp->buf = calloc(qp->bsize, sizeof(qp->buf[0]));
    return qp;
}


void q_push(queue_t *qp, void *item)
{
    pthread_mutex_lock(&qp->mutex);

    while (qp->occupied >= qp->bsize)
	pthread_cond_wait(&qp->less, &qp->mutex);

    assert(qp->occupied < qp->bsize);

    qp->buf[qp->nextin++] = item;
    qp->nextin %= qp->bsize;
    qp->occupied++;

    pthread_cond_signal(&qp->more);
    pthread_mutex_unlock(&qp->mutex);
}


void *q_pop(queue_t *qp)
{
    void *item;


    pthread_mutex_lock(&qp->mutex);
    while (qp->occupied <= 0)
	pthread_cond_wait(&qp->more, &qp->mutex);

    assert(qp->occupied > 0);
  
    item = qp->buf[qp->nextout++];
    qp->nextout %= qp->bsize;
    qp->occupied--;

    pthread_cond_signal(&qp->less);
    pthread_mutex_unlock(&qp->mutex);

    return item;
}


buffer_t *b_create(int size)
{
    buffer_t *bp;


    bp = (buffer_t *) malloc(sizeof(*bp));
    bp->pos = 0;
    bp->len = 0;
    bp->size = size;
    bp->buf = malloc(size);

    return bp;
}

void
spin(time_t now)
{
    static char *spintab = "-\\|/";
    static int pos = 0;
    static time_t last = 0;
    

    if (now == last)
	return;

    last = now;
    putchar(spintab[pos]);
    putchar('\b');
    fflush(stdout);
    pos = (pos+1)&3;
}

    

void *writer(void *foo)
{
    buffer_t *bp;
    long long written = 0;
    long long last_written = 0;
    int len;

    
    while (bp = q_pop(write_q))
    {
	if (x_pwrite64(bp->pos, bp->buf, bp->len) != bp->len)
	{
	    if (!silent)
		putc('\n', stderr);
	    
	    fprintf(stderr,
		    "pcopy: %s: Error: Write of %d bytes failed at "
#if SIZEOF_LONG == 8
		    "%ld"
#else
		    "%lld"
#endif
		    ": %s\n",
		    dstdev, bp->len, bp->pos,
		    strerror(errno));
	    exit(1);
	}

	written += bp->len;
	
	bp->pos = 0;
	bp->len = 0;
	q_push(free_q, bp);

	if (!silent)
	{
	    if (dot_mode)
	    {
		putchar('.');
		fflush(stdout);
	    }
	    else
	    {
		time_t now;
		long long mbs;
		
		
		time(&now);
		len = now - start;
		if (len <= 0)
		    len = 1;
		
		if (now > last+freq)
		{
		    mbs = (written-last_written)/freq/1024/1024;

		    last_written = written;
			
#if SIZEOF_LONG == 8
		    printf("\rCopied: %ld MB (%ld MB/sec)     ",
			   written/1024/1024,
			   mbs);
#else
		    printf("\rCopied: %lld MB (%lld MB/sec)     ",
			   written/1024/1024,
			   mbs);
#endif
		    fflush(stdout);
		    last = now;
		}

		spin(now);
	    }
	}
    }

    return NULL;
}


void usage(void)
{
    fprintf(stderr, "Usage: pcopy [options] <source> <destination>\n");
    fprintf(stderr, "Options:\n");
    fprintf(stderr, "-s\tSilent mode\n");
    fprintf(stderr, "-q<num>\tQueue size\n");
    fprintf(stderr, "-u<num>\tUpdate frequency (in seconds)\n");
    fprintf(stderr, "-b<num>\tBuffer size (in KBytes)\n");
    fprintf(stderr, "-d\tShow progress as a series of dots\n");
}


int main(int argc, char *argv[])
{
    int i, size;
    pthread_t tid;
    off64_t pos;
    long long bytes, mbytes;
    int len;
    void *status;
    buffer_t *bp;
    time_t stop;
    
    
    for (i = 1; i < argc && argv[i][0] == '-'; i++)
	switch (argv[i][1])
	{
	  case 's':
	    silent = 1;
	    break;
	    
	  case 'n':
	    no_write = 1;
	    break;
	    
	  case 'd':
	    dot_mode = 1;
	    break;
	    
	  case 'q':
	    queue_size = atoi(argv[i]+2);
	    break;

	  case 'u':
	    freq = atoi(argv[i]+2);
	    break;

	  case 'b':
	    buffer_size = atoi(argv[i]+2)*1024;
	    break;

	  case 'h':
	  default:
	    usage();
	    exit(0);
	}
    
    if (i + 2 != argc)
    {
	usage();
	exit(1);
    }

    srcdev=argv[i];
    dstdev=argv[i+1];
    
    if (!silent)
    {
	printf("Copying from %s to %s (start in 3 seconds)", srcdev, dstdev);
	fflush(stdout);
	sleep(1);
	putchar('.');
	fflush(stdout);
	sleep(1);
	putchar('.');
	fflush(stdout);
	sleep(1);
	putchar('.');
	putchar('\n');
	fflush(stdout);
    }

#ifdef HAVE_LIBTHREAD
    thr_setconcurrency(4);
#endif
    
    read_fd = open(srcdev, O_RDONLY);
    if (read_fd < 0)
    {
	fprintf(stderr, "pcopy: %s: Source device open failed: %s\n",
		srcdev, strerror(errno));
	exit(1);
    }
  
    write_fd = open(dstdev, O_WRONLY+O_SYNC);
    if (write_fd < 0)
    {
	fprintf(stderr, "pcopy: %s: Destination device open failed: %s\n",
		dstdev, strerror(errno));
	exit(1);
    }
    
    
    free_q = q_create(queue_size);
    write_q = q_create(queue_size);

    for (i = 0; i < queue_size; i++)
	q_push(free_q, b_create(buffer_size));

    pthread_create(&tid, NULL, writer, NULL);
    pos = 0;
    bytes = 0;

    time(&start);
    time(&last);
  Loop:
    bp = q_pop(free_q);
    size = bp->size;

    while ((len = x_pread64(pos, bp->buf, size)) < 0 && errno == EIO)
    {
	/* XXX: This code more or less assumes that buffer_size is
	   an even multiple of 2 */
	
	if (size > 512)
	{
	    if (!silent)
		putc('\n', stderr);
	    
	    fprintf(stderr,
		    "pcopy: %s: Warning: Problems reading %d bytes at "
#if SIZEOF_LONG == 8
		    "%ld"
#else
		    "%lld"
#endif
		    ": %s\n",
		    srcdev, size, pos, strerror(errno));
	    
	    size >>= 2; /* Try with a smaller size */
	}
	else
	{
	    /* Give up, and return a zero-filled sector */
	    fprintf(stderr,
		    "pcopy: %s: Error: Read of %d bytes at "
#if SIZEOF_LONG == 8
		    "%ld"
#else
		    "%lld"
#endif
		    " failed: %s\n",
		    srcdev, size, pos, strerror(errno));
	    
	    len = size;
	    memset(bp->buf, 0, len);
	    break;
	}
    }

    if (len == 0)
	goto End;
  
    bp->len = len;
    bp->pos = pos;
  
    pos += len;

    bytes += len;
    
    q_push(write_q, bp);
    goto Loop;

  End:
    q_push(write_q, NULL);
    pthread_join(tid, &status);
    
    time(&stop);
    len = stop-start;
    if (len <= 0)
	len = 1;
    
    mbytes = bytes/1024/1024;

    if (!silent)
	putchar('\n');
    
#if SIZEOF_LONG == 8
    printf("%s: Done. Copied %ld MB in %d seconds (%ld MB/s)\n",
	   srcdev, mbytes, len, mbytes/len);
#else
    printf("%s: Done. Copied %lld MB in %d seconds (%lld MB/s)\n",
	   srcdev, mbytes, len, mbytes/len);
#endif
    exit(0);
}