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
|
#include <stdio.h>
#include <stdarg.h>
#include <time.h>
#include <inttypes.h>
#include "mfs.h"
#include "vstream-client.h"
#define CHUNK ( 1024 * 1024 )
// on cygwin, we have large file support already, and no fopen64.
#ifdef __CYGWIN__
#define fopen64 fopen
#endif
#ifdef __APPLE__
#define fopen64 fopen
#endif
void vstream_error( const char *format, ... )
{
va_list va;
va_start( va, format );
vfprintf( stderr, format, va );
va_end( va );
}
int usage(char *name)
{
fprintf(stderr, "Simple Usage:\n");
fprintf(stderr, "list available streams: %s hostname\n", name);
fprintf(stderr, "list available streams (long format): %s hostname -l\n", name);
fprintf(stderr, "download a stream: %s hostname fsid [-o filename] [-a offset]\n", name);
fprintf(stderr, "\n");
fprintf(stderr, "URL Usage:\n");
fprintf(stderr, "list available streams: %s tivo://hostname/list\n", name);
fprintf(stderr, "list available streams (long format): %s tivo://hostname/llist\n", name);
fprintf(stderr, "download a stream: %s tivo://hostname/fsid [-o filename] [-a offset]\n", name);
fprintf(stderr, "\n");
fprintf(stderr, "offsets are specified in megabytes.\n");
fprintf(stderr, "filename, if unspecified, defaults to <fsid>.ty\n");
fprintf(stderr, "\n");
return 1;
}
int main( int argc, char *argv[] )
{
int64_t offset = 0, size, count;
unsigned char buffer[ CHUNK ];
FILE *output;
char *filename = NULL, *hostname = NULL, *fsid = NULL;
int megs, seconds, start_time;
char *me;
int dashes = 0, ll = 0;
me = strrchr(argv[0], '/');
if (!me) me = strrchr(argv[0], '\\'); else me++;
if (!me) me = argv[0]; else me++;
argc--;
argv++;
while (argc) {
if (!strcmp(argv[0], "--")) {
dashes = 1;
} else if (!dashes && !strcmp(argv[0], "-l")) {
ll = 1;
} else if (!dashes && !strcmp(argv[0], "-o")) {
argv++; argc--;
if (!argc) return usage(me);
filename = strdup(argv[0]);
} else if (!dashes && !strcmp(argv[0], "-a")) {
argv++; argc--;
if (!argc) return usage(me);
offset = ((int64_t)atol(argv[0])) << 20;
} else if (!strncmp(argv[0], "tivo://", 7)) {
if (hostname) return usage(me);
hostname = argv[0] + 7;
if (!hostname[0]) return usage(me);
fsid = strchr(hostname, '/');
if (!fsid) return usage(me);
*fsid = '\0';
fsid++;
if (strchr(fsid, '/')) return usage(me);
} else {
if (!hostname) hostname = argv[0];
else if (hostname && !fsid) fsid = argv[0];
else return usage(me);
}
argc--;
argv++;
}
if (!hostname) return usage(me);
if (vstream_open_socket(hostname)) {
fprintf(stderr, "%s: connection error.\n", me);
return 1;
}
if (!fsid || !strcmp(fsid, "list")) {
vstream_list_streams(0);
return 0;
} else if (!strcmp(fsid, "llist") || ll) {
vstream_list_streams(1);
return 0;
}
if (!filename) {
int len = strlen(fsid) + 4;
filename = malloc(len);
snprintf(filename, len, "%s.ty", fsid);
}
{
if (vstream_start()) {
fprintf(stderr, "%s: Cryptic internal error 1.\n", me);
return 1;
}
if (vstream_startstream(fsid)) {
fprintf(stderr, "%s: Cryptic internal error 2.\n", me);
return 1;
}
size = vstream_streamsize();
megs = 0;
fprintf(stderr, "(%s) size %"PRId64" MB, getting %"PRId64" MB\n", fsid, size >> 20, (size - offset) >> 20);
if (strcmp(filename, "-")!=0) output = fopen64(filename, "w" );
else output = stdout;
if (!output) {
fprintf(stderr, "%s: File creation failed.\n", me);
return 1;
}
start_time = time(NULL);
while (offset < size)
{
count = CHUNK;
if (offset + CHUNK > size)
count = size - offset;
count = vstream_load_chunk(fsid, buffer, count, offset);
offset += count;
fwrite(buffer, 1, count, output);
megs++;
seconds = time(NULL) - start_time;
if (seconds) fprintf(stderr, "\r%5d MB %5d kb/s", megs, (megs << 10) / seconds);
fflush(stderr);
}
fclose(output);
fprintf(stderr, "\nDone.\n");
}
free(filename);
return 0;
}
|