-
Notifications
You must be signed in to change notification settings - Fork 292
/
cmd-ls.c
378 lines (335 loc) · 9.54 KB
/
cmd-ls.c
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*
* command for listing the vault
*
* Copyright (C) 2014-2024 LastPass.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* In addition, as a special exception, the copyright holders give
* permission to link the code of portions of this program with the
* OpenSSL library under certain conditions as described in each
* individual source file, and distribute linked combinations
* including the two.
*
* You must obey the GNU General Public License in all respects
* for all of the code used other than OpenSSL. If you modify
* file(s) with this exception, you may extend this exception to your
* version of the file(s), but you are not obligated to do so. If you
* do not wish to do so, delete this exception statement from your
* version. If you delete this exception statement from all source
* files in the program, then also delete it here.
*
* See LICENSE.OpenSSL for more details regarding this exception.
*/
#include "cmd.h"
#include "util.h"
#include "config.h"
#include "terminal.h"
#include "format.h"
#include "kdf.h"
#include <getopt.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
static bool long_listing = false;
static bool show_mtime = true;
struct node {
char *name;
struct account *account;
bool shared;
struct list_head children;
struct list_head list;
};
struct path_component
{
char *component;
struct list_head list;
};
/*
* Tokenize path and add each component to the components list.
* For group names, the path separator is a backslash. The path
* string is modified in place and the component list stores
* pointers to the modified string.
*/
static void parse_path(char *path, struct list_head *components)
{
char *token;
struct path_component *pc;
for (token = strtok(path, "\\"); token; token = strtok(NULL, "\\")) {
pc = new0(struct path_component, 1);
pc->component = token;
list_add_tail(&pc->list, components);
}
}
static void __insert_node(struct node *head,
struct list_head *components,
struct account *account)
{
struct path_component *pc;
struct node *child, *tmp;
/* iteratively build a tree from all the path components */
list_for_each_entry(pc, components, list) {
child = NULL;
list_for_each_entry(tmp, &head->children, list) {
if (!strcmp(tmp->name, pc->component)) {
child = tmp;
break;
}
}
if (!child) {
child = new0(struct node, 1);
child->shared= !!account->share;
child->name = xstrdup(pc->component);
INIT_LIST_HEAD(&child->children);
list_add_tail(&child->list, &head->children);
}
head = child;
}
/* skip group display -- we already added the hierarchy for them */
if (account_is_group(account))
return;
/* and add the site at the lowest level */
child = new0(struct node, 1);
child->account = account;
child->shared= !!account->share;
child->name = xstrdup(account->name);
INIT_LIST_HEAD(&child->children);
list_add_tail(&child->list, &head->children);
}
static void insert_node(struct node *head, const char *path, struct account *account)
{
struct list_head components;
struct path_component *pc, *tmp;
_cleanup_free_ char *dirname = xstrdup(path);
char *pos;
/* remove name portion of fullname; we don't parse that */
if (strlen(dirname) >= strlen(account->name)) {
char *tmp = dirname + strlen(dirname) - strlen(account->name);
if (strcmp(tmp, account->name) == 0) {
*tmp = 0;
}
}
pos = dirname;
/* trim trailing slash */
if (strlen(pos))
pos[strlen(pos)-1] = 0;
/*
* We are left with one of:
*
* (none)/
* groupname/
* Shared-folder/
* Shared-folder/groupname/
*
* If there are embedded backslashes, these are treated as folder
* names by parse_path().
*/
INIT_LIST_HEAD(&components);
if (account->share && strlen(pos) >= strlen(account->share->name)) {
pos[strlen(account->share->name)] = 0;
parse_path(pos, &components);
pos += strlen(account->share->name) + 1;
}
/* either '(none)/' or group/ or empty string */
parse_path(pos, &components);
__insert_node(head, &components, account);
list_for_each_entry_safe(pc, tmp, &components, list) {
list_del(&pc->list);
free(pc);
}
}
static void free_node(struct node *head)
{
struct node *node, *tmp;
if (!head)
return;
list_for_each_entry_safe(node, tmp, &head->children, list) {
free_node(node);
}
free(head->name);
free(head);
}
static void print_node(struct node *head, char *fmt_str, int level)
{
struct node *node;
list_for_each_entry(node, &head->children, list) {
if (node->name) {
for (int i = 0; i < level; ++i)
printf(" ");
if (node->account) {
struct buffer buf;
buffer_init(&buf);
format_account(&buf, fmt_str, node->account);
terminal_printf("%s\n", buf.bytes);
free(buf.bytes);
}
else if (node->shared)
terminal_printf(TERMINAL_FG_CYAN TERMINAL_BOLD "%s" TERMINAL_RESET "\n", node->name);
else
terminal_printf(TERMINAL_FG_BLUE TERMINAL_BOLD "%s" TERMINAL_RESET "\n", node->name);
}
print_node(node, fmt_str, level + 1);
}
}
static int compare_account(const void *a, const void *b)
{
struct account * const *acct_a = a;
struct account * const *acct_b = b;
_cleanup_free_ char *str1 = get_display_fullname(*acct_a);
_cleanup_free_ char *str2 = get_display_fullname(*acct_b);
return strcmp(str1, str2);
}
int cmd_ls(int argc, char **argv)
{
unsigned char key[KDF_HASH_LEN];
struct session *session = NULL;
struct blob *blob = NULL;
static struct option long_options[] = {
{"sync", required_argument, NULL, 'S'},
{"color", required_argument, NULL, 'C'},
{"format", required_argument, NULL, 'f'},
{"long", no_argument, NULL, 'l'},
{0, 0, 0, 0}
};
int option;
int option_index;
char *group = NULL;
int group_len;
char *sub;
struct node *root;
char *fullname;
enum blobsync sync = BLOB_SYNC_AUTO;
enum color_mode cmode = COLOR_MODE_AUTO;
bool print_tree;
struct account *account;
_cleanup_free_ struct account **account_array = NULL;
int i, num_accounts;
_cleanup_free_ char *fmt_str = NULL;
struct share *share;
while ((option = getopt_long(argc, argv, "lmu", long_options, &option_index)) != -1) {
switch (option) {
case 'S':
sync = parse_sync_string(optarg);
break;
case 'C':
cmode = parse_color_mode_string(optarg);
break;
case 'f':
fmt_str = xstrdup(optarg);
break;
case 'l':
long_listing = true;
break;
case 'm':
show_mtime = true;
break;
case 'u':
show_mtime = false;
break;
case '?':
default:
die_usage(cmd_ls_usage);
}
}
switch (argc - optind) {
case 0:
break;
case 1:
group = argv[optind];
break;
default:
die_usage(cmd_ls_usage);
}
terminal_set_color_mode(cmode);
print_tree = cmode == COLOR_MODE_ALWAYS ||
(cmode == COLOR_MODE_AUTO && isatty(fileno(stdout)));
init_all(sync, key, &session, &blob);
root = new0(struct node, 1);
INIT_LIST_HEAD(&root->children);
/* '(none)' group -> search for any without group */
if (group && !strcmp(group, "(none)"))
group = "";
num_accounts = 0;
list_for_each_entry(account, &blob->account_head, list) {
num_accounts++;
}
list_for_each_entry(share, &blob->share_head, list) {
num_accounts++;
}
i=0;
account_array = xcalloc(num_accounts, sizeof(struct account *));
list_for_each_entry(account, &blob->account_head, list) {
account_array[i++] = account;
}
/* fake accounts for shares, so that empty shared folders are shown. */
list_for_each_entry(share, &blob->share_head, list) {
struct account *account = new_account();
char *tmpname = NULL;
xasprintf(&tmpname, "%s/", share->name);
account->share = share;
account->id = share->id;
account_set_name(account, xstrdup(""), key);
account_set_fullname(account, tmpname, key);
account_set_url(account, "http://group", key, &session->feature_flag);
account_array[i++] = account;
}
qsort(account_array, num_accounts, sizeof(struct account *),
compare_account);
if (!fmt_str) {
xasprintf(&fmt_str,
TERMINAL_FG_CYAN "%s"
TERMINAL_FG_GREEN TERMINAL_BOLD "%%a%c"
TERMINAL_NO_BOLD
" [id: %%ai]"
"%s" TERMINAL_RESET,
(long_listing) ?
((show_mtime) ? "%am " : "%aU ") : "",
(print_tree) ? 'n' : 'N',
(long_listing) ? " [username: %au]" : "");
}
for (i=0; i < num_accounts; i++)
{
struct account *account = account_array[i];
if (group) {
sub = strstr(account->fullname, group);
if (!sub || sub != account->fullname)
continue;
group_len = strlen(group);
sub += group_len;
if (group_len &&
group[group_len - 1] != '/' &&
sub[0] != '\0' && sub[0] != '/')
continue;
}
fullname = get_display_fullname(account);
if (print_tree)
insert_node(root, fullname, account);
else {
struct buffer buf;
buffer_init(&buf);
format_account(&buf, fmt_str, account);
terminal_printf("%s\n", buf.bytes);
free(buf.bytes);
}
free(fullname);
}
if (print_tree)
print_node(root, fmt_str, 0);
free_node(root);
session_free(session);
blob_free(blob);
return 0;
}