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 : 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 0 : (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 :
49 0 : p=encode_time(statp->st_mtime, p);
50 0 : *p++=' ';
51 0 : for(f=sb->path.buf; *f; ) *p++=*f++;
52 0 : *p=0;
53 0 : }
54 :
55 0 : static void ls_long_output(struct sbuf *sb)
56 : {
57 : static char lsbuf[2048];
58 0 : ls_to_buf(lsbuf, sb);
59 0 : printf("%s", lsbuf);
60 0 : if(sb->link.buf) printf(" -> %s", sb->link.buf);
61 0 : printf("\n");
62 0 : }
63 :
64 : static void ls_short_output(struct sbuf *sb)
65 : {
66 0 : printf("%s\n", sb->path.buf);
67 : }
68 :
69 : static void list_item(enum action act, struct sbuf *sb)
70 : {
71 0 : if(act==ACTION_LIST_LONG)
72 : {
73 0 : ls_long_output(sb);
74 : }
75 : else
76 : {
77 0 : ls_short_output(sb);
78 : }
79 : }
80 :
81 0 : int do_list_client(struct asfd *asfd, enum action act, struct conf **confs)
82 : {
83 0 : int ret=-1;
84 0 : char msg[512]="";
85 0 : struct sbuf *sb=NULL;
86 0 : struct iobuf *rbuf=asfd->rbuf;
87 0 : const char *backup=get_string(confs[OPT_BACKUP]);
88 0 : const char *backup2=get_string(confs[OPT_BACKUP2]);
89 0 : const char *browsedir=get_string(confs[OPT_BROWSEDIR]);
90 0 : const char *regex=get_string(confs[OPT_REGEX]);
91 : //logp("in do_list\n");
92 :
93 0 : switch(act)
94 : {
95 : case ACTION_LIST:
96 : case ACTION_LIST_LONG:
97 0 : if(browsedir && regex)
98 : {
99 0 : logp("You cannot specify both a directory and a regular expression when listing.\n");
100 0 : goto end;
101 : }
102 0 : if(browsedir)
103 0 : snprintf(msg, sizeof(msg), "listb %s:%s",
104 : backup?backup:"", browsedir);
105 : else
106 0 : snprintf(msg, sizeof(msg), "list %s:%s",
107 : backup?backup:"", regex?regex:"");
108 : break;
109 : case ACTION_DIFF:
110 : case ACTION_DIFF_LONG:
111 0 : snprintf(msg, sizeof(msg), "diff %s:%s",
112 : backup?backup:"", backup2?backup2:"");
113 0 : break;
114 : default:
115 0 : logp("unknown action %d\n", act);
116 0 : goto end;
117 : }
118 0 : if(asfd->write_str(asfd, CMD_GEN, msg)
119 0 : || asfd_read_expect(asfd, CMD_GEN, "ok"))
120 : goto end;
121 :
122 0 : if(!(sb=sbuf_alloc(get_protocol(confs)))) goto end;
123 0 : iobuf_init(&sb->path);
124 0 : iobuf_init(&sb->link);
125 0 : iobuf_init(&sb->attr);
126 :
127 : // This should probably should use the sbuf stuff.
128 : while(1)
129 : {
130 0 : sbuf_free_content(sb);
131 :
132 0 : iobuf_free_content(rbuf);
133 0 : if(asfd->read(asfd)) break;
134 0 : if(rbuf->cmd==CMD_MESSAGE)
135 : {
136 0 : printf("%s\n", rbuf->buf);
137 0 : if(!strcmp(rbuf->buf, "no backups"))
138 0 : ret=0;
139 : goto end;
140 : }
141 0 : else if(rbuf->cmd==CMD_TIMESTAMP)
142 : {
143 : // A backup timestamp, just print it.
144 0 : printf("Backup: %s\n", rbuf->buf);
145 0 : if(browsedir)
146 0 : printf("Listing directory: %s\n",
147 : browsedir);
148 0 : if(regex)
149 0 : printf("With regex: %s\n",
150 : regex);
151 0 : continue;
152 : }
153 0 : else if(rbuf->cmd!=CMD_ATTRIBS)
154 : {
155 0 : iobuf_log_unexpected(rbuf, __func__);
156 0 : goto end;
157 : }
158 0 : iobuf_copy(&sb->attr, rbuf);
159 0 : iobuf_init(rbuf);
160 :
161 0 : attribs_decode(sb);
162 :
163 0 : if(asfd->read(asfd))
164 : {
165 0 : logp("got stat without an object\n");
166 0 : goto end;
167 : }
168 0 : iobuf_copy(&sb->path, rbuf);
169 0 : iobuf_init(rbuf);
170 :
171 0 : if(sb->path.cmd==CMD_DIRECTORY
172 0 : || sb->path.cmd==CMD_FILE
173 0 : || sb->path.cmd==CMD_ENC_FILE
174 0 : || sb->path.cmd==CMD_EFS_FILE
175 0 : || sb->path.cmd==CMD_SPECIAL)
176 : {
177 0 : list_item(act, sb);
178 : }
179 0 : else if(cmd_is_link(sb->path.cmd)) // symlink or hardlink
180 : {
181 0 : if(asfd->read(asfd)
182 0 : || rbuf->cmd!=sb->path.cmd)
183 : {
184 0 : logp("could not get link %s\n",
185 0 : iobuf_to_printable(&sb->path));
186 0 : goto end;
187 : }
188 0 : iobuf_copy(&sb->link, rbuf);
189 0 : iobuf_init(rbuf);
190 0 : list_item(act, sb);
191 : }
192 : else
193 : {
194 0 : logp("unlistable %s\n", iobuf_to_printable(&sb->path));
195 : }
196 : }
197 :
198 : ret=0;
199 : end:
200 0 : sbuf_free(&sb);
201 0 : if(!ret) logp("List finished ok\n");
202 0 : return ret;
203 : }
|