1 ////////////////////////////////////////////////////////////////////////
2 // FILE: main.cpp
3 // AUTHOR: Johannes Winkelmann, jw@tks6.net
4 // COPYRIGHT: (c) 2002-2005 by Johannes Winkelmann
5 // ---------------------------------------------------------------------
6 // This program is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 ////////////////////////////////////////////////////////////////////////
11
12 #include <iostream>
13 #include <string>
14 #include <cstdlib>
15 using namespace std;
16
17 #include <unistd.h>
18
19 #include "httpup.h"
20 #include "fileutils.h"
21 #include "httpupargparser.h"
22
23
24 int main(int argc, char** argv)
25 {
26 HttpupArgparser htap;
27 htap.parse(argc, argv);
28
29 HttpUp::ExecType execType = HttpUp::TYPE_SYNC;
30
31 if (htap.command() == HttpupArgparser::CMD_SYNC) {
32 execType = HttpUp::TYPE_SYNC;
33 } else if (htap.command() == HttpupArgparser::CMD_COPY) {
34 execType = HttpUp::TYPE_COPY;
35 } else if (htap.command() == HttpupArgparser::CMD_LIST) {
36 string dir = ".";
37 if (htap.otherArguments().size() > 0) {
38 dir = htap.otherArguments()[0];
39 }
40 FileUtils::listFiles(dir);
41 exit(0);
42 } else {
43 cerr << "Supported commands so far:\n"
44 << " sync [<url#fragment>] [<target dir>]\n"
45 << " list [<target dir>]\n"
46 << "\n"
47 << " if target dir is omitted, the "
48 << " current working directory is used.\n"
49 << " if url is omitted, it is read from the .httpup-urlinfo file"
50 << endl;
51 exit(-1);
52 }
53
54 string url = "";
55 string fragment = "";
56 if (htap.otherArguments().size() > 0) {
57 url = htap.otherArguments()[0];
58 } else {
59 FILE* fp = fopen(HttpUp::URLINFO.c_str(), "r");
60 if (!fp) {
61 cerr << "Couldn't find " << HttpUp::URLINFO
62 << " in current working directory. "
63 << endl;
64 exit(-1);
65 }
66 char urlBuf[512];
67 fgets(urlBuf, 512, fp);
68 url = urlBuf;
69 }
70
71 if (!htap.isSet(HttpupArgparser::OPT_ENCODE)) {
72 string::size_type pos = url.find("#");
73 if (pos != string::npos) {
74 fragment = url.substr(pos+1);
75 url = url.substr(0, pos);
76 }
77
78 if (fragment[fragment.size()-1] == '/') {
79 fragment = fragment.substr(0, fragment.length()-1);
80 }
81 }
82 if (url[url.size()-1] != '/') {
83 url += '/';
84 }
85
86 string target = "";
87 if (htap.otherArguments().size() > 1) {
88 target = htap.otherArguments()[1];
89 } else {
90 char* pwd = new char[256];
91 if (getcwd(pwd, 265) == NULL) {
92 delete pwd;
93 pwd = new char[1024];
94 if (getcwd(pwd, 1024) == NULL) {
95 cerr << "Path longer then 1024 characters; exiting" << endl;
96 exit(-1);
97 }
98 }
99
100 target = pwd;
101 delete pwd;
102 }
103 if (target[target.size()-1] != '/') {
104 target += '/';
105 }
106
107 string repoFile = "";
108 if (htap.isSet(HttpupArgparser::OPT_REPOFILE)) {
109 repoFile = htap.getOptionValue(HttpupArgparser::OPT_REPOFILE);
110 }
111
112 bool verifyMd5 = false;
113 if (htap.isSet(HttpupArgparser::OPT_VERIFY_MD5)) {
114 verifyMd5 = true;
115 }
116
117 #if 0
118 cout << "Sync "
119 << (fragment==""?"all":fragment) << " from "
120 << url << " to "
121 << target << endl;
122 #endif
123
124 HttpUp httpup(htap, url, target, fragment, repoFile, verifyMd5);
125 return httpup.exec(execType);
126 }
|