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