-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdirlist.c
329 lines (258 loc) · 7.08 KB
/
dirlist.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
/*
* Copyright (c) 2021 Joris Vink <joris@coders.se>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/param.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fts.h>
#include <fnmatch.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "ce.h"
#define DENTRY_FLAG_HIDDEN (1 << 1)
struct dentry {
char *path;
const char *vpath;
u_int16_t flags;
mode_t mode;
mode_t vmode;
};
struct dlist {
size_t nelm;
char *path;
struct dentry *entries;
};
union cp {
const char *cp;
char *p;
};
static void dirlist_load(struct cebuf *, const char *);
static void dirlist_tobuf(struct cebuf *, const char *);
static int dirlist_cmp(const FTSENT **, const FTSENT **);
static const char *ignored[] = {
"*.git*",
"*.svn*",
"*.o",
NULL,
};
void
ce_dirlist_path(struct cebuf *buf, const char *path)
{
struct dlist *list;
if (buf->intdata != NULL)
fatal("%s: intdata for '%s' not NULL", __func__, buf->name);
if ((list = calloc(1, sizeof(*list))) == NULL)
fatal("%s: calloc failed while allocating list", __func__);
list->path = ce_strdup(path);
buf->intdata = list;
buf->flags |= CE_BUFFER_RO;
dirlist_load(buf, path);
dirlist_tobuf(buf, NULL);
}
void
ce_dirlist_rescan(struct cebuf *buf)
{
ce_dirlist_close(buf);
ce_dirlist_path(buf, buf->path);
}
void
ce_dirlist_close(struct cebuf *buf)
{
size_t idx;
struct dlist *list;
struct dentry *entry;
if (buf->intdata == NULL)
fatal("%s: no dirlist attached to '%s'", __func__, buf->name);
list = buf->intdata;
buf->intdata = NULL;
for (idx = 0; idx < list->nelm; idx++) {
entry = &list->entries[idx];
free(entry->path);
}
free(list->entries);
free(list->path);
free(list);
}
void
ce_dirlist_narrow(struct cebuf *buf, const char *pattern)
{
dirlist_tobuf(buf, pattern);
}
void
ce_dirlist_rmfile(const void *arg)
{
struct stat st;
const char *fp = arg;
if (stat(fp, &st) == -1) {
ce_editor_message("stat(%s): %s", fp, errno_s);
return;
}
if (!S_ISREG(st.st_mode)) {
ce_editor_message("refusing to delete directory %s", fp);
return;
}
if (unlink(fp) == -1) {
ce_editor_message("unlink(%s): %s", fp, errno_s);
return;
}
}
const char *
ce_dirlist_index2path(struct cebuf *buf, size_t index)
{
int len;
struct dlist *list;
char fpath[PATH_MAX];
list = buf->intdata;
if (index >= list->nelm)
return (0);
len = snprintf(fpath, sizeof(fpath), "%s/%s",
buf->path, list->entries[index].vpath);
if (len == -1 || (size_t)len >= sizeof(fpath)) {
fatal("%s: failed to construct %s/%s",
__func__, buf->path, list->entries[index].vpath);
}
return (ce_editor_fullpath(fpath));
}
mode_t
ce_dirlist_index2mode(struct cebuf *buf, size_t index)
{
struct dlist *list;
list = buf->intdata;
if (index >= list->nelm)
return (0);
return (list->entries[index].vmode);
}
static void
dirlist_load(struct cebuf *buf, const char *path)
{
FTS *fts;
FTSENT *ent;
const char *name;
struct dlist *list;
union cp cp = { .cp = path };
size_t i, rootlen, cnt, len;
char *pathv[] = { cp.p, NULL };
if (buf->intdata == NULL)
fatal("%s: no dirlist attached to '%s'", __func__, buf->name);
fts = fts_open(pathv,
FTS_NOCHDIR | FTS_PHYSICAL | FTS_XDEV, dirlist_cmp);
if (fts == NULL) {
ce_editor_message("cannot read '%s': %s", path, errno_s);
return;
}
cnt = 0;
list = buf->intdata;
rootlen = strlen(path) + 1;
while ((ent = fts_read(fts)) != NULL) {
if (!strcmp(ent->fts_accpath, path))
continue;
if (S_ISDIR(ent->fts_statp->st_mode))
continue;
name = ent->fts_path + rootlen;
for (i = 0; ignored[i] != NULL; i++) {
if (fnmatch(ignored[i],
name, FNM_NOESCAPE | FNM_CASEFOLD) == 0)
break;
}
if (ignored[i] != NULL)
continue;
if (cnt >= list->nelm) {
len = (list->nelm + 64) * sizeof(struct dentry);
list->entries = realloc(list->entries, len);
if (list->entries == NULL)
fatal("%s: calloc (%zu)", __func__, len);
list->nelm += 64;
}
list->entries[cnt].path = ce_strdup(name);
list->entries[cnt].mode = ent->fts_statp->st_mode;
list->entries[cnt].vpath = list->entries[cnt].path;
list->entries[cnt].vmode = list->entries[cnt].mode;
cnt++;
}
list->nelm = cnt;
fts_close(fts);
ce_editor_message("loaded directory '%s'", path);
}
static void
dirlist_tobuf(struct cebuf *buf, const char *match)
{
int len;
struct dlist *list;
struct dentry *entry;
size_t idx, line;
char title[PATH_MAX], pattern[PATH_MAX];
if (buf->intdata == NULL)
fatal("%s: no dirlist attached to '%s'", __func__, buf->name);
if (match != NULL) {
len = snprintf(pattern, sizeof(pattern), "*%s*", match);
if (len == -1 || (size_t)len >= sizeof(pattern))
fatal("%s: failed to construct pattern", __func__);
}
len = snprintf(title, sizeof(title), "<dir %s>", buf->path);
if (len == -1 || (size_t)len >= sizeof(title))
fatal("%s: failed to construct buf title", __func__);
ce_buffer_erase(buf);
ce_buffer_setname(buf, title);
list = buf->intdata;
len = snprintf(title, sizeof(title), "Directory listing for '%s'\n",
list->path);
if (len == -1 || (size_t)len >= sizeof(title))
fatal("%s: failed to construct title", __func__);
ce_buffer_appendl(buf, title, len);
if (match) {
len = snprintf(title, sizeof(title), "filter '%s'\n", match);
if (len == -1 || (size_t)len >= sizeof(title))
fatal("%s: failed to construct title", __func__);
ce_buffer_appendl(buf, title, len);
ce_buffer_appendl(buf, "\n", 1);
} else {
ce_buffer_appendl(buf, "\n", 1);
ce_buffer_appendl(buf, "\n", 1);
}
line = 0;
for (idx = 0; idx < list->nelm; idx++) {
entry = &list->entries[idx];
if (match) {
if (fnmatch(pattern,
entry->path, FNM_NOESCAPE | FNM_CASEFOLD) == 0)
entry->flags &= ~DENTRY_FLAG_HIDDEN;
else
entry->flags = DENTRY_FLAG_HIDDEN;
} else {
entry->flags &= ~DENTRY_FLAG_HIDDEN;
}
if (entry->flags & DENTRY_FLAG_HIDDEN)
continue;
len = snprintf(title, sizeof(title), "%o %s\n",
entry->mode, entry->path);
if (len == -1 || (size_t)len >= sizeof(title))
fatal("%s: snprintf failed", __func__);
ce_buffer_appendl(buf, title, len);
list->entries[line].vpath = entry->path;
list->entries[line].vmode = entry->mode;
line++;
}
ce_editor_dirty();
ce_buffer_jump_line(buf, TERM_CURSOR_MIN, TERM_CURSOR_MIN);
}
static int
dirlist_cmp(const FTSENT **a1, const FTSENT **b1)
{
const FTSENT *a = *a1;
const FTSENT *b = *b1;
return (strcmp(a->fts_name, b->fts_name));
}