vfstest: Convert cmd_lstat to synthetic_smb_fname
[Samba/id10ts.git] / source3 / libsmb / libsmb_stat.c
blob5cfbcb842c58166c7961f2ad752b4c80a78306c5
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"
31 /*
32 * Generate an inode number from file name for those things that need it
35 static ino_t
36 generate_inode(SMBCCTX *context,
37 const char *name)
39 if (!context || !context->internal->initialized) {
40 errno = EINVAL;
41 return -1;
44 if (!*name) return 2; /* FIXME, why 2 ??? */
45 return (ino_t)str_checksum(name);
49 * Routine to put basic stat info into a stat structure ... Used by stat and
50 * fstat below.
53 static int
54 setup_stat(SMBCCTX *context,
55 struct stat *st,
56 const char *fname,
57 off_t size,
58 int mode)
60 TALLOC_CTX *frame = talloc_stackframe();
62 st->st_mode = 0;
64 if (IS_DOS_DIR(mode)) {
65 st->st_mode = SMBC_DIR_MODE;
66 } else {
67 st->st_mode = SMBC_FILE_MODE;
70 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
71 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
72 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
73 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
75 st->st_size = size;
76 #ifdef HAVE_STAT_ST_BLKSIZE
77 st->st_blksize = 512;
78 #endif
79 #ifdef HAVE_STAT_ST_BLOCKS
80 st->st_blocks = (size+511)/512;
81 #endif
82 #ifdef HAVE_STRUCT_STAT_ST_RDEV
83 st->st_rdev = 0;
84 #endif
85 st->st_uid = getuid();
86 st->st_gid = getgid();
88 if (IS_DOS_DIR(mode)) {
89 st->st_nlink = 2;
90 } else {
91 st->st_nlink = 1;
94 if (st->st_ino == 0) {
95 st->st_ino = generate_inode(context, fname);
98 TALLOC_FREE(frame);
99 return True; /* FIXME: Is this needed ? */
103 * Routine to stat a file given a name
107 SMBC_stat_ctx(SMBCCTX *context,
108 const char *fname,
109 struct stat *st)
111 SMBCSRV *srv = NULL;
112 char *server = NULL;
113 char *share = NULL;
114 char *user = NULL;
115 char *password = NULL;
116 char *workgroup = NULL;
117 char *path = NULL;
118 struct timespec write_time_ts;
119 struct timespec access_time_ts;
120 struct timespec change_time_ts;
121 off_t size = 0;
122 uint16 mode = 0;
123 SMB_INO_T ino = 0;
124 TALLOC_CTX *frame = talloc_stackframe();
126 if (!context || !context->internal->initialized) {
127 errno = EINVAL; /* Best I can think of ... */
128 TALLOC_FREE(frame);
129 return -1;
132 if (!fname) {
133 errno = EINVAL;
134 TALLOC_FREE(frame);
135 return -1;
138 DEBUG(4, ("smbc_stat(%s)\n", fname));
140 if (SMBC_parse_path(frame,
141 context,
142 fname,
143 &workgroup,
144 &server,
145 &share,
146 &path,
147 &user,
148 &password,
149 NULL)) {
150 errno = EINVAL;
151 TALLOC_FREE(frame);
152 return -1;
155 if (!user || user[0] == (char)0) {
156 user = talloc_strdup(frame, smbc_getUser(context));
157 if (!user) {
158 errno = ENOMEM;
159 TALLOC_FREE(frame);
160 return -1;
164 srv = SMBC_server(frame, context, True,
165 server, share, &workgroup, &user, &password);
166 if (!srv) {
167 TALLOC_FREE(frame);
168 return -1; /* errno set by SMBC_server */
171 if (!SMBC_getatr(context, srv, path, &mode, &size,
172 NULL,
173 &access_time_ts,
174 &write_time_ts,
175 &change_time_ts,
176 &ino)) {
177 errno = SMBC_errno(context, srv->cli);
178 TALLOC_FREE(frame);
179 return -1;
182 st->st_ino = ino;
184 setup_stat(context, st, fname, size, mode);
186 st->st_atime = convert_timespec_to_time_t(access_time_ts);
187 st->st_ctime = convert_timespec_to_time_t(change_time_ts);
188 st->st_mtime = convert_timespec_to_time_t(write_time_ts);
189 st->st_dev = srv->dev;
191 TALLOC_FREE(frame);
192 return 0;
196 * Routine to stat a file given an fd
200 SMBC_fstat_ctx(SMBCCTX *context,
201 SMBCFILE *file,
202 struct stat *st)
204 struct timespec change_time_ts;
205 struct timespec access_time_ts;
206 struct timespec write_time_ts;
207 off_t size;
208 uint16 mode;
209 char *server = NULL;
210 char *share = NULL;
211 char *user = NULL;
212 char *password = NULL;
213 char *path = NULL;
214 char *targetpath = NULL;
215 struct cli_state *targetcli = NULL;
216 SMB_INO_T ino = 0;
217 TALLOC_CTX *frame = talloc_stackframe();
218 NTSTATUS status;
220 if (!context || !context->internal->initialized) {
221 errno = EINVAL;
222 TALLOC_FREE(frame);
223 return -1;
226 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
227 errno = EBADF;
228 TALLOC_FREE(frame);
229 return -1;
232 if (!file->file) {
233 TALLOC_FREE(frame);
234 return smbc_getFunctionFstatdir(context)(context, file, st);
237 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
238 if (SMBC_parse_path(frame,
239 context,
240 file->fname,
241 NULL,
242 &server,
243 &share,
244 &path,
245 &user,
246 &password,
247 NULL)) {
248 errno = EINVAL;
249 TALLOC_FREE(frame);
250 return -1;
253 /*d_printf(">>>fstat: resolving %s\n", path);*/
254 status = cli_resolve_path(frame, "", context->internal->auth_info,
255 file->srv->cli, path,
256 &targetcli, &targetpath);
257 if (!NT_STATUS_IS_OK(status)) {
258 d_printf("Could not resolve %s\n", path);
259 errno = ENOENT;
260 TALLOC_FREE(frame);
261 return -1;
263 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
265 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
266 targetcli, file->cli_fd, &mode, &size,
267 NULL,
268 &access_time_ts,
269 &write_time_ts,
270 &change_time_ts,
271 &ino))) {
272 time_t change_time, access_time, write_time;
274 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd, &mode, &size,
275 &change_time, &access_time, &write_time))) {
276 errno = EINVAL;
277 TALLOC_FREE(frame);
278 return -1;
280 change_time_ts = convert_time_t_to_timespec(change_time);
281 access_time_ts = convert_time_t_to_timespec(access_time);
282 write_time_ts = convert_time_t_to_timespec(write_time);
285 st->st_ino = ino;
287 setup_stat(context, st, file->fname, size, mode);
289 st->st_atime = convert_timespec_to_time_t(access_time_ts);
290 st->st_ctime = convert_timespec_to_time_t(change_time_ts);
291 st->st_mtime = convert_timespec_to_time_t(write_time_ts);
292 st->st_dev = file->srv->dev;
294 TALLOC_FREE(frame);
295 return 0;
300 * Routine to obtain file system information given a path
303 SMBC_statvfs_ctx(SMBCCTX *context,
304 char *path,
305 struct statvfs *st)
307 int ret;
308 bool bIsDir;
309 struct stat statbuf;
310 SMBCFILE * pFile;
312 /* Determine if the provided path is a file or a folder */
313 if (SMBC_stat_ctx(context, path, &statbuf) < 0) {
314 return -1;
317 /* Is it a file or a directory? */
318 if (S_ISDIR(statbuf.st_mode)) {
319 /* It's a directory. */
320 if ((pFile = SMBC_opendir_ctx(context, path)) == NULL) {
321 return -1;
323 bIsDir = true;
324 } else if (S_ISREG(statbuf.st_mode)) {
325 /* It's a file. */
326 if ((pFile = SMBC_open_ctx(context, path,
327 O_RDONLY, 0)) == NULL) {
328 return -1;
330 bIsDir = false;
331 } else {
332 /* It's neither a file nor a directory. Not supported. */
333 errno = ENOSYS;
334 return -1;
337 /* Now we have an open file handle, so just use SMBC_fstatvfs */
338 ret = SMBC_fstatvfs_ctx(context, pFile, st);
340 /* Close the file or directory */
341 if (bIsDir) {
342 SMBC_closedir_ctx(context, pFile);
343 } else {
344 SMBC_close_ctx(context, pFile);
347 return ret;
352 * Routine to obtain file system information given an fd
356 SMBC_fstatvfs_ctx(SMBCCTX *context,
357 SMBCFILE *file,
358 struct statvfs *st)
360 unsigned long flags = 0;
361 uint32 fs_attrs = 0;
362 struct cli_state *cli = file->srv->cli;
364 /* Initialize all fields (at least until we actually use them) */
365 memset(st, 0, sizeof(*st));
368 * The state of each flag is such that the same bits are unset as
369 * would typically be unset on a local file system on a POSIX OS. Thus
370 * the bit is on, for example, only for case-insensitive file systems
371 * since most POSIX file systems are case sensitive and fstatvfs()
372 * would typically return zero in these bits on such a local file
373 * system.
376 /* See if the server has UNIX CIFS support */
377 if (! SERVER_HAS_UNIX_CIFS(cli)) {
378 uint64_t total_allocation_units;
379 uint64_t caller_allocation_units;
380 uint64_t actual_allocation_units;
381 uint64_t sectors_per_allocation_unit;
382 uint64_t bytes_per_sector;
383 NTSTATUS status;
385 /* Nope. If size data is available... */
386 status = cli_get_fs_full_size_info(cli,
387 &total_allocation_units,
388 &caller_allocation_units,
389 &actual_allocation_units,
390 &sectors_per_allocation_unit,
391 &bytes_per_sector);
392 if (NT_STATUS_IS_OK(status)) {
394 /* ... then provide it */
395 st->f_bsize =
396 (unsigned long) bytes_per_sector;
397 #if HAVE_FRSIZE
398 st->f_frsize =
399 (unsigned long) sectors_per_allocation_unit;
400 #endif
401 st->f_blocks =
402 (fsblkcnt_t) total_allocation_units;
403 st->f_bfree =
404 (fsblkcnt_t) actual_allocation_units;
407 flags |= SMBC_VFS_FEATURE_NO_UNIXCIFS;
408 } else {
409 uint32 optimal_transfer_size;
410 uint32 block_size;
411 uint64_t total_blocks;
412 uint64_t blocks_available;
413 uint64_t user_blocks_available;
414 uint64_t total_file_nodes;
415 uint64_t free_file_nodes;
416 uint64_t fs_identifier;
417 NTSTATUS status;
419 /* Has UNIXCIFS. If POSIX filesystem info is available... */
420 status = cli_get_posix_fs_info(cli,
421 &optimal_transfer_size,
422 &block_size,
423 &total_blocks,
424 &blocks_available,
425 &user_blocks_available,
426 &total_file_nodes,
427 &free_file_nodes,
428 &fs_identifier);
429 if (NT_STATUS_IS_OK(status)) {
431 /* ... then what's provided here takes precedence. */
432 st->f_bsize =
433 (unsigned long) block_size;
434 st->f_blocks =
435 (fsblkcnt_t) total_blocks;
436 st->f_bfree =
437 (fsblkcnt_t) blocks_available;
438 st->f_bavail =
439 (fsblkcnt_t) user_blocks_available;
440 st->f_files =
441 (fsfilcnt_t) total_file_nodes;
442 st->f_ffree =
443 (fsfilcnt_t) free_file_nodes;
444 #if HAVE_FSID_INT
445 st->f_fsid =
446 (unsigned long) fs_identifier;
447 #endif
451 /* See if the share is case sensitive */
452 if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(cli, &fs_attrs))) {
454 * We can't determine the case sensitivity of
455 * the share. We have no choice but to use the
456 * user-specified case sensitivity setting.
458 if (! smbc_getOptionCaseSensitive(context)) {
459 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
461 } else {
462 if (! (fs_attrs & FILE_CASE_SENSITIVE_SEARCH)) {
463 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
467 /* See if DFS is supported */
468 if ((smb1cli_conn_capabilities(cli->conn) & CAP_DFS) && cli->dfsroot) {
469 flags |= SMBC_VFS_FEATURE_DFS;
472 #if HAVE_STATVFS_F_FLAG
473 st->f_flag = flags;
474 #elif HAVE_STATVFS_F_FLAGS
475 st->f_flags = flags;
476 #endif
478 return 0;