[go: up one dir, main page]

Menu

[r73]: / client / upload.cpp  Maximize  Restore  History

Download this file

105 lines (85 with data), 2.1 kB

  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
#include <fstream>
#include <fsclient.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <termios.h>
#include <string.h>
#include <errno.h>
#include <iostream>
using namespace std;
using namespace cb;
int upload(const char* file, const char* dst = NULL)
{
timeval t1, t2;
gettimeofday(&t1, 0);
ifstream ifs(file);
ifs.seekg(0, ios::end);
long long int size = ifs.tellg();
ifs.seekg(0);
cout << "uploading " << file << " of " << size << " bytes" << endl;
File* fh = Sector::createFileHandle();
if (NULL == fh)
return -1;
char* rname;
if (NULL != dst)
{
rname = (char*)dst;
}
else
{
rname = (char*)file;
for (int i = strlen(file); i >= 0; -- i)
{
if ('/' == file[i])
{
rname = (char*)file + i + 1;
break;
}
}
}
char cert[1024];
cert[0] = '\0';
if (fh->open(rname, 2, cert) < 0)
{
cout << "ERROR: unable to connect to server." << endl;
return -1;
}
if (0 != strlen(cert))
{
cout << "file owner certificate: " << cert << endl;
ofstream ofs((string(rname) + ".cert").c_str());
ofs << cert << endl;
ofs.close();
}
bool finish = true;
if (fh->upload(file) < 0)
finish = false;
fh->close();
Sector::releaseFileHandle(fh);
if (finish)
{
gettimeofday(&t2, 0);
float throughput = size * 8.0 / 1000000.0 / ((t2.tv_sec - t1.tv_sec) + (t2.tv_usec - t1.tv_usec) / 1000000.0);
cout << "Uploading accomplished! " << "AVG speed " << throughput << " Mb/s." << endl << endl ;
}
else
cout << "Uploading failed! Please retry. " << endl << endl;
return 1;
}
int main(int argc, char** argv)
{
if ((4 != argc) && (5 != argc))
{
cout << "usage: upload <ip> <port> <src file> [dst file]" << endl;
return 0;
}
Sector::init(argv[1], atoi(argv[2]));
if (5 == argc)
upload(argv[3], argv[4]);
else
upload(argv[3]);
Sector::close();
return 1;
}