1 /*-
2  * Copyright (c) 2014-2018 MongoDB, Inc.
3  * Copyright (c) 2008-2014 WiredTiger, Inc.
4  *	All rights reserved.
5  *
6  * See the file LICENSE for redistribution information.
7  */
8 
9 /*
10  * __wt_fs_directory_list --
11  *	Return a list of files from a directory.
12  */
13 static inline int
__wt_fs_directory_list(WT_SESSION_IMPL * session,const char * dir,const char * prefix,char *** dirlistp,u_int * countp)14 __wt_fs_directory_list(WT_SESSION_IMPL *session,
15     const char *dir, const char *prefix, char ***dirlistp, u_int *countp)
16 {
17 	WT_DECL_RET;
18 	WT_FILE_SYSTEM *file_system;
19 	WT_SESSION *wt_session;
20 	char *path;
21 
22 	*dirlistp = NULL;
23 	*countp = 0;
24 
25 	__wt_verbose(session, WT_VERB_FILEOPS,
26 	    "%s: directory-list: prefix %s",
27 	    dir, prefix == NULL ? "all" : prefix);
28 
29 	WT_RET(__wt_filename(session, dir, &path));
30 
31 	file_system = S2C(session)->file_system;
32 	wt_session = (WT_SESSION *)session;
33 	ret = file_system->fs_directory_list(
34 	    file_system, wt_session, path, prefix, dirlistp, countp);
35 
36 	__wt_free(session, path);
37 	return (ret);
38 }
39 
40 /*
41  * __wt_fs_directory_list_single --
42  *	Return a single matching file from a directory.
43  */
44 static inline int
__wt_fs_directory_list_single(WT_SESSION_IMPL * session,const char * dir,const char * prefix,char *** dirlistp,u_int * countp)45 __wt_fs_directory_list_single(WT_SESSION_IMPL *session,
46     const char *dir, const char *prefix, char ***dirlistp, u_int *countp)
47 {
48 	WT_DECL_RET;
49 	WT_FILE_SYSTEM *file_system;
50 	WT_SESSION *wt_session;
51 	char *path;
52 
53 	*dirlistp = NULL;
54 	*countp = 0;
55 
56 	__wt_verbose(session, WT_VERB_FILEOPS,
57 	    "%s: directory-list-single: prefix %s",
58 	    dir, prefix == NULL ? "all" : prefix);
59 
60 	WT_RET(__wt_filename(session, dir, &path));
61 
62 	file_system = S2C(session)->file_system;
63 	wt_session = (WT_SESSION *)session;
64 	ret = file_system->fs_directory_list_single(
65 	    file_system, wt_session, path, prefix, dirlistp, countp);
66 
67 	__wt_free(session, path);
68 	return (ret);
69 }
70 
71 /*
72  * __wt_fs_directory_list_free --
73  *	Free memory allocated by __wt_fs_directory_list.
74  */
75 static inline int
__wt_fs_directory_list_free(WT_SESSION_IMPL * session,char *** dirlistp,u_int count)76 __wt_fs_directory_list_free(
77     WT_SESSION_IMPL *session, char ***dirlistp, u_int count)
78 {
79 	WT_DECL_RET;
80 	WT_FILE_SYSTEM *file_system;
81 	WT_SESSION *wt_session;
82 
83 	if (*dirlistp != NULL) {
84 		file_system = S2C(session)->file_system;
85 		wt_session = (WT_SESSION *)session;
86 		ret = file_system->fs_directory_list_free(
87 		    file_system, wt_session, *dirlistp, count);
88 	}
89 
90 	*dirlistp = NULL;
91 	return (ret);
92 }
93 
94 /*
95  * __wt_fs_exist --
96  *	Return if the file exists.
97  */
98 static inline int
__wt_fs_exist(WT_SESSION_IMPL * session,const char * name,bool * existp)99 __wt_fs_exist(WT_SESSION_IMPL *session, const char *name, bool *existp)
100 {
101 	WT_DECL_RET;
102 	WT_FILE_SYSTEM *file_system;
103 	WT_SESSION *wt_session;
104 	char *path;
105 
106 	__wt_verbose(session, WT_VERB_FILEOPS, "%s: file-exist", name);
107 
108 	WT_RET(__wt_filename(session, name, &path));
109 
110 	file_system = S2C(session)->file_system;
111 	wt_session = (WT_SESSION *)session;
112 	ret = file_system->fs_exist(file_system, wt_session, path, existp);
113 
114 	__wt_free(session, path);
115 	return (ret);
116 }
117 
118 /*
119  * __wt_fs_remove --
120  *	Remove the file.
121  */
122 static inline int
__wt_fs_remove(WT_SESSION_IMPL * session,const char * name,bool durable)123 __wt_fs_remove(WT_SESSION_IMPL *session, const char *name, bool durable)
124 {
125 	WT_DECL_RET;
126 	WT_FILE_SYSTEM *file_system;
127 	WT_SESSION *wt_session;
128 	char *path;
129 
130 	WT_ASSERT(session, !F_ISSET(S2C(session), WT_CONN_READONLY));
131 
132 	__wt_verbose(session, WT_VERB_FILEOPS, "%s: file-remove", name);
133 
134 #ifdef HAVE_DIAGNOSTIC
135 	/*
136 	 * It is a layering violation to retrieve a WT_FH here, but it is a
137 	 * useful diagnostic to ensure WiredTiger doesn't have the handle open.
138 	 */
139 	if (__wt_handle_is_open(session, name))
140 		WT_RET_MSG(session, EINVAL,
141 		    "%s: file-remove: file has open handles", name);
142 #endif
143 
144 	WT_RET(__wt_filename(session, name, &path));
145 
146 	file_system = S2C(session)->file_system;
147 	wt_session = (WT_SESSION *)session;
148 	ret = file_system->fs_remove(
149 	    file_system, wt_session, path, durable ? WT_FS_DURABLE : 0);
150 
151 	__wt_free(session, path);
152 	return (ret);
153 }
154 
155 /*
156  * __wt_fs_rename --
157  *	Rename the file.
158  */
159 static inline int
__wt_fs_rename(WT_SESSION_IMPL * session,const char * from,const char * to,bool durable)160 __wt_fs_rename(
161     WT_SESSION_IMPL *session, const char *from, const char *to, bool durable)
162 {
163 	WT_DECL_RET;
164 	WT_FILE_SYSTEM *file_system;
165 	WT_SESSION *wt_session;
166 	char *from_path, *to_path;
167 
168 	WT_ASSERT(session, !F_ISSET(S2C(session), WT_CONN_READONLY));
169 
170 	__wt_verbose(
171 	    session, WT_VERB_FILEOPS, "%s to %s: file-rename", from, to);
172 
173 #ifdef HAVE_DIAGNOSTIC
174 	/*
175 	 * It is a layering violation to retrieve a WT_FH here, but it is a
176 	 * useful diagnostic to ensure WiredTiger doesn't have the handle open.
177 	 */
178 	if (__wt_handle_is_open(session, from))
179 		WT_RET_MSG(session, EINVAL,
180 		    "%s: file-rename: file has open handles", from);
181 	if (__wt_handle_is_open(session, to))
182 		WT_RET_MSG(session, EINVAL,
183 		    "%s: file-rename: file has open handles", to);
184 #endif
185 
186 	from_path = to_path = NULL;
187 	WT_ERR(__wt_filename(session, from, &from_path));
188 	WT_ERR(__wt_filename(session, to, &to_path));
189 
190 	file_system = S2C(session)->file_system;
191 	wt_session = (WT_SESSION *)session;
192 	ret = file_system->fs_rename(file_system,
193 	    wt_session, from_path, to_path, durable ? WT_FS_DURABLE : 0);
194 
195 err:	__wt_free(session, from_path);
196 	__wt_free(session, to_path);
197 	return (ret);
198 }
199 
200 /*
201  * __wt_fs_size --
202  *	Return the size of a file in bytes, by file name.
203  */
204 static inline int
__wt_fs_size(WT_SESSION_IMPL * session,const char * name,wt_off_t * sizep)205 __wt_fs_size(WT_SESSION_IMPL *session, const char *name, wt_off_t *sizep)
206 {
207 	WT_DECL_RET;
208 	WT_FILE_SYSTEM *file_system;
209 	WT_SESSION *wt_session;
210 	char *path;
211 
212 	__wt_verbose(session, WT_VERB_FILEOPS, "%s: file-size", name);
213 
214 	WT_RET(__wt_filename(session, name, &path));
215 
216 	file_system = S2C(session)->file_system;
217 	wt_session = (WT_SESSION *)session;
218 	ret = file_system->fs_size(file_system, wt_session, path, sizep);
219 
220 	__wt_free(session, path);
221 	return (ret);
222 }
223