s3:winbindd: call process_set_title() for locator child
[Samba.git] / source3 / libsmb / libsmb_stat.c
blob2c5eddbffdee1748a778c9cd511efe06d04ac017
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"
30 #include "lib/util/time.h"
33 * Generate an inode number from file name for those things that need it
36 static ino_t generate_inode(const char *name)
38 if (name == NULL) {
39 return (ino_t)-1;
41 return (ino_t)str_checksum(name);
45 * Routine to put basic stat info into a stat structure ... Used by stat and
46 * fstat below.
49 void setup_stat(struct stat *st,
50 const char *fname,
51 off_t size,
52 int attr,
53 ino_t ino,
54 dev_t dev,
55 struct timespec access_time_ts,
56 struct timespec change_time_ts,
57 struct timespec write_time_ts)
59 st->st_mode = 0;
61 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
62 st->st_mode = (S_IFDIR | 0555);
63 } else {
64 st->st_mode = (S_IFREG | 0444);
67 if (attr & FILE_ATTRIBUTE_ARCHIVE) {
68 st->st_mode |= S_IXUSR;
70 if (attr & FILE_ATTRIBUTE_SYSTEM) {
71 st->st_mode |= S_IXGRP;
73 if (attr & FILE_ATTRIBUTE_HIDDEN) {
74 st->st_mode |= S_IXOTH;
76 if (!(attr & FILE_ATTRIBUTE_READONLY)) {
77 st->st_mode |= S_IWUSR;
80 st->st_size = size;
81 #ifdef HAVE_STAT_ST_BLKSIZE
82 st->st_blksize = 512;
83 #endif
84 #ifdef HAVE_STAT_ST_BLOCKS
85 st->st_blocks = (size+511)/512;
86 #endif
87 #ifdef HAVE_STRUCT_STAT_ST_RDEV
88 st->st_rdev = 0;
89 #endif
90 st->st_uid = getuid();
91 st->st_gid = getgid();
93 if (attr & FILE_ATTRIBUTE_DIRECTORY) {
94 st->st_nlink = 2;
95 } else {
96 st->st_nlink = 1;
99 if (ino != 0) {
100 st->st_ino = ino;
101 } else {
102 st->st_ino = generate_inode(fname);
105 st->st_dev = dev;
107 st->st_atime = access_time_ts.tv_sec;
108 set_atimensec(st, access_time_ts.tv_nsec);
110 st->st_ctime = change_time_ts.tv_sec;
111 set_ctimensec(st, change_time_ts.tv_nsec);
113 st->st_mtime = write_time_ts.tv_sec;
114 set_mtimensec(st, write_time_ts.tv_nsec);
118 * Routine to stat a file given a name
122 SMBC_stat_ctx(SMBCCTX *context,
123 const char *fname,
124 struct stat *st)
126 SMBCSRV *srv = NULL;
127 char *server = NULL;
128 char *share = NULL;
129 char *user = NULL;
130 char *password = NULL;
131 char *workgroup = NULL;
132 char *path = NULL;
133 uint16_t port = 0;
134 NTSTATUS status;
135 TALLOC_CTX *frame = talloc_stackframe();
137 if (!context || !context->internal->initialized) {
138 errno = EINVAL; /* Best I can think of ... */
139 TALLOC_FREE(frame);
140 return -1;
143 if (!fname) {
144 errno = EINVAL;
145 TALLOC_FREE(frame);
146 return -1;
149 DEBUG(4, ("smbc_stat(%s)\n", fname));
151 if (SMBC_parse_path(frame,
152 context,
153 fname,
154 &workgroup,
155 &server,
156 &port,
157 &share,
158 &path,
159 &user,
160 &password,
161 NULL)) {
162 errno = EINVAL;
163 TALLOC_FREE(frame);
164 return -1;
167 if (!user || user[0] == (char)0) {
168 user = talloc_strdup(frame, smbc_getUser(context));
169 if (!user) {
170 errno = ENOMEM;
171 TALLOC_FREE(frame);
172 return -1;
176 srv = SMBC_server(frame, context, True,
177 server, port, share, &workgroup, &user, &password);
178 if (!srv) {
179 TALLOC_FREE(frame);
180 return -1; /* errno set by SMBC_server */
183 status = SMBC_getatr(context, srv, path, st);
184 if (!NT_STATUS_IS_OK(status)) {
185 TALLOC_FREE(frame);
186 errno = cli_status_to_errno(status);
187 return -1;
190 TALLOC_FREE(frame);
191 return 0;
195 * Routine to stat a file given an fd
199 SMBC_fstat_ctx(SMBCCTX *context,
200 SMBCFILE *file,
201 struct stat *st)
203 struct timespec change_time_ts;
204 struct timespec access_time_ts;
205 struct timespec write_time_ts;
206 off_t size;
207 uint32_t attr;
208 char *server = NULL;
209 char *share = NULL;
210 char *user = NULL;
211 char *password = NULL;
212 char *path = NULL;
213 char *targetpath = NULL;
214 struct cli_state *targetcli = NULL;
215 SMB_INO_T ino = 0;
216 uint16_t port = 0;
217 struct cli_credentials *creds = NULL;
218 TALLOC_CTX *frame = talloc_stackframe();
219 NTSTATUS status;
221 if (!context || !context->internal->initialized) {
222 errno = EINVAL;
223 TALLOC_FREE(frame);
224 return -1;
227 if (!SMBC_dlist_contains(context->internal->files, file)) {
228 errno = EBADF;
229 TALLOC_FREE(frame);
230 return -1;
233 if (!file->file) {
234 TALLOC_FREE(frame);
235 return smbc_getFunctionFstatdir(context)(context, file, st);
238 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
239 if (SMBC_parse_path(frame,
240 context,
241 file->fname,
242 NULL,
243 &server,
244 &port,
245 &share,
246 &path,
247 &user,
248 &password,
249 NULL)) {
250 errno = EINVAL;
251 TALLOC_FREE(frame);
252 return -1;
255 creds = context->internal->creds;
257 /*d_printf(">>>fstat: resolving %s\n", path);*/
258 status = cli_resolve_path(frame, "",
259 creds,
260 file->srv->cli, path,
261 &targetcli, &targetpath);
262 if (!NT_STATUS_IS_OK(status)) {
263 d_printf("Could not resolve %s\n", path);
264 errno = ENOENT;
265 TALLOC_FREE(frame);
266 return -1;
268 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
270 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
271 targetcli, file->cli_fd, &attr, &size,
272 NULL,
273 &access_time_ts,
274 &write_time_ts,
275 &change_time_ts,
276 &ino))) {
277 errno = EINVAL;
278 TALLOC_FREE(frame);
279 return -1;
282 setup_stat(st,
283 path,
284 size,
285 attr,
286 ino,
287 file->srv->dev,
288 access_time_ts,
289 change_time_ts,
290 write_time_ts);
292 TALLOC_FREE(frame);
293 return 0;
298 * Routine to obtain file system information given a path
301 SMBC_statvfs_ctx(SMBCCTX *context,
302 char *path,
303 struct statvfs *st)
305 int ret;
306 bool bIsDir;
307 struct stat statbuf;
308 SMBCFILE * pFile;
309 TALLOC_CTX *frame = talloc_stackframe();
311 /* Determine if the provided path is a file or a folder */
312 if (SMBC_stat_ctx(context, path, &statbuf) < 0) {
313 TALLOC_FREE(frame);
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 TALLOC_FREE(frame);
322 return -1;
324 bIsDir = true;
325 } else if (S_ISREG(statbuf.st_mode)) {
326 /* It's a file. */
327 if ((pFile = SMBC_open_ctx(context, path,
328 O_RDONLY, 0)) == NULL) {
329 TALLOC_FREE(frame);
330 return -1;
332 bIsDir = false;
333 } else {
334 /* It's neither a file nor a directory. Not supported. */
335 TALLOC_FREE(frame);
336 errno = ENOSYS;
337 return -1;
340 /* Now we have an open file handle, so just use SMBC_fstatvfs */
341 ret = SMBC_fstatvfs_ctx(context, pFile, st);
343 /* Close the file or directory */
344 if (bIsDir) {
345 SMBC_closedir_ctx(context, pFile);
346 } else {
347 SMBC_close_ctx(context, pFile);
350 TALLOC_FREE(frame);
351 return ret;
356 * Routine to obtain file system information given an fd
360 SMBC_fstatvfs_ctx(SMBCCTX *context,
361 SMBCFILE *file,
362 struct statvfs *st)
364 unsigned long flags = 0;
365 uint32_t fs_attrs = 0;
366 struct cli_state *cli = file->srv->cli;
367 struct smbXcli_tcon *tcon;
368 TALLOC_CTX *frame = talloc_stackframe();
370 if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
371 tcon = cli->smb2.tcon;
372 } else {
373 tcon = cli->smb1.tcon;
376 /* Initialize all fields (at least until we actually use them) */
377 ZERO_STRUCTP(st);
380 * The state of each flag is such that the same bits are unset as
381 * would typically be unset on a local file system on a POSIX OS. Thus
382 * the bit is on, for example, only for case-insensitive file systems
383 * since most POSIX file systems are case sensitive and fstatvfs()
384 * would typically return zero in these bits on such a local file
385 * system.
388 /* See if the server has UNIX CIFS support */
389 if (! SERVER_HAS_UNIX_CIFS(cli)) {
390 uint64_t total_allocation_units;
391 uint64_t caller_allocation_units;
392 uint64_t actual_allocation_units;
393 uint64_t sectors_per_allocation_unit;
394 uint64_t bytes_per_sector;
395 NTSTATUS status;
397 /* Nope. If size data is available... */
398 status = cli_get_fs_full_size_info(cli,
399 &total_allocation_units,
400 &caller_allocation_units,
401 &actual_allocation_units,
402 &sectors_per_allocation_unit,
403 &bytes_per_sector);
404 if (NT_STATUS_IS_OK(status)) {
406 /* ... then provide it */
407 st->f_bsize =
408 (unsigned long) bytes_per_sector;
409 #ifdef HAVE_FRSIZE
410 st->f_frsize =
411 (unsigned long) sectors_per_allocation_unit;
412 #endif
413 st->f_blocks =
414 (fsblkcnt_t) total_allocation_units;
415 st->f_bfree =
416 (fsblkcnt_t) actual_allocation_units;
417 st->f_bavail =
418 (fsblkcnt_t) caller_allocation_units;
421 flags |= SMBC_VFS_FEATURE_NO_UNIXCIFS;
422 } else {
423 uint32_t optimal_transfer_size;
424 uint32_t block_size;
425 uint64_t total_blocks;
426 uint64_t blocks_available;
427 uint64_t user_blocks_available;
428 uint64_t total_file_nodes;
429 uint64_t free_file_nodes;
430 uint64_t fs_identifier;
431 NTSTATUS status;
433 /* Has UNIXCIFS. If POSIX filesystem info is available... */
434 status = cli_get_posix_fs_info(cli,
435 &optimal_transfer_size,
436 &block_size,
437 &total_blocks,
438 &blocks_available,
439 &user_blocks_available,
440 &total_file_nodes,
441 &free_file_nodes,
442 &fs_identifier);
443 if (NT_STATUS_IS_OK(status)) {
445 /* ... then what's provided here takes precedence. */
446 st->f_bsize =
447 (unsigned long) block_size;
448 st->f_blocks =
449 (fsblkcnt_t) total_blocks;
450 st->f_bfree =
451 (fsblkcnt_t) blocks_available;
452 st->f_bavail =
453 (fsblkcnt_t) user_blocks_available;
454 st->f_files =
455 (fsfilcnt_t) total_file_nodes;
456 st->f_ffree =
457 (fsfilcnt_t) free_file_nodes;
458 #ifdef HAVE_FSID_INT
459 st->f_fsid =
460 (unsigned long) fs_identifier;
461 #endif
465 /* See if the share is case sensitive */
466 if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(cli, &fs_attrs))) {
468 * We can't determine the case sensitivity of
469 * the share. We have no choice but to use the
470 * user-specified case sensitivity setting.
472 if (! smbc_getOptionCaseSensitive(context)) {
473 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
475 } else {
476 if (! (fs_attrs & FILE_CASE_SENSITIVE_SEARCH)) {
477 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
481 /* See if DFS is supported */
482 if (smbXcli_conn_dfs_supported(cli->conn) &&
483 smbXcli_tcon_is_dfs_share(tcon))
485 flags |= SMBC_VFS_FEATURE_DFS;
488 #if defined(HAVE_STATVFS_F_FLAG)
489 st->f_flag = flags;
490 #elif defined(HAVE_STATVFS_F_FLAGS)
491 st->f_flags = flags;
492 #endif
494 TALLOC_FREE(frame);
495 return 0;