]> git.proxmox.com Git - ceph.git/blob - ceph/src/pmdk/src/core/fs_posix.c
import ceph 16.2.7
[ceph.git] / ceph / src / pmdk / src / core / fs_posix.c
1 // SPDX-License-Identifier: BSD-3-Clause
2 /* Copyright 2017-2020, Intel Corporation */
3
4 /*
5 * fs_posix.c -- file system traversal Posix implementation
6 */
7
8 #include <fts.h>
9 #include "util.h"
10 #include "out.h"
11 #include "vec.h"
12 #include "fs.h"
13
14 struct fs {
15 FTS *ft;
16 struct fs_entry entry;
17 };
18
19 /*
20 * fs_new -- creates fs traversal instance
21 */
22 struct fs *
23 fs_new(const char *path)
24 {
25 struct fs *f = Zalloc(sizeof(*f));
26 if (f == NULL)
27 goto error_fs_alloc;
28
29 const char *paths[2] = {path, NULL};
30 f->ft = fts_open((char * const *)paths, FTS_COMFOLLOW | FTS_XDEV, NULL);
31 if (f->ft == NULL)
32 goto error_fts_open;
33
34 return f;
35
36 error_fts_open:
37 Free(f);
38 error_fs_alloc:
39 return NULL;
40 }
41
42 /*
43 * fs_read -- reads an entry from the fs path
44 */
45 struct fs_entry *
46 fs_read(struct fs *f)
47 {
48 FTSENT *entry = fts_read(f->ft);
49 if (entry == NULL)
50 return NULL;
51
52 switch (entry->fts_info) {
53 case FTS_D:
54 f->entry.type = FS_ENTRY_DIRECTORY;
55 break;
56 case FTS_F:
57 f->entry.type = FS_ENTRY_FILE;
58 break;
59 case FTS_SL:
60 f->entry.type = FS_ENTRY_SYMLINK;
61 break;
62 default:
63 f->entry.type = FS_ENTRY_OTHER;
64 break;
65 }
66
67 f->entry.name = entry->fts_name;
68 f->entry.namelen = entry->fts_namelen;
69 f->entry.path = entry->fts_path;
70 f->entry.pathlen = entry->fts_pathlen;
71 f->entry.level = entry->fts_level;
72
73 return &f->entry;
74 }
75
76 /*
77 * fs_delete -- deletes a fs traversal instance
78 */
79 void
80 fs_delete(struct fs *f)
81 {
82 fts_close(f->ft);
83 Free(f);
84 }