-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.cpp
179 lines (157 loc) · 4.67 KB
/
main.cpp
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
// @(#)main.cpp
// Time-stamp: <Julian Qian 2011-10-31 21:12:44>
// Copyright 2011 Julian Qian
// Version: $Id: main.cpp,v 0.0 2011/06/12 05:04:14 jqian Exp $
#include <fcntl.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <vector>
#include <fstream>
#include <algorithm>
#include "Logging.h"
#include "HttpGet.h"
#define AMAZON_PROXY "fints-g7g.amazon.com"
#define DOCUMENT_PATH "/mnt/us/documents/"
#define CONFIG_FILE "kindlepull.ini"
#define PID_FILE "/tmp/kindlepull.pid"
using std::string;
typedef std::vector<string> DownloadList;
typedef struct {
string fsn;
string whisper;
string log;
} PullConfig;
string trim_string(string str){
string::iterator itr = std::remove(str.begin(), str.end(), ' ');
str.erase(itr, str.end());
return str;
}
int get_config(const char* cfile, PullConfig& conf){
std::ifstream cstrm;
cstrm.open(cfile);
if(! cstrm.is_open()){
return -1;
}
string line;
while(std::getline(cstrm, line)){
// parse line
if(!line.empty() && line[0] != '#'){
size_t pos = line.find("=");
if(pos != string::npos){
string key = trim_string(line.substr(0, pos));
if(key == "fsn"){
conf.fsn = trim_string(line.substr(pos+1));
}else if(key == "whisper"){
conf.whisper = trim_string(line.substr(pos+1));
}else if(key == "log"){
conf.log = trim_string(line.substr(pos+1));
}
}
}
}
cstrm.close();
return 0;
}
int url2file(string url, string& file){
size_t start = url.find_last_of("/");
if(start != string::npos){
size_t end = url.find("?", start);
file = DOCUMENT_PATH;
file.append(url.substr(start + 1,
end == string::npos ? string::npos : end -start -1));
return 0;
}
return -1;
}
static int lock_file(int fd){
struct flock fl;
fl.l_type = F_WRLCK;
fl.l_start = 0;
fl.l_whence = SEEK_SET;
fl.l_len = 0;
return fcntl(fd, F_SETLK, (long)&fl);
}
static int already_running(void){
int fd = open(PID_FILE, O_RDWR|O_CREAT,
S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
if(fd < 0){
fprintf(stderr, "Can't open %s: %s.\n", PID_FILE, strerror(errno));
_exit(1);
}
if(lock_file(fd) < 0){
if (errno == EACCES || errno == EAGAIN){
close(fd);
fprintf(stderr, "Another instance is running?\n");
_exit(1);
}
fprintf(stderr, "Can't lock %s: %s.\n", PID_FILE, strerror(errno));
_exit(1);
}
int rval = ftruncate(fd,0);
char buf[16];
sprintf(buf,"%d", getpid());
rval = write(fd, buf, strlen(buf));
if(rval < 0){
close(fd);
fprintf(stderr, "Failed to write pid to %s.\n", PID_FILE);
_exit(1);
}
return fd;
}
int main(int argc, char *argv[]){
int lockfd = already_running();
PullConfig conf;
if(-1 == get_config(CONFIG_FILE, conf) &&
-1 == get_config("/mnt/us/" CONFIG_FILE, conf) &&
-1 == get_config("/mnt/us/kindlepull/" CONFIG_FILE, conf)){
fprintf(stderr, "failed to load conf file.\n");
return -1;
}
if(conf.fsn.empty() || conf.whisper.empty()){
fprintf(stderr, "please set fsn and whisper url.\n");
return -1;
}
Logger::instance()->level(Logger::DEBUG_LEVEL);
if(!conf.log.empty()){
Logger::instance()->open(conf.log);
}
// query what to download
DownloadList dlist;
{
string listText;
HttpGet qlist(AMAZON_PROXY, conf.fsn.c_str());
qlist.request(conf.whisper);
qlist.gunzipText(listText);
LDEBUG("gunzipText: %s", listText.c_str());
size_t start = 0, end;
while(1){
end = listText.find("\n", start);
if(end != string::npos){
string line = listText.substr(start, end - start);
if(line.find("http://") == 0){
dlist.push_back(line);
}
}else{
break;
}
start = end + 1; // skip delimiter \n
}
}
LDEBUG("begin download...: %d.", dlist.size());
// download them
for (DownloadList::iterator itr = dlist.begin();
itr != dlist.end(); ++itr) {
string file, url = *itr;
LINFO("download %s.\n", url.c_str());
// validate download file
HttpGet hget(AMAZON_PROXY, conf.fsn.c_str());
hget.request(url.c_str());
hget.gunzipFile(DOCUMENT_PATH);
}
// release lock file
close(lockfd);
unlink(PID_FILE);
return 0;
}