Line data Source code
1 : #include "../burp.h"
2 : #include "../action.h"
3 : #include "../asfd.h"
4 : #include "../async.h"
5 : #include "../attribs.h"
6 : #include "../cmd.h"
7 : #include "../handy.h"
8 : #include "../log.h"
9 : #include "../times.h"
10 : #include "list.h"
11 :
12 : /* Note: The chars in this function are not the same as in the CMD_ set.
13 : These are for printing to the screen only. */
14 0 : static char *encode_mode(mode_t mode, char *buf)
15 : {
16 0 : char *cp=buf;
17 0 : *cp++=S_ISDIR(mode)?'d':S_ISBLK(mode)?'b':S_ISCHR(mode)?'c':
18 : S_ISLNK(mode)?'l':S_ISFIFO(mode)?'p':S_ISSOCK(mode)?'s':'-';
19 0 : *cp++=mode&S_IRUSR?'r':'-';
20 0 : *cp++=mode&S_IWUSR?'w':'-';
21 0 : *cp++=(mode&S_ISUID?(mode&S_IXUSR?'s':'S'):(mode&S_IXUSR?'x':'-'));
22 0 : *cp++=mode&S_IRGRP?'r':'-';
23 0 : *cp++=mode&S_IWGRP?'w':'-';
24 0 : *cp++=(mode&S_ISGID?(mode&S_IXGRP?'s':'S'):(mode&S_IXGRP?'x':'-'));
25 0 : *cp++=mode&S_IROTH?'r':'-';
26 0 : *cp++=mode&S_IWOTH?'w':'-';
27 0 : *cp++=(mode&S_ISVTX?(mode&S_IXOTH?'t':'T'):(mode&S_IXOTH?'x':'-'));
28 0 : *cp='\0';
29 0 : return cp;
30 : }
31 :
32 0 : static void ls_to_buf(char *lsbuf, struct sbuf *sb)
33 : {
34 : int n;
35 : char *p;
36 : time_t time;
37 : const char *f;
38 0 : struct stat *statp=&sb->statp;
39 0 : *lsbuf='\0';
40 :
41 0 : p=encode_mode(statp->st_mode, lsbuf);
42 0 : n=sprintf(p, " %2d ", (uint32_t)statp->st_nlink);
43 0 : p+=n;
44 0 : n=sprintf(p, "%5d %5d", (uint32_t)statp->st_uid,
45 : (uint32_t)statp->st_gid);
46 0 : p+=n;
47 0 : n=sprintf(p, " %7lu ", (unsigned long)statp->st_size);
48 0 : p+=n;
49 0 : if(statp->st_ctime>statp->st_mtime) time=statp->st_ctime;
50 0 : else time=statp->st_mtime;
51 :
52 : // Display most recent time.
53 0 : p=encode_time(time, p);
54 0 : *p++=' ';
55 0 : for(f=sb->path.buf; *f; ) *p++=*f++;
56 0 : *p=0;
57 0 : }
58 :
59 0 : static void ls_long_output(struct sbuf *sb)
60 : {
61 : static char lsbuf[2048];
62 0 : ls_to_buf(lsbuf, sb);
63 : printf("%s", lsbuf);
64 0 : if(sb->link.buf) printf(" -> %s", sb->link.buf);
65 : printf("\n");
66 0 : }
67 :
68 : static void ls_short_output(struct sbuf *sb)
69 : {
70 0 : printf("%s\n", sb->path.buf);
71 : }
72 :
73 0 : static void list_item(enum action act, struct sbuf *sb)
74 : {
75 0 : if(act==ACTION_LIST_LONG)
76 : {
77 0 : ls_long_output(sb);
78 : }
79 : else
80 : {
81 0 : ls_short_output(sb);
82 : }
83 0 : }
84 :
85 0 : int do_list_client(struct asfd *asfd, enum action act, struct conf **confs)
86 : {
87 0 : int ret=-1;
88 0 : char msg[512]="";
89 0 : struct sbuf *sb=NULL;
90 0 : struct iobuf *rbuf=asfd->rbuf;
91 0 : const char *backup=get_string(confs[OPT_BACKUP]);
92 0 : const char *backup2=get_string(confs[OPT_BACKUP2]);
93 0 : const char *browsedir=get_string(confs[OPT_BROWSEDIR]);
94 0 : const char *regex=get_string(confs[OPT_REGEX]);
95 : //logp("in do_list\n");
96 :
97 0 : switch(act)
98 : {
99 : case ACTION_LIST:
100 : case ACTION_LIST_LONG:
101 0 : if(browsedir && regex)
102 : {
103 0 : logp("You cannot specify both a directory and a regular expression when listing.\n");
104 0 : goto end;
105 : }
106 0 : if(browsedir)
107 0 : snprintf(msg, sizeof(msg), "listb %s:%s",
108 : backup?backup:"", browsedir);
109 : else
110 0 : snprintf(msg, sizeof(msg), "list %s:%s",
111 : backup?backup:"", regex?regex:"");
112 : break;
113 : case ACTION_DIFF:
114 : case ACTION_DIFF_LONG:
115 0 : snprintf(msg, sizeof(msg), "diff %s:%s",
116 : backup?backup:"", backup2?backup2:"");
117 : break;
118 : default:
119 0 : logp("unknown action %d\n", act);
120 0 : goto end;
121 : }
122 0 : if(asfd->write_str(asfd, CMD_GEN, msg)
123 0 : || asfd_read_expect(asfd, CMD_GEN, "ok"))
124 : goto end;
125 :
126 0 : if(!(sb=sbuf_alloc(get_protocol(confs)))) goto end;
127 0 : iobuf_init(&sb->path);
128 0 : iobuf_init(&sb->link);
129 0 : iobuf_init(&sb->attr);
130 :
131 : // This should probably should use the sbuf stuff.
132 : while(1)
133 : {
134 0 : sbuf_free_content(sb);
135 :
136 0 : iobuf_free_content(rbuf);
137 0 : if(asfd->read(asfd)) break;
138 0 : if(rbuf->cmd==CMD_MESSAGE)
139 : {
140 0 : printf("%s\n", rbuf->buf);
141 0 : if(!strcmp(rbuf->buf, "no backups"))
142 0 : ret=0;
143 : goto end;
144 : }
145 0 : else if(rbuf->cmd==CMD_TIMESTAMP)
146 : {
147 : // A backup timestamp, just print it.
148 0 : printf("Backup: %s\n", rbuf->buf);
149 0 : if(browsedir)
150 : printf("Listing directory: %s\n",
151 : browsedir);
152 0 : if(regex)
153 : printf("With regex: %s\n",
154 : regex);
155 0 : continue;
156 : }
157 0 : else if(rbuf->cmd!=CMD_ATTRIBS)
158 : {
159 0 : iobuf_log_unexpected(rbuf, __func__);
160 0 : goto end;
161 : }
162 0 : iobuf_copy(&sb->attr, rbuf);
163 0 : iobuf_init(rbuf);
164 :
165 0 : attribs_decode(sb);
166 :
167 0 : if(asfd->read(asfd))
168 : {
169 0 : logp("got stat without an object\n");
170 0 : goto end;
171 : }
172 0 : iobuf_copy(&sb->path, rbuf);
173 0 : iobuf_init(rbuf);
174 :
175 0 : if(sb->path.cmd==CMD_DIRECTORY
176 0 : || sb->path.cmd==CMD_FILE
177 0 : || sb->path.cmd==CMD_ENC_FILE
178 0 : || sb->path.cmd==CMD_EFS_FILE
179 0 : || sb->path.cmd==CMD_SPECIAL)
180 : {
181 0 : list_item(act, sb);
182 : }
183 0 : else if(cmd_is_link(sb->path.cmd)) // symlink or hardlink
184 : {
185 0 : if(asfd->read(asfd)
186 0 : || rbuf->cmd!=sb->path.cmd)
187 : {
188 0 : logp("could not get link %s\n",
189 0 : iobuf_to_printable(&sb->path));
190 0 : goto end;
191 : }
192 0 : iobuf_copy(&sb->link, rbuf);
193 0 : iobuf_init(rbuf);
194 0 : list_item(act, sb);
195 : }
196 : else
197 : {
198 0 : logp("unlistable %s\n", iobuf_to_printable(&sb->path));
199 : }
200 : }
201 :
202 : ret=0;
203 : end:
204 0 : sbuf_free(&sb);
205 0 : if(!ret) logp("List finished ok\n");
206 0 : return ret;
207 : }
|