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