linux auto_ftp自动上传更新的文件


1 /prj/fup 192.168.0.1 linshao woaienen /woaienen/ /woaienen/file_list


  1 #include <stdio.h>
  2 #include <sys/types.h>
  3 #include <sys/stat.h>
  4 #include <fcntl.h>
  5 #include <unistd.h>
  6 #include <dirent.h>
  7 #include <string.h>
  8 #include <time.h>
  9 #include <stdlib.h>
 10 #define FILE_NAME_MAX 256
 11 #define FLAGS O_WRONLY | O_CREAT | O_TRUNC
 12 #define MODE S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH
 13 
 14 typedef struct _en_file {
 15     char d_name[FILE_NAME_MAX + 1];
 16     time_t ctime;
 17 } en_file;
 18 
 19 typedef struct _file_list {
 20     en_file file;
 21     struct _file_list * next;
 22 } file_list;
 23 
 24 void get_file_list(const char * pathname, file_list * f)
 25 {
 26     DIR * dir = opendir(pathname);
 27     if (dir == NULL)
 28     {
 29         printf("No such directory ... \n");
 30         return;
 31     }
 32     
 33     struct dirent * dt = NULL;
 34     file_list * list = f;
 35     file_list * fl = NULL;
 36     
 37     while (dt = readdir(dir))
 38     {
 39         if (strcmp(dt->d_name, ".") == 0 || 
 40             strcmp(dt->d_name, "..") == 0 || 
 41             strcmp(dt->d_name, "file_list") == 0 || 
 42             strcmp(dt->d_name, "ftp_sh") == 0 || 
 43             strcmp(dt->d_name, "fl.log") == 0)
 44             continue;
 45             
 46         fl = (file_list *)malloc(sizeof(file_list));
 47         fl->next = NULL;
 48         strcpy(fl->file.d_name, pathname);
 49         strcat(fl->file.d_name, dt->d_name);
 50         
 51         struct stat sbuf;
 52         stat(fl->file.d_name, &sbuf);
 53         fl->file.ctime = sbuf.st_ctime;
 54         list->next = fl;
 55         list = list->next;
 56     }
 57     closedir(dir);
 58     return;
 59 }
 60 
 61 void read_file_list(const char * pathname, file_list * f)
 62 {
 63     int len, i, j, is_num = 0;
 64     int fd;
 65     if ((fd = open(pathname, O_RDONLY)) == -1)
 66     {
 67         printf("error, open file failed ... \n");
 68         return;
 69     }
 70     
 71     file_list * list = f;
 72     file_list * fl = NULL;
 73     char buf[1024];
 74     while (1)
 75     {
 76         len = read(fd, buf, 1024);
 77         if (len <= 0)
 78             break;
 79         for (i = 0; i < len; i++)
 80         {
 81             if (buf[i] == ':')
 82             {
 83                 is_num = 1;
 84                 continue;
 85             }
 86             
 87             if (buf[i] == '\n')
 88             {
 89                 is_num = 0;
 90                 fl->file.d_name[j] = '\0';
 91                 list->next = fl;
 92                 list = list->next;
 93                 fl = NULL;
 94                 continue;
 95             }
 96             
 97             if (!is_num) 
 98             {
 99                 if (fl == NULL) 
100                 {
101                     fl = (file_list *)malloc(sizeof(file_list));
102                     fl->next = NULL;
103                     fl->file.ctime = 0;
104                     j = 0;
105                 }
106                 fl->file.d_name[j++] = buf[i];
107             }
108             else
109             {
110                 if (buf[i] >= '0' && buf[i] <= '9') 
111                 {
112                     fl->file.ctime = fl->file.ctime * 10 + buf[i] - '0';
113                 }
114             }
115         }
116         if (len < 1024)
117             break;
118     }
119     close(fd);
120     return;
121 }
122 
123 void write_file_list(const char * pathname, file_list * f)
124 {
125     int fd;
126     if ((fd = open(pathname, FLAGS, MODE)) == -1)
127     {
128         printf("error, open file failed ... \n");
129         return;
130     }
131     
132     file_list * list = f->next;
133     char num[13];
134     while (list)
135     {
136         write(fd, list->file.d_name, strlen(list->file.d_name));
137         sprintf(num, ":%10d\n", list->file.ctime);
138         write(fd, num, 12);
139         list = list->next;
140     }
141     close(fd);
142     return;
143 }
144 
145 void printf_file_list(file_list * f)
146 {
147     int total = 0;
148     file_list * list = f->next;
149     printf("The file list is : \n\n");
150     printf("%-60s  %16s\n", "  file name", "time");
151     printf("================================================================================\n");
152     while (list)
153     {
154         printf("%-60s  %18d\n", list->file.d_name, list->file.ctime);
155         list = list->next;
156         total++;
157     }
158     printf("================================================================================\n");
159     printf("  toatl: %d\n", total);
160     return;
161 }
162 
163 int get_send_file_list(file_list * r, file_list * w, file_list * f)
164 {
165     file_list * list, * re, * wr, * fl;
166     list = f;
167     wr = w->next;
168     int state = 0;
169     int n = 0;
170     while (wr)
171     {
172         re = r->next;
173         while (re)
174         {
175             if (strcmp(wr->file.d_name, re->file.d_name) == 0)
176             {
177                 state = 1;
178                 if (wr->file.ctime > re->file.ctime)
179                 {
180                     fl = (file_list *)malloc(sizeof(file_list));
181                     fl->file = wr->file;
182                     fl->next = NULL;
183                     list->next = fl;
184                     list = list->next;
185                     n++;
186                 }
187                 break;
188             }
189             re = re->next;
190         }
191         if (!state)
192         {
193             fl = (file_list *)malloc(sizeof(file_list));
194             fl->file = wr->file;
195             fl->next = NULL;
196             list->next = fl;
197             list = list->next;
198             n++;
199         }
200         state = 0;
201         wr = wr->next;
202     }
203     return n;
204 }
205 
206 void get_win_file_name(const char * p, char * q)
207 {
208     int i = 0, j = 0, m = 0, n = 0;
209     while (p[i])
210         if (p[i++] == '/')
211             m = i;
212     i = 0;
213     while (p[m])
214         q[i++] = p[m++];
215     q[i] = '\0';
216     return;
217 }
218 
219 void send_file(const char * ip, const char * user, 
220     const char * password, file_list * f, int num)
221 {
222     int i;
223     file_list * list = f->next;
224     FILE * fp, * log;
225     printf("\n\nSend %d files ...\n", num);
226     if ((fp=popen("ftp -n","w")) == NULL) 
227     { 
228         puts("Run ftp fail !\n"); 
229         exit(1); 
230     }
231     log = fopen("/var/log/ftp.update.log", "aw+");
232     char name[FILE_NAME_MAX + 1];
233     char str[255];
234     fprintf(fp,"open %s\n", ip); 
235     fprintf(fp,"user %s %s\n", user, password);
236     while (list)
237     {
238         get_win_file_name(list->file.d_name, name);
239         fprintf(fp,"put %s %s\n", list->file.d_name, name);
240         printf("Upload the %s ...\n", list->file.d_name);
241         fprintf(log, "Upload the %s ...\n", list->file.d_name);
242         list = list->next;
243     }
244     while ((fgets(str, 255, fp)) != NULL) 
245         fputs(str, log); 
246     fprintf(fp,"bye\n"); 
247     pclose(fp);
248     fclose(log);
249     printf("Ok! That is right ... \n\n");
250     return;
251 }
252 
253 int main(int argc, char * argv[])
254 {
255     file_list re, wr, send;
256     if (argc != 6){ 
257         printf("%s\n%s\n", 
258         "Usage:", 
259         "fup <ip> <user> <password> <dir> <filelist>"); 
260         exit(0); 
261     }
262     re.next = NULL;
263     wr.next = NULL;
264     send.next = NULL;
265     int num = 0;
266     read_file_list(argv[5], &re);
267     get_file_list(argv[4], &wr);
268     num = get_send_file_list(&re, &wr, &send);
269     printf_file_list(&send);
270     send_file(argv[1], argv[2], argv[3], &send, num);
271     write_file_list(argv[5], &wr);
272     return 0;
273 }
几许风雨天,千里孤月夜。日出似无时,何处现流光。
原文地址:https://www.cnblogs.com/shendiao/p/2550931.html