vfs_io_uring: move error handling out of vfs_io_uring_pread_recv()
[Samba.git] / source3 / libsmb / libsmb_stat.c
blobf20f79579e2caa4ec2c761c0397936e58837eb31
1 /*
2 Unix SMB/Netbios implementation.
3 SMB client library implementation
4 Copyright (C) Andrew Tridgell 1998
5 Copyright (C) Richard Sharpe 2000, 2002
6 Copyright (C) John Terpstra 2000
7 Copyright (C) Tom Jansen (Ninja ISD) 2002
8 Copyright (C) Derrell Lipman 2003-2008
9 Copyright (C) Jeremy Allison 2007, 2008
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 3 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 #include "includes.h"
26 #include "libsmb/libsmb.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
29 #include "../libcli/smb/smbXcli_base.h"
32 * Generate an inode number from file name for those things that need it
35 static ino_t generate_inode(const char *name)
37 if (name == NULL) {
38 return (ino_t)-1;
40 return (ino_t)str_checksum(name);
44 * Routine to put basic stat info into a stat structure ... Used by stat and
45 * fstat below.
48 void setup_stat(struct stat *st,
49 const char *fname,
50 off_t size,
51 int mode,
52 ino_t ino,
53 dev_t dev,
54 struct timespec access_time_ts,
55 struct timespec change_time_ts,
56 struct timespec write_time_ts)
58 st->st_mode = 0;
60 if (IS_DOS_DIR(mode)) {
61 st->st_mode = SMBC_DIR_MODE;
62 } else {
63 st->st_mode = SMBC_FILE_MODE;
66 if (IS_DOS_ARCHIVE(mode)) {
67 st->st_mode |= S_IXUSR;
69 if (IS_DOS_SYSTEM(mode)) {
70 st->st_mode |= S_IXGRP;
72 if (IS_DOS_HIDDEN(mode)) {
73 st->st_mode |= S_IXOTH;
75 if (!IS_DOS_READONLY(mode)) {
76 st->st_mode |= S_IWUSR;
79 st->st_size = size;
80 #ifdef HAVE_STAT_ST_BLKSIZE
81 st->st_blksize = 512;
82 #endif
83 #ifdef HAVE_STAT_ST_BLOCKS
84 st->st_blocks = (size+511)/512;
85 #endif
86 #ifdef HAVE_STRUCT_STAT_ST_RDEV
87 st->st_rdev = 0;
88 #endif
89 st->st_uid = getuid();
90 st->st_gid = getgid();
92 if (IS_DOS_DIR(mode)) {
93 st->st_nlink = 2;
94 } else {
95 st->st_nlink = 1;
98 if (ino != 0) {
99 st->st_ino = ino;
100 } else {
101 st->st_ino = generate_inode(fname);
104 st->st_dev = dev;
105 st->st_atime = convert_timespec_to_time_t(access_time_ts);
106 st->st_ctime = convert_timespec_to_time_t(change_time_ts);
107 st->st_mtime = convert_timespec_to_time_t(write_time_ts);
110 void setup_stat_from_stat_ex(const struct stat_ex *stex,
111 const char *fname,
112 struct stat *st)
114 st->st_atime = convert_timespec_to_time_t(stex->st_ex_atime);
115 st->st_ctime = convert_timespec_to_time_t(stex->st_ex_ctime);
116 st->st_mtime = convert_timespec_to_time_t(stex->st_ex_mtime);
118 st->st_mode = stex->st_ex_mode;
119 st->st_size = stex->st_ex_size;
120 #ifdef HAVE_STAT_ST_BLKSIZE
121 st->st_blksize = 512;
122 #endif
123 #ifdef HAVE_STAT_ST_BLOCKS
124 st->st_blocks = (st->st_size + 511) / 512;
125 #endif
126 #ifdef HAVE_STRUCT_STAT_ST_RDEV
127 st->st_rdev = 0;
128 #endif
129 st->st_uid = stex->st_ex_uid;
130 st->st_gid = stex->st_ex_gid;
132 st->st_nlink = stex->st_ex_nlink;
134 if (stex->st_ex_ino == 0) {
135 st->st_ino = 0;
136 if (fname != NULL) {
137 st->st_ino = generate_inode(fname);
139 } else {
140 st->st_ino = stex->st_ex_ino;
143 st->st_dev = stex->st_ex_dev;
148 * Routine to stat a file given a name
152 SMBC_stat_ctx(SMBCCTX *context,
153 const char *fname,
154 struct stat *st)
156 SMBCSRV *srv = NULL;
157 char *server = NULL;
158 char *share = NULL;
159 char *user = NULL;
160 char *password = NULL;
161 char *workgroup = NULL;
162 char *path = NULL;
163 uint16_t port = 0;
164 TALLOC_CTX *frame = talloc_stackframe();
166 if (!context || !context->internal->initialized) {
167 errno = EINVAL; /* Best I can think of ... */
168 TALLOC_FREE(frame);
169 return -1;
172 if (!fname) {
173 errno = EINVAL;
174 TALLOC_FREE(frame);
175 return -1;
178 DEBUG(4, ("smbc_stat(%s)\n", fname));
180 if (SMBC_parse_path(frame,
181 context,
182 fname,
183 &workgroup,
184 &server,
185 &port,
186 &share,
187 &path,
188 &user,
189 &password,
190 NULL)) {
191 errno = EINVAL;
192 TALLOC_FREE(frame);
193 return -1;
196 if (!user || user[0] == (char)0) {
197 user = talloc_strdup(frame, smbc_getUser(context));
198 if (!user) {
199 errno = ENOMEM;
200 TALLOC_FREE(frame);
201 return -1;
205 srv = SMBC_server(frame, context, True,
206 server, port, share, &workgroup, &user, &password);
207 if (!srv) {
208 TALLOC_FREE(frame);
209 return -1; /* errno set by SMBC_server */
212 if (!SMBC_getatr(context, srv, path, st)) {
213 errno = SMBC_errno(context, srv->cli);
214 TALLOC_FREE(frame);
215 return -1;
218 TALLOC_FREE(frame);
219 return 0;
223 * Routine to stat a file given an fd
227 SMBC_fstat_ctx(SMBCCTX *context,
228 SMBCFILE *file,
229 struct stat *st)
231 struct timespec change_time_ts;
232 struct timespec access_time_ts;
233 struct timespec write_time_ts;
234 off_t size;
235 uint16_t mode;
236 char *server = NULL;
237 char *share = NULL;
238 char *user = NULL;
239 char *password = NULL;
240 char *path = NULL;
241 char *targetpath = NULL;
242 struct cli_state *targetcli = NULL;
243 SMB_INO_T ino = 0;
244 uint16_t port = 0;
245 TALLOC_CTX *frame = talloc_stackframe();
246 NTSTATUS status;
248 if (!context || !context->internal->initialized) {
249 errno = EINVAL;
250 TALLOC_FREE(frame);
251 return -1;
254 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
255 errno = EBADF;
256 TALLOC_FREE(frame);
257 return -1;
260 if (!file->file) {
261 TALLOC_FREE(frame);
262 return smbc_getFunctionFstatdir(context)(context, file, st);
265 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
266 if (SMBC_parse_path(frame,
267 context,
268 file->fname,
269 NULL,
270 &server,
271 &port,
272 &share,
273 &path,
274 &user,
275 &password,
276 NULL)) {
277 errno = EINVAL;
278 TALLOC_FREE(frame);
279 return -1;
282 /*d_printf(">>>fstat: resolving %s\n", path);*/
283 status = cli_resolve_path(frame, "", context->internal->auth_info,
284 file->srv->cli, path,
285 &targetcli, &targetpath);
286 if (!NT_STATUS_IS_OK(status)) {
287 d_printf("Could not resolve %s\n", path);
288 errno = ENOENT;
289 TALLOC_FREE(frame);
290 return -1;
292 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
294 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
295 targetcli, file->cli_fd, &mode, &size,
296 NULL,
297 &access_time_ts,
298 &write_time_ts,
299 &change_time_ts,
300 &ino))) {
301 time_t change_time, access_time, write_time;
303 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd, &mode, &size,
304 &change_time, &access_time, &write_time))) {
305 errno = EINVAL;
306 TALLOC_FREE(frame);
307 return -1;
309 change_time_ts = convert_time_t_to_timespec(change_time);
310 access_time_ts = convert_time_t_to_timespec(access_time);
311 write_time_ts = convert_time_t_to_timespec(write_time);
314 setup_stat(st,
315 path,
316 size,
317 mode,
318 ino,
319 file->srv->dev,
320 access_time_ts,
321 change_time_ts,
322 write_time_ts);
324 TALLOC_FREE(frame);
325 return 0;
330 * Routine to obtain file system information given a path
333 SMBC_statvfs_ctx(SMBCCTX *context,
334 char *path,
335 struct statvfs *st)
337 int ret;
338 bool bIsDir;
339 struct stat statbuf;
340 SMBCFILE * pFile;
341 TALLOC_CTX *frame = talloc_stackframe();
343 /* Determine if the provided path is a file or a folder */
344 if (SMBC_stat_ctx(context, path, &statbuf) < 0) {
345 TALLOC_FREE(frame);
346 return -1;
349 /* Is it a file or a directory? */
350 if (S_ISDIR(statbuf.st_mode)) {
351 /* It's a directory. */
352 if ((pFile = SMBC_opendir_ctx(context, path)) == NULL) {
353 TALLOC_FREE(frame);
354 return -1;
356 bIsDir = true;
357 } else if (S_ISREG(statbuf.st_mode)) {
358 /* It's a file. */
359 if ((pFile = SMBC_open_ctx(context, path,
360 O_RDONLY, 0)) == NULL) {
361 TALLOC_FREE(frame);
362 return -1;
364 bIsDir = false;
365 } else {
366 /* It's neither a file nor a directory. Not supported. */
367 TALLOC_FREE(frame);
368 errno = ENOSYS;
369 return -1;
372 /* Now we have an open file handle, so just use SMBC_fstatvfs */
373 ret = SMBC_fstatvfs_ctx(context, pFile, st);
375 /* Close the file or directory */
376 if (bIsDir) {
377 SMBC_closedir_ctx(context, pFile);
378 } else {
379 SMBC_close_ctx(context, pFile);
382 TALLOC_FREE(frame);
383 return ret;
388 * Routine to obtain file system information given an fd
392 SMBC_fstatvfs_ctx(SMBCCTX *context,
393 SMBCFILE *file,
394 struct statvfs *st)
396 unsigned long flags = 0;
397 uint32_t fs_attrs = 0;
398 struct cli_state *cli = file->srv->cli;
399 struct smbXcli_tcon *tcon;
400 TALLOC_CTX *frame = talloc_stackframe();
402 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
403 tcon = cli->smb2.tcon;
404 } else {
405 tcon = cli->smb1.tcon;
408 /* Initialize all fields (at least until we actually use them) */
409 memset(st, 0, sizeof(*st));
412 * The state of each flag is such that the same bits are unset as
413 * would typically be unset on a local file system on a POSIX OS. Thus
414 * the bit is on, for example, only for case-insensitive file systems
415 * since most POSIX file systems are case sensitive and fstatvfs()
416 * would typically return zero in these bits on such a local file
417 * system.
420 /* See if the server has UNIX CIFS support */
421 if (! SERVER_HAS_UNIX_CIFS(cli)) {
422 uint64_t total_allocation_units;
423 uint64_t caller_allocation_units;
424 uint64_t actual_allocation_units;
425 uint64_t sectors_per_allocation_unit;
426 uint64_t bytes_per_sector;
427 NTSTATUS status;
429 /* Nope. If size data is available... */
430 status = cli_get_fs_full_size_info(cli,
431 &total_allocation_units,
432 &caller_allocation_units,
433 &actual_allocation_units,
434 &sectors_per_allocation_unit,
435 &bytes_per_sector);
436 if (NT_STATUS_IS_OK(status)) {
438 /* ... then provide it */
439 st->f_bsize =
440 (unsigned long) bytes_per_sector;
441 #ifdef HAVE_FRSIZE
442 st->f_frsize =
443 (unsigned long) sectors_per_allocation_unit;
444 #endif
445 st->f_blocks =
446 (fsblkcnt_t) total_allocation_units;
447 st->f_bfree =
448 (fsblkcnt_t) actual_allocation_units;
449 st->f_bavail =
450 (fsblkcnt_t) caller_allocation_units;
453 flags |= SMBC_VFS_FEATURE_NO_UNIXCIFS;
454 } else {
455 uint32_t optimal_transfer_size;
456 uint32_t block_size;
457 uint64_t total_blocks;
458 uint64_t blocks_available;
459 uint64_t user_blocks_available;
460 uint64_t total_file_nodes;
461 uint64_t free_file_nodes;
462 uint64_t fs_identifier;
463 NTSTATUS status;
465 /* Has UNIXCIFS. If POSIX filesystem info is available... */
466 status = cli_get_posix_fs_info(cli,
467 &optimal_transfer_size,
468 &block_size,
469 &total_blocks,
470 &blocks_available,
471 &user_blocks_available,
472 &total_file_nodes,
473 &free_file_nodes,
474 &fs_identifier);
475 if (NT_STATUS_IS_OK(status)) {
477 /* ... then what's provided here takes precedence. */
478 st->f_bsize =
479 (unsigned long) block_size;
480 st->f_blocks =
481 (fsblkcnt_t) total_blocks;
482 st->f_bfree =
483 (fsblkcnt_t) blocks_available;
484 st->f_bavail =
485 (fsblkcnt_t) user_blocks_available;
486 st->f_files =
487 (fsfilcnt_t) total_file_nodes;
488 st->f_ffree =
489 (fsfilcnt_t) free_file_nodes;
490 #ifdef HAVE_FSID_INT
491 st->f_fsid =
492 (unsigned long) fs_identifier;
493 #endif
497 /* See if the share is case sensitive */
498 if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(cli, &fs_attrs))) {
500 * We can't determine the case sensitivity of
501 * the share. We have no choice but to use the
502 * user-specified case sensitivity setting.
504 if (! smbc_getOptionCaseSensitive(context)) {
505 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
507 } else {
508 if (! (fs_attrs & FILE_CASE_SENSITIVE_SEARCH)) {
509 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
513 /* See if DFS is supported */
514 if (smbXcli_conn_dfs_supported(cli->conn) &&
515 smbXcli_tcon_is_dfs_share(tcon))
517 flags |= SMBC_VFS_FEATURE_DFS;
520 #if defined(HAVE_STATVFS_F_FLAG)
521 st->f_flag = flags;
522 #elif defined(HAVE_STATVFS_F_FLAGS)
523 st->f_flags = flags;
524 #endif
526 TALLOC_FREE(frame);
527 return 0;