1#ifndef __BASE_H
2#define __BASE_H
3#include <iostream>
4#include <sys/socket.h>
5#include <netinet/in.h>
6#include <sys/select.h>
7#include <sys/time.h>
8#include <unistd.h>
9#include <pthread.h>
10#include <malloc.h>
11#include <string>
12#include <dirent.h>
13#include <curl/curl.h>
14#include <curl/easy.h>
15#include <sys/stat.h>
16#include <sys/statvfs.h>
17#include <utime.h>
18#include <errno.h>
19#include <signal.h>
20#include <unistd.h>
21#include <iconv.h>
22#include <cstring>
23#include <cstdlib>
24#include <stdarg.h>
25#include "list.h"
26
27
28using namespace std;
29
30#ifdef NVRAM_
31extern "C"
32{
33    #ifdef USE_TCAPI
34        #define WANDUCK "Wanduck_Command"
35        #define AICLOUD "AiCloud_Entry"
36        #include "libtcapi.h"
37        #include "tcapi.h"
38    #else
39        //#include <bcmnvram.h>
40        extern char * nvram_get(const char *name);
41        #define nvram_safe_get(name) (nvram_get(name) ? : "")
42    #endif
43        #include <shutils.h>
44}
45#endif
46
47#define __DEBUG__
48#ifdef __DEBUG__
49#define DEBUG(info, ...) printf(info,##__VA_ARGS__)
50#else
51#define DEBUG(info, ...)
52#endif
53
54#define __DEBUG1__
55#ifdef __DEBUG1__
56#define DEBUG1(info, ...) printf(info,##__VA_ARGS__)
57#else
58#define DEBUG1(info, ...)
59#endif
60
61#define __FTPDEBUG__
62
63#ifdef __FTPDEBUG__
64#define _show(info, ...) printf(info,##__VA_ARGS__)
65#else
66#define _show(info, ...)
67#endif
68
69/* log struct */
70typedef struct LOG_STRUCT
71{
72    uint8_t  status;
73    char  error[512];
74    float  progress;
75    char path[512];
76}Log_struc;
77
78typedef struct Mult_Rule
79{
80    int rules;
81    char base_path[256];
82    int base_path_len;
83    char base_path_parentref[256];
84    int base_path_parentref_len;
85    char rooturl[256];
86    int rooturl_len;
87    char fullrooturl[256];
88    char mount_path[256];
89    char base_folder[256];
90    int encoding;
91    char f_usr[32];
92    char f_pwd[32];
93    char user_pwd[64];
94    char server_ip[256];
95    int server_ip_len;
96    char hardware[256];
97}MultRule;
98typedef struct _Config
99{
100    int id;
101    int dir_num;
102    MultRule **multrule;
103}Config;
104
105typedef struct _Node
106{
107    char *cmdName;
108    //char *re_cmd;
109    struct _Node *next;
110}Node;
111//Node *newNode;
112
113typedef struct Action_Item
114{
115    char *action;
116    char *path;
117    struct Action_Item *next;
118} action_item;
119
120typedef struct node{
121    int isfile;
122    char * href;
123    //char auth[MIN_LENGTH];
124    long long int size;
125    //char month[MIN_LENGTH];
126    //char day[MIN_LENGTH];
127    //char lastmodifytime[MINSIZE];
128    char filename[MAX_CONTENT];
129    //char mtime[MIN_LENGTH];
130    unsigned long int modtime;
131    struct node *next;
132    int ismodify;
133}CloudFile;
134
135typedef struct BROWSE
136{
137    int foldernumber;
138    int filenumber;
139    CloudFile *folderlist;
140    CloudFile *filelist;
141}Browse;
142
143typedef struct ServerTreeNode
144{
145    int level;
146    char *parenthref;
147    Browse *browse;
148    struct ServerTreeNode *Child;
149    struct ServerTreeNode *NextBrother;
150}Server_TreeNode;
151//Server_TreeNode *ServerNode_del,*ServerNode_List;
152
153typedef struct SYNC_LIST
154{
155    char conflict_item[256];
156    char up_item_file[256];
157    char temp_path[MAX_LENGTH];
158    char down_item_file[256];
159    int index;
160    int sync_up;
161    int sync_down;
162    int download_only;
163    int upload_only;
164    int first_sync;
165    int receve_socket;
166    int have_local_socket;
167    int no_local_root;
168    int init_completed;
169    int sync_disk_exist;
170    int upload_break;
171    int IsNetWorkUnlink;    //2014.11.03 by sherry (ip������������������������������)
172    int IsPermission;       //2014.11.04 by sherry (server������)
173    action_item *copy_file_list;         //The copy files
174    action_item *server_action_list;    //Server���������������Socket
175    action_item *dragfolder_action_list;   //dragfolder������������������������Socket������
176    action_item *unfinished_list;
177    action_item *download_only_socket_head;
178    action_item *up_space_not_enough_list;
179    Node *SocketActionList;
180    Node *SocketActionList_Rename;
181    Node *SocketActionList_Refresh;
182    Server_TreeNode *ServerRootNode;
183    Server_TreeNode *OldServerRootNode;
184    Server_TreeNode *OrigServerRootNode;
185}sync_list;
186//sync_list **g_pSyncList;
187
188typedef struct LOCALFOLDER{
189    char *path;
190    char name[MAX_CONTENT];
191    struct LOCALFOLDER *next;
192}LocalFolder;
193
194typedef struct LOCALFILE{
195    char *path;
196    char name[MAX_CONTENT];
197    //char creationtime[MINSIZE];
198    //char lastaccesstime[MINSIZE];
199    //char lastwritetime[MINSIZE];
200    unsigned long modtime;
201    //long long int size;
202    struct LOCALFILE *next;
203}LocalFile;
204
205typedef struct LOCAL
206{
207    int foldernumber;
208    int filenumber;
209    LocalFolder *folderlist;
210    LocalFile *filelist;
211}Local;
212
213typedef struct MODTIME
214{
215    char mtime[MINSIZE];
216    unsigned long modtime;
217}mod_time;
218
219struct mounts_info_tag
220{
221    int num;
222    char **paths;
223};
224
225struct tokenfile_info_tag
226{
227    char *folder;
228    char *url;
229    char *mountpath;
230    struct tokenfile_info_tag *next;
231};
232
233typedef struct file_info
234{
235    char *path;
236    int index;
237}_info;
238
239struct FtpFile {
240  const char *filename;
241  FILE *stream;
242};
243
244int code_convert(char *from_charset,char *to_charset,char *inbuf,size_t inlen,char *outbuf,size_t outlen);
245char *to_utf8(char *buf,int index);
246char *utf8_to(char *buf,int index);
247size_t my_write_func(void *ptr, size_t size, size_t nmemb, FILE *stream);
248size_t my_read_func(void *ptr, size_t size, size_t nmemb, FILE *stream);
249int my_progress_func(char *progress_data,double t, double d,double ultotal,double ulnow);
250int download_(char *serverpath,int index);
251int upload(char *localpath1,int index);
252int my_delete_DELE(char *localpath,int index);
253int my_delete_RMD(char *localpath,int index);
254int my_rename_(char *localpath,char *newername,int index);
255int my_mkdir_(char *localpath,int index);
256int moveFolder(char *old_dir,char *new_dir,int index);
257int createFolder(char *dir,int index);
258void SearchServerTree_(Server_TreeNode* treeRoot,int index);
259int Delete(char *localpath,int index);
260int write_log(int status, char *message, char *filename,int index);
261void clear_global_var();
262int wait_handle_socket(int index);
263mod_time *get_mtime_1(FILE *fp);
264mod_time *Getmodtime(char *href,int index);
265int ChangeFile_modtime(char *filepath,time_t servermodtime,int index);
266int parseCloudInfo_one(char *parentherf,int index);
267int getCloudInfo_one(char *URL,int (* cmd_data)(char *,int),int index);
268int is_server_exist(char *parentpath,char *filepath,int index);
269void parseCloudInfo_forsize(char *parentherf);
270int getCloudInfo_forsize(char *URL,void (* cmd_data)(char *),int index);
271long long int get_file_size(char *serverpath,int index);
272int is_ftp_file_copying(char *serverhref,int index);
273void *Save_Socket(void *argc);
274int getCloudInfo(char *URL,void (* cmd_data)(char *,int),int index);
275int ftp_zone();
276time_t change_time_to_sec(char *time);
277mod_time *get_mtime(FILE *fp);
278mod_time *ftp_MDTM(char *href,int index);
279void parserHW(Config config);
280int parseCloudInfo_tree(char *parentherf,int index);
281Browse *browseFolder(char *URL,int index);
282int browse_to_tree(char *parenthref,Server_TreeNode *node,int index);
283Local *Find_Floor_Dir(const char *path);
284int the_same_name_compare(LocalFile *localfiletmp,CloudFile *filetmp,int index);
285int sync_server_to_local_perform(Browse *perform_br,Local *perform_lo,int index);
286int sync_server_to_local(Server_TreeNode *treenode,int (*sync_fuc)(Browse*,Local*,int),int index);
287int compareLocalList(int index);
288int isServerChanged(Server_TreeNode *newNode,Server_TreeNode *oldNode);
289int compareServerList(int index);
290void *SyncServer(void *argc);
291char *change_server_same_name(char *fullname,int index);
292char *change_local_same_name(char *fullname);
293int cmd_parser(char *cmd,int index);
294int download_only_add_socket_item(char *cmd,int index);
295void *Socket_Parser(void *argc);
296int send_action(int type, char *content,int port);
297void send_to_inotify();
298void read_config();
299void init_global_var();
300int initMyLocalFolder(Server_TreeNode *servertreenode,int index);
301int sync_local_to_server_init(Local *perform_lo,int index);
302int sync_local_to_server(char *path,int index);
303int sync_server_to_local_init(Browse *perform_br,Local *perform_lo,int index);
304int initialization();
305void run();
306void my_local_mkdir(char *path);
307void* sigmgr_thread(void *argc);
308int verify(Config config);
309char *my_str_malloc(size_t len);
310char *my_nstrchr(const char chr,char *str,int n);
311long long int get_local_freespace(int index);
312char *get_socket_base_path(char *cmd);
313char *get_prepath(char *temp,int len);
314action_item *get_action_item(const char *action,const char *path,action_item *head,int index);
315int get_path_to_index(char *path);
316action_item *create_action_item_head();
317Server_TreeNode *create_server_treeroot();
318int add_action_item(const char *action,const char *path,action_item *head);
319int add_all_download_only_dragfolder_socket_list(const char *dir,int index);
320int add_socket_item(char *buf,int i);
321int add_all_download_only_socket_list(char *cmd,const char *dir,int index);
322int del_action_item(const char *action,const char *path,action_item *head);
323void del_download_only_action_item(const char *action,const char *path,action_item *head);
324void del_all_items(char *dir,int index);
325void show(Node* head);
326void show_item(action_item* head);
327int test_if_download_temp_file(char *filename);
328int test_if_dir(const char *dir);
329int test_if_dir_empty(char *dir);
330Node *queue_dequeue (Node *q);
331void replace_char_in_str(char *str,char newchar,char oldchar);
332void SearchServerTree(Server_TreeNode* treeRoot);
333char *localpath_to_serverpath(char *from_localpath,int index);
334char *serverpath_to_localpath(char *from_serverpath,int index);
335int is_file(char *p);
336int is_folder(char *p);
337int is_local_space_enough(CloudFile *do_file,int index);
338int is_server_space_enough(char *localfilepath,int index);//2014.11.14 by sherry add���server������������
339//int get_server_info(char *URL,int (* cmd_data)(char *,int),int index);//2014.11.14 by sherry add���server������������
340int get_server_info(char *URL,int index);
341int is_server_have_localpath(char *path,Server_TreeNode *treenode,int index);
342void free_action_item(action_item *head);
343void free_CloudFile_item(CloudFile *head);
344void free_server_tree(Server_TreeNode *node);
345void free_LocalFolder_item(LocalFolder *head);
346void free_LocalFile_item(LocalFile *head);
347void free_localfloor_node(Local *local);
348void init_utf8_char_table();
349const char* tellenc(const char* const buffer, const size_t len);
350int get_mounts_info(struct mounts_info_tag *info);
351int get_tokenfile_info();
352int check_config_path(int is_read_config);
353struct tokenfile_info_tag *initial_tokenfile_info_data(struct tokenfile_info_tag **token);
354int write_to_nvram(char *contents,char *nv_name);
355int write_to_ftp_tokenfile(char *contents);
356int delete_tokenfile_info(char *url,char *folder);
357char *delete_nvram_contents(char *url,char *folder);
358int write_to_tokenfile(char *mpath);
359int add_tokenfile_info(char *url,char *folder,char *mpath);
360char *add_nvram_contents(char *url,char *folder);
361int rewrite_tokenfile_and_nv();
362void sig_handler (int signum);
363int write_notify_file(char *path,int signal_num);
364int detect_process(char * process_name);
365int create_shell_file();
366int write_get_nvram_script(char *name,char *shell_dir,char *val_dir);
367int create_ftp_conf_file(Config *config);
368int free_tokenfile_info(struct tokenfile_info_tag *head);
369int check_disk_change();
370int check_sync_disk_removed();
371CloudFile *get_CloudFile_node(Server_TreeNode* treeRoot,const char *dofile_href,int a);
372int do_unfinished(int index);
373int deal_dragfolder_to_socketlist(char *dir,int index);
374int check_serverlist_modify(int index,Server_TreeNode *newnode);
375int parse_config(const char *path,Config *config);
376Server_TreeNode * getoldnode(Server_TreeNode *tempoldnode,char *href);
377int get_prefix(char *temp,int len);
378int is_server_modify(Server_TreeNode *newNode,Server_TreeNode *oldNode);
379char *search_newpath(char *href,int i);
380char *oauth_url_unescape(const char *string, size_t *olen);
381char *oauth_url_escape(const char *string);
382static void *xmalloc_fatal(size_t size);
383void *xmalloc (size_t size);
384void *xrealloc (void *ptr, size_t size);
385char *xstrdup (const char *s);
386int convert_nvram_to_file_mutidir(char *file,Config *config);
387int convert_nvram_to_file_usbinfo(char *file);
388int webdav_config_usbinfo_exist();
389int convert_nvram_to_file(char *file);
390int debugFun(CURL *curl,curl_infotype type,char *str,size_t len,void *stream);
391void my_mkdir_r(char *path,int index);
392int create_xml(int status);
393int socket_check(char *dir,char *name,int index);
394int usr_auth(char *ip,char *user_pwd);
395//void create_start_file();
396//int detect_process_file();
397#endif  //BASE_H
398