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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
|
/**
* Copyright (c) 2017, Timothy Stack
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* * Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* * Neither the name of Timothy Stack nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ''AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "fstat_vtab.hh"
#include <glob.h>
#include <grp.h>
#include <pwd.h>
#include <string.h>
#include <sys/stat.h>
#include "base/auto_mem.hh"
#include "base/fs_util.hh"
#include "base/injector.hh"
#include "base/lnav_log.hh"
#include "bound_tags.hh"
#include "config.h"
#include "ghc/filesystem.hpp"
#include "sql_help.hh"
#include "sql_util.hh"
#include "vtab_module.hh"
namespace {
enum {
FSTAT_COL_PARENT,
FSTAT_COL_NAME,
FSTAT_COL_DEV,
FSTAT_COL_INO,
FSTAT_COL_TYPE,
FSTAT_COL_MODE,
FSTAT_COL_NLINK,
FSTAT_COL_UID,
FSTAT_COL_USER,
FSTAT_COL_GID,
FSTAT_COL_GROUP,
FSTAT_COL_RDEV,
FSTAT_COL_SIZE,
FSTAT_COL_BLKSIZE,
FSTAT_COL_BLOCKS,
FSTAT_COL_ATIME,
FSTAT_COL_MTIME,
FSTAT_COL_CTIME,
FSTAT_COL_ERROR,
FSTAT_COL_PATTERN,
FSTAT_COL_DATA,
};
/**
* @feature f0:sql.tables.fstat
*/
struct fstat_table {
static constexpr const char* NAME = "fstat";
static constexpr const char* CREATE_STMT = R"(
-- The fstat() table-valued function allows you to query the file system.
CREATE TABLE fstat (
st_parent TEXT,
st_name TEXT,
st_dev INTEGER,
st_ino INTEGER,
st_type TEXT,
st_mode INTEGER,
st_nlink INTEGER,
st_uid TEXT,
st_user TEXT,
st_gid TEXT,
st_group TEXT,
st_rdev INTEGER,
st_size INTEGER,
st_blksize INTEGER,
st_blocks INTEGER,
st_atime DATETIME,
st_mtime DATETIME,
st_ctime DATETIME,
error TEXT,
pattern TEXT HIDDEN,
data BLOB HIDDEN
);
)";
struct cursor {
sqlite3_vtab_cursor base;
std::string c_pattern;
static_root_mem<glob_t, globfree> c_glob;
size_t c_path_index{0};
struct stat c_stat;
std::string c_error;
explicit cursor(sqlite3_vtab* vt) : base({vt})
{
memset(&this->c_stat, 0, sizeof(this->c_stat));
}
void load_stat()
{
auto rc = lstat(this->c_glob->gl_pathv[this->c_path_index],
&this->c_stat);
if (rc == -1) {
this->c_error = strerror(errno);
}
}
int next()
{
if (this->c_path_index < this->c_glob->gl_pathc) {
this->c_path_index += 1;
this->load_stat();
}
return SQLITE_OK;
}
int reset() { return SQLITE_OK; }
int eof() { return this->c_path_index >= this->c_glob->gl_pathc; }
int get_rowid(sqlite3_int64& rowid_out)
{
rowid_out = this->c_path_index;
return SQLITE_OK;
}
};
int get_column(const cursor& vc, sqlite3_context* ctx, int col)
{
const char* path = vc.c_glob->gl_pathv[vc.c_path_index];
char time_buf[32];
switch (col) {
case FSTAT_COL_PARENT: {
const char* slash = strrchr(path, '/');
if (slash == nullptr) {
sqlite3_result_text(ctx, ".", 1, SQLITE_STATIC);
} else if (path[1] == '\0') {
sqlite3_result_text(ctx, "", 0, SQLITE_STATIC);
} else {
sqlite3_result_text(
ctx, path, slash - path + 1, SQLITE_TRANSIENT);
}
break;
}
case FSTAT_COL_NAME: {
const char* slash = strrchr(path, '/');
if (slash == nullptr) {
sqlite3_result_text(ctx, path, -1, SQLITE_TRANSIENT);
} else {
sqlite3_result_text(ctx, slash + 1, -1, SQLITE_TRANSIENT);
}
break;
}
case FSTAT_COL_DEV:
if (vc.c_error.empty()) {
sqlite3_result_int(ctx, vc.c_stat.st_dev);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_INO:
if (vc.c_error.empty()) {
sqlite3_result_int64(ctx, vc.c_stat.st_ino);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_TYPE:
if (!vc.c_error.empty()) {
sqlite3_result_null(ctx);
} else if (S_ISREG(vc.c_stat.st_mode)) {
sqlite3_result_text(ctx, "reg", 3, SQLITE_STATIC);
} else if (S_ISBLK(vc.c_stat.st_mode)) {
sqlite3_result_text(ctx, "blk", 3, SQLITE_STATIC);
} else if (S_ISCHR(vc.c_stat.st_mode)) {
sqlite3_result_text(ctx, "chr", 3, SQLITE_STATIC);
} else if (S_ISDIR(vc.c_stat.st_mode)) {
sqlite3_result_text(ctx, "dir", 3, SQLITE_STATIC);
} else if (S_ISFIFO(vc.c_stat.st_mode)) {
sqlite3_result_text(ctx, "fifo", 4, SQLITE_STATIC);
} else if (S_ISLNK(vc.c_stat.st_mode)) {
sqlite3_result_text(ctx, "lnk", 3, SQLITE_STATIC);
} else if (S_ISSOCK(vc.c_stat.st_mode)) {
sqlite3_result_text(ctx, "sock", 3, SQLITE_STATIC);
}
break;
case FSTAT_COL_MODE:
if (vc.c_error.empty()) {
sqlite3_result_int(ctx, vc.c_stat.st_mode & 0777);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_NLINK:
if (vc.c_error.empty()) {
sqlite3_result_int(ctx, vc.c_stat.st_nlink);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_UID:
if (vc.c_error.empty()) {
sqlite3_result_int(ctx, vc.c_stat.st_uid);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_USER: {
if (vc.c_error.empty()) {
struct passwd* pw = getpwuid(vc.c_stat.st_uid);
if (pw != nullptr) {
sqlite3_result_text(
ctx, pw->pw_name, -1, SQLITE_TRANSIENT);
} else {
sqlite3_result_int(ctx, vc.c_stat.st_uid);
}
} else {
sqlite3_result_null(ctx);
}
break;
}
case FSTAT_COL_GID:
if (vc.c_error.empty()) {
sqlite3_result_int(ctx, vc.c_stat.st_gid);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_GROUP: {
if (vc.c_error.empty()) {
struct group* gr = getgrgid(vc.c_stat.st_gid);
if (gr != nullptr) {
sqlite3_result_text(
ctx, gr->gr_name, -1, SQLITE_TRANSIENT);
} else {
sqlite3_result_int(ctx, vc.c_stat.st_gid);
}
} else {
sqlite3_result_null(ctx);
}
break;
}
case FSTAT_COL_RDEV:
if (vc.c_error.empty()) {
sqlite3_result_int(ctx, vc.c_stat.st_rdev);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_SIZE:
if (vc.c_error.empty()) {
sqlite3_result_int64(ctx, vc.c_stat.st_size);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_BLKSIZE:
if (vc.c_error.empty()) {
sqlite3_result_int(ctx, vc.c_stat.st_blksize);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_BLOCKS:
if (vc.c_error.empty()) {
sqlite3_result_int(ctx, vc.c_stat.st_blocks);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_ATIME:
if (vc.c_error.empty()) {
sql_strftime(
time_buf, sizeof(time_buf), vc.c_stat.st_atime, 0);
sqlite3_result_text(ctx, time_buf, -1, SQLITE_TRANSIENT);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_MTIME:
if (vc.c_error.empty()) {
sql_strftime(
time_buf, sizeof(time_buf), vc.c_stat.st_mtime, 0);
sqlite3_result_text(ctx, time_buf, -1, SQLITE_TRANSIENT);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_CTIME:
if (vc.c_error.empty()) {
sql_strftime(
time_buf, sizeof(time_buf), vc.c_stat.st_ctime, 0);
sqlite3_result_text(ctx, time_buf, -1, SQLITE_TRANSIENT);
} else {
sqlite3_result_null(ctx);
}
break;
case FSTAT_COL_ERROR:
if (vc.c_error.empty()) {
sqlite3_result_null(ctx);
} else {
to_sqlite(ctx, vc.c_error);
}
break;
case FSTAT_COL_PATTERN:
sqlite3_result_text(ctx,
vc.c_pattern.c_str(),
vc.c_pattern.length(),
SQLITE_TRANSIENT);
break;
case FSTAT_COL_DATA: {
auto fs_path = ghc::filesystem::path{path};
if (!vc.c_error.empty()) {
sqlite3_result_null(ctx);
} else if (S_ISREG(vc.c_stat.st_mode)) {
auto open_res
= lnav::filesystem::open_file(fs_path, O_RDONLY);
if (open_res.isErr()) {
log_error("unable to read file: %s -- %s",
path,
open_res.unwrapErr().c_str());
sqlite3_result_null(ctx);
} else {
auto buffer = auto_buffer::alloc(vc.c_stat.st_size);
auto fd = open_res.unwrap();
while (true) {
if (buffer.available() == 0) {
buffer.expand_by(4096);
}
auto rc = read(fd,
buffer.next_available(),
buffer.available());
if (rc <= 0) {
break;
}
buffer.resize_by(rc);
}
to_sqlite(ctx, blob_auto_buffer{std::move(buffer)});
}
} else if (S_ISLNK(vc.c_stat.st_mode)) {
auto link_path = ghc::filesystem::read_symlink(fs_path);
to_sqlite(ctx, link_path.string());
} else {
sqlite3_result_null(ctx);
}
break;
}
}
return SQLITE_OK;
}
#if 0
int update_row(sqlite3_vtab *tab,
sqlite3_int64 &index,
const char *st_parent,
const char *st_name,
int64_t st_dev,
int64_t st_ino,
const char *st_type,
int64_t st_mode,
int64_t st_nlink,
int64_t st_uid,
const char *st_user,
int64_t st_gid,
const char *st_group,
int64_t st_rdev,
int64_t st_size,
int64_t st_blksize,
int64_t st_blocks,
const char *atime,
const char *mtime,
const char *ctime,
const char *pattern) {
};
#endif
};
static int
rcBestIndex(sqlite3_vtab* tab, sqlite3_index_info* pIdxInfo)
{
vtab_index_constraints vic(pIdxInfo);
vtab_index_usage viu(pIdxInfo);
for (auto iter = vic.begin(); iter != vic.end(); ++iter) {
if (iter->op != SQLITE_INDEX_CONSTRAINT_EQ) {
continue;
}
switch (iter->iColumn) {
case FSTAT_COL_PATTERN:
viu.column_used(iter);
break;
}
}
viu.allocate_args(FSTAT_COL_PATTERN, FSTAT_COL_PATTERN, 1);
return SQLITE_OK;
}
static int
rcFilter(sqlite3_vtab_cursor* pVtabCursor,
int idxNum,
const char* idxStr,
int argc,
sqlite3_value** argv)
{
fstat_table::cursor* pCur = (fstat_table::cursor*) pVtabCursor;
if (argc != 1) {
pCur->c_pattern.clear();
return SQLITE_OK;
}
const char* pattern = (const char*) sqlite3_value_text(argv[0]);
pCur->c_pattern = pattern;
auto glob_flags = GLOB_ERR;
if (!lnav::filesystem::is_glob(pCur->c_pattern)) {
glob_flags |= GLOB_NOCHECK;
}
#ifdef GLOB_TILDE
glob_flags |= GLOB_TILDE;
#endif
switch (glob(pattern, glob_flags, nullptr, pCur->c_glob.inout())) {
case GLOB_NOSPACE:
pVtabCursor->pVtab->zErrMsg
= sqlite3_mprintf("No space to perform glob()");
return SQLITE_ERROR;
case GLOB_NOMATCH:
return SQLITE_OK;
}
pCur->load_stat();
return SQLITE_OK;
}
} // namespace
int
register_fstat_vtab(sqlite3* db)
{
static vtab_module<tvt_no_update<fstat_table>> FSTAT_MODULE;
static auto fstat_help
= help_text("fstat",
"A table-valued function for getting information about "
"file paths/globs")
.sql_table_valued_function()
.with_parameter(
{"pattern", "The file path or glob pattern to query."})
.with_result(
{"st_parent", "The parent path of the directory entry"})
.with_result({"st_name", "The name of the directory entry"})
.with_result({"st_dev", "The device number"})
.with_result({"st_ino", "The inode number"})
.with_result(help_text{"st_type", "The type of the entry"}
.with_enum_values({
"reg",
"blk",
"chr",
"dir",
"fifo",
"lnk",
"sock",
}))
.with_result({"st_mode", "The protection mode"})
.with_result(
{"st_nlink", "The number of hard links to the entry"})
.with_result({"st_uid", "The ID of the owning user"})
.with_result({"st_user", "The user name"})
.with_result({"st_gid", "The ID of the owning group"})
.with_result({"st_group", "The group name"})
.with_result({"st_rdev", "The device type"})
.with_result({"st_size", "The size of the entry in bytes"})
.with_result({"st_blksize", "The optimal size for I/O"})
.with_result({"st_blocks", "Blocks allocated for the file"})
.with_result({"st_atime", "The last access time"})
.with_result({"st_mtime", "The last modified time"})
.with_result({"st_ctime", "The creation time"})
.with_result({"error",
"Error message if there was a problem looking up "
"the entry"})
.with_result({"data", "The contents of the file"})
.with_example(help_example{
"To read a file and raise an error if there is a problem",
"SELECT ifnull(data, raise_error('cannot read: ' || st_name, "
"error)) FROM fstat('/non-existent')",
});
int rc;
FSTAT_MODULE.vm_module.xBestIndex = rcBestIndex;
FSTAT_MODULE.vm_module.xFilter = rcFilter;
static auto& lnav_flags = injector::get<unsigned long&, lnav_flags_tag>();
if (lnav_flags & LNF_SECURE_MODE) {
return SQLITE_OK;
}
rc = FSTAT_MODULE.create(db, "fstat");
sqlite_function_help.emplace("fstat", &fstat_help);
fstat_help.index_tags();
ensure(rc == SQLITE_OK);
return rc;
}
|