summaryrefslogtreecommitdiff
path: root/src/third_party/wiredtiger/src/include/os_fs_inline.h
blob: 2276f096312f458878d3ed80d2236cd48d515abb (plain)
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
/*-
 * Copyright (c) 2014-present MongoDB, Inc.
 * Copyright (c) 2008-2014 WiredTiger, Inc.
 *	All rights reserved.
 *
 * See the file LICENSE for redistribution information.
 */

/*
 * __wt_fs_file_system --
 *     Get the active file system handle.
 */
static inline WT_FILE_SYSTEM *
__wt_fs_file_system(WT_SESSION_IMPL *session)
{
    return (S2FS(session));
}

/*
 * __wt_fs_directory_list --
 *     Return a list of files from a directory.
 */
static inline int
__wt_fs_directory_list(
  WT_SESSION_IMPL *session, const char *dir, const char *prefix, char ***dirlistp, u_int *countp)
{
    WT_DECL_RET;
    WT_FILE_SYSTEM *file_system;
    WT_SESSION *wt_session;
    char *path;

    *dirlistp = NULL;
    *countp = 0;

    __wt_verbose(session, WT_VERB_FILEOPS, "%s: directory-list: prefix %s", dir,
      prefix == NULL ? "all" : prefix);

    WT_RET(__wt_filename(session, dir, &path));

    file_system = __wt_fs_file_system(session);
    wt_session = (WT_SESSION *)session;
    ret = file_system->fs_directory_list(file_system, wt_session, path, prefix, dirlistp, countp);

    __wt_free(session, path);
    return (ret);
}

/*
 * __wt_fs_directory_list_single --
 *     Return a single matching file from a directory.
 */
static inline int
__wt_fs_directory_list_single(
  WT_SESSION_IMPL *session, const char *dir, const char *prefix, char ***dirlistp, u_int *countp)
{
    WT_DECL_RET;
    WT_FILE_SYSTEM *file_system;
    WT_SESSION *wt_session;
    char *path;

    *dirlistp = NULL;
    *countp = 0;

    __wt_verbose(session, WT_VERB_FILEOPS, "%s: directory-list-single: prefix %s", dir,
      prefix == NULL ? "all" : prefix);

    WT_RET(__wt_filename(session, dir, &path));

    file_system = __wt_fs_file_system(session);
    wt_session = (WT_SESSION *)session;
    ret = file_system->fs_directory_list_single(
      file_system, wt_session, path, prefix, dirlistp, countp);

    __wt_free(session, path);
    return (ret);
}

/*
 * __wt_fs_directory_list_free --
 *     Free memory allocated by __wt_fs_directory_list.
 */
static inline int
__wt_fs_directory_list_free(WT_SESSION_IMPL *session, char ***dirlistp, u_int count)
{
    WT_DECL_RET;
    WT_FILE_SYSTEM *file_system;
    WT_SESSION *wt_session;

    if (*dirlistp != NULL) {
        file_system = __wt_fs_file_system(session);
        wt_session = (WT_SESSION *)session;
        ret = file_system->fs_directory_list_free(file_system, wt_session, *dirlistp, count);
    }

    *dirlistp = NULL;
    return (ret);
}

/*
 * __wt_fs_exist --
 *     Return if the file exists.
 */
static inline int
__wt_fs_exist(WT_SESSION_IMPL *session, const char *name, bool *existp)
{
    WT_DECL_RET;
    WT_FILE_SYSTEM *file_system;
    WT_SESSION *wt_session;
    char *path;

    __wt_verbose(session, WT_VERB_FILEOPS, "%s: file-exist", name);

    WT_RET(__wt_filename(session, name, &path));

    file_system = __wt_fs_file_system(session);
    wt_session = (WT_SESSION *)session;
    ret = file_system->fs_exist(file_system, wt_session, path, existp);

    __wt_free(session, path);
    return (ret);
}

/*
 * __wt_fs_remove --
 *     Remove the file.
 */
static inline int
__wt_fs_remove(WT_SESSION_IMPL *session, const char *name, bool durable)
{
    WT_DECL_RET;
    WT_FILE_SYSTEM *file_system;
    WT_SESSION *wt_session;
    char *path;

    WT_ASSERT(session, !F_ISSET(S2C(session), WT_CONN_READONLY));

    __wt_verbose(session, WT_VERB_FILEOPS, "%s: file-remove", name);

#ifdef HAVE_DIAGNOSTIC
    /*
     * It is a layering violation to retrieve a WT_FH here, but it is a useful diagnostic to ensure
     * WiredTiger doesn't have the handle open.
     */
    if (__wt_handle_is_open(session, name))
        WT_RET_MSG(session, EINVAL, "%s: file-remove: file has open handles", name);
#endif

    WT_RET(__wt_filename(session, name, &path));

    file_system = __wt_fs_file_system(session);
    wt_session = (WT_SESSION *)session;
    ret = file_system->fs_remove(file_system, wt_session, path, durable ? WT_FS_DURABLE : 0);

    __wt_free(session, path);
    return (ret);
}

/*
 * __wt_fs_rename --
 *     Rename the file.
 */
static inline int
__wt_fs_rename(WT_SESSION_IMPL *session, const char *from, const char *to, bool durable)
{
    WT_DECL_RET;
    WT_FILE_SYSTEM *file_system;
    WT_SESSION *wt_session;
    char *from_path, *to_path;

    WT_ASSERT(session, !F_ISSET(S2C(session), WT_CONN_READONLY));

    __wt_verbose(session, WT_VERB_FILEOPS, "%s to %s: file-rename", from, to);

#ifdef HAVE_DIAGNOSTIC
    /*
     * It is a layering violation to retrieve a WT_FH here, but it is a useful diagnostic to ensure
     * WiredTiger doesn't have the handle open.
     */
    if (__wt_handle_is_open(session, from))
        WT_RET_MSG(session, EINVAL, "%s: file-rename: file has open handles", from);
    if (__wt_handle_is_open(session, to))
        WT_RET_MSG(session, EINVAL, "%s: file-rename: file has open handles", to);
#endif

    from_path = to_path = NULL;
    WT_ERR(__wt_filename(session, from, &from_path));
    WT_ERR(__wt_filename(session, to, &to_path));

    file_system = __wt_fs_file_system(session);
    wt_session = (WT_SESSION *)session;
    ret = file_system->fs_rename(
      file_system, wt_session, from_path, to_path, durable ? WT_FS_DURABLE : 0);

err:
    __wt_free(session, from_path);
    __wt_free(session, to_path);
    return (ret);
}

/*
 * __wt_fs_size --
 *     Return the size of a file in bytes, by file name.
 */
static inline int
__wt_fs_size(WT_SESSION_IMPL *session, const char *name, wt_off_t *sizep)
{
    WT_DECL_RET;
    WT_FILE_SYSTEM *file_system;
    WT_SESSION *wt_session;
    char *path;

    __wt_verbose(session, WT_VERB_FILEOPS, "%s: file-size", name);

    WT_RET(__wt_filename(session, name, &path));

    file_system = __wt_fs_file_system(session);
    wt_session = (WT_SESSION *)session;
    ret = file_system->fs_size(file_system, wt_session, path, sizep);

    __wt_free(session, path);
    return (ret);
}