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