lib/util Move more network utility functions from source3 into lib/util
[Samba.git] / source3 / libsmb / libsmb_stat.c
blob9c613508eb4fc41c0e0bc419b030c5ec74a177c3
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 "libsmbclient.h"
27 #include "libsmb_internal.h"
30 /*
31 * Generate an inode number from file name for those things that need it
34 static ino_t
35 generate_inode(SMBCCTX *context,
36 const char *name)
38 if (!context || !context->internal->initialized) {
39 errno = EINVAL;
40 return -1;
43 if (!*name) return 2; /* FIXME, why 2 ??? */
44 return (ino_t)str_checksum(name);
48 * Routine to put basic stat info into a stat structure ... Used by stat and
49 * fstat below.
52 static int
53 setup_stat(SMBCCTX *context,
54 struct stat *st,
55 char *fname,
56 SMB_OFF_T size,
57 int mode)
59 TALLOC_CTX *frame = talloc_stackframe();
61 st->st_mode = 0;
63 if (IS_DOS_DIR(mode)) {
64 st->st_mode = SMBC_DIR_MODE;
65 } else {
66 st->st_mode = SMBC_FILE_MODE;
69 if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
70 if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
71 if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
72 if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
74 st->st_size = size;
75 #ifdef HAVE_STAT_ST_BLKSIZE
76 st->st_blksize = 512;
77 #endif
78 #ifdef HAVE_STAT_ST_BLOCKS
79 st->st_blocks = (size+511)/512;
80 #endif
81 #ifdef HAVE_STRUCT_STAT_ST_RDEV
82 st->st_rdev = 0;
83 #endif
84 st->st_uid = getuid();
85 st->st_gid = getgid();
87 if (IS_DOS_DIR(mode)) {
88 st->st_nlink = 2;
89 } else {
90 st->st_nlink = 1;
93 if (st->st_ino == 0) {
94 st->st_ino = generate_inode(context, fname);
97 TALLOC_FREE(frame);
98 return True; /* FIXME: Is this needed ? */
102 * Routine to stat a file given a name
106 SMBC_stat_ctx(SMBCCTX *context,
107 const char *fname,
108 struct stat *st)
110 SMBCSRV *srv = NULL;
111 char *server = NULL;
112 char *share = NULL;
113 char *user = NULL;
114 char *password = NULL;
115 char *workgroup = NULL;
116 char *path = NULL;
117 struct timespec write_time_ts;
118 struct timespec access_time_ts;
119 struct timespec change_time_ts;
120 SMB_OFF_T size = 0;
121 uint16 mode = 0;
122 SMB_INO_T ino = 0;
123 TALLOC_CTX *frame = talloc_stackframe();
125 if (!context || !context->internal->initialized) {
126 errno = EINVAL; /* Best I can think of ... */
127 TALLOC_FREE(frame);
128 return -1;
131 if (!fname) {
132 errno = EINVAL;
133 TALLOC_FREE(frame);
134 return -1;
137 DEBUG(4, ("smbc_stat(%s)\n", fname));
139 if (SMBC_parse_path(frame,
140 context,
141 fname,
142 &workgroup,
143 &server,
144 &share,
145 &path,
146 &user,
147 &password,
148 NULL)) {
149 errno = EINVAL;
150 TALLOC_FREE(frame);
151 return -1;
154 if (!user || user[0] == (char)0) {
155 user = talloc_strdup(frame, smbc_getUser(context));
156 if (!user) {
157 errno = ENOMEM;
158 TALLOC_FREE(frame);
159 return -1;
163 srv = SMBC_server(frame, context, True,
164 server, share, &workgroup, &user, &password);
165 if (!srv) {
166 TALLOC_FREE(frame);
167 return -1; /* errno set by SMBC_server */
170 if (!SMBC_getatr(context, srv, path, &mode, &size,
171 NULL,
172 &access_time_ts,
173 &write_time_ts,
174 &change_time_ts,
175 &ino)) {
176 errno = SMBC_errno(context, srv->cli);
177 TALLOC_FREE(frame);
178 return -1;
181 st->st_ino = ino;
183 setup_stat(context, st, (char *) fname, size, mode);
185 st->st_atime = convert_timespec_to_time_t(access_time_ts);
186 st->st_ctime = convert_timespec_to_time_t(change_time_ts);
187 st->st_mtime = convert_timespec_to_time_t(write_time_ts);
188 st->st_dev = srv->dev;
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 SMB_OFF_T size;
207 uint16 mode;
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 TALLOC_CTX *frame = talloc_stackframe();
218 if (!context || !context->internal->initialized) {
219 errno = EINVAL;
220 TALLOC_FREE(frame);
221 return -1;
224 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
225 errno = EBADF;
226 TALLOC_FREE(frame);
227 return -1;
230 if (!file->file) {
231 TALLOC_FREE(frame);
232 return smbc_getFunctionFstatdir(context)(context, file, st);
235 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
236 if (SMBC_parse_path(frame,
237 context,
238 file->fname,
239 NULL,
240 &server,
241 &share,
242 &path,
243 &user,
244 &password,
245 NULL)) {
246 errno = EINVAL;
247 TALLOC_FREE(frame);
248 return -1;
251 /*d_printf(">>>fstat: resolving %s\n", path);*/
252 if (!cli_resolve_path(frame, "", context->internal->auth_info,
253 file->srv->cli, path,
254 &targetcli, &targetpath)) {
255 d_printf("Could not resolve %s\n", path);
256 errno = ENOENT;
257 TALLOC_FREE(frame);
258 return -1;
260 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
262 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
263 targetcli, file->cli_fd, &mode, &size,
264 NULL,
265 &access_time_ts,
266 &write_time_ts,
267 &change_time_ts,
268 &ino))) {
269 time_t change_time, access_time, write_time;
271 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd, &mode, &size,
272 &change_time, &access_time, &write_time))) {
273 errno = EINVAL;
274 TALLOC_FREE(frame);
275 return -1;
277 change_time_ts = convert_time_t_to_timespec(change_time);
278 access_time_ts = convert_time_t_to_timespec(access_time);
279 write_time_ts = convert_time_t_to_timespec(write_time);
282 st->st_ino = ino;
284 setup_stat(context, st, file->fname, size, mode);
286 st->st_atime = convert_timespec_to_time_t(access_time_ts);
287 st->st_ctime = convert_timespec_to_time_t(change_time_ts);
288 st->st_mtime = convert_timespec_to_time_t(write_time_ts);
289 st->st_dev = file->srv->dev;
291 TALLOC_FREE(frame);
292 return 0;
297 * Routine to obtain file system information given a path
300 SMBC_statvfs_ctx(SMBCCTX *context,
301 char *path,
302 struct statvfs *st)
304 int ret;
305 bool bIsDir;
306 struct stat statbuf;
307 SMBCFILE * pFile;
309 /* Determine if the provided path is a file or a folder */
310 if (SMBC_stat_ctx(context, path, &statbuf) < 0) {
311 return -1;
314 /* Is it a file or a directory? */
315 if (S_ISDIR(statbuf.st_mode)) {
316 /* It's a directory. */
317 if ((pFile = SMBC_opendir_ctx(context, path)) == NULL) {
318 return -1;
320 bIsDir = true;
321 } else if (S_ISREG(statbuf.st_mode)) {
322 /* It's a file. */
323 if ((pFile = SMBC_open_ctx(context, path,
324 O_RDONLY, 0)) == NULL) {
325 return -1;
327 bIsDir = false;
328 } else {
329 /* It's neither a file nor a directory. Not supported. */
330 errno = ENOSYS;
331 return -1;
334 /* Now we have an open file handle, so just use SMBC_fstatvfs */
335 ret = SMBC_fstatvfs_ctx(context, pFile, st);
337 /* Close the file or directory */
338 if (bIsDir) {
339 SMBC_closedir_ctx(context, pFile);
340 } else {
341 SMBC_close_ctx(context, pFile);
344 return ret;
349 * Routine to obtain file system information given an fd
353 SMBC_fstatvfs_ctx(SMBCCTX *context,
354 SMBCFILE *file,
355 struct statvfs *st)
357 unsigned long flags = 0;
358 uint32 fs_attrs = 0;
359 struct cli_state *cli = file->srv->cli;
361 /* Initialize all fields (at least until we actually use them) */
362 memset(st, 0, sizeof(*st));
365 * The state of each flag is such that the same bits are unset as
366 * would typically be unset on a local file system on a POSIX OS. Thus
367 * the bit is on, for example, only for case-insensitive file systems
368 * since most POSIX file systems are case sensitive and fstatvfs()
369 * would typically return zero in these bits on such a local file
370 * system.
373 /* See if the server has UNIX CIFS support */
374 if (! SERVER_HAS_UNIX_CIFS(cli)) {
375 uint64_t total_allocation_units;
376 uint64_t caller_allocation_units;
377 uint64_t actual_allocation_units;
378 uint64_t sectors_per_allocation_unit;
379 uint64_t bytes_per_sector;
380 NTSTATUS status;
382 /* Nope. If size data is available... */
383 status = cli_get_fs_full_size_info(cli,
384 &total_allocation_units,
385 &caller_allocation_units,
386 &actual_allocation_units,
387 &sectors_per_allocation_unit,
388 &bytes_per_sector);
389 if (NT_STATUS_IS_OK(status)) {
391 /* ... then provide it */
392 st->f_bsize =
393 (unsigned long) bytes_per_sector;
394 #if HAVE_FRSIZE
395 st->f_frsize =
396 (unsigned long) sectors_per_allocation_unit;
397 #endif
398 st->f_blocks =
399 (fsblkcnt_t) total_allocation_units;
400 st->f_bfree =
401 (fsblkcnt_t) actual_allocation_units;
404 flags |= SMBC_VFS_FEATURE_NO_UNIXCIFS;
405 } else {
406 uint32 optimal_transfer_size;
407 uint32 block_size;
408 uint64_t total_blocks;
409 uint64_t blocks_available;
410 uint64_t user_blocks_available;
411 uint64_t total_file_nodes;
412 uint64_t free_file_nodes;
413 uint64_t fs_identifier;
414 NTSTATUS status;
416 /* Has UNIXCIFS. If POSIX filesystem info is available... */
417 status = cli_get_posix_fs_info(cli,
418 &optimal_transfer_size,
419 &block_size,
420 &total_blocks,
421 &blocks_available,
422 &user_blocks_available,
423 &total_file_nodes,
424 &free_file_nodes,
425 &fs_identifier);
426 if (NT_STATUS_IS_OK(status)) {
428 /* ... then what's provided here takes precedence. */
429 st->f_bsize =
430 (unsigned long) block_size;
431 st->f_blocks =
432 (fsblkcnt_t) total_blocks;
433 st->f_bfree =
434 (fsblkcnt_t) blocks_available;
435 st->f_bavail =
436 (fsblkcnt_t) user_blocks_available;
437 st->f_files =
438 (fsfilcnt_t) total_file_nodes;
439 st->f_ffree =
440 (fsfilcnt_t) free_file_nodes;
441 #if HAVE_FSID_INT
442 st->f_fsid =
443 (unsigned long) fs_identifier;
444 #endif
448 /* See if the share is case sensitive */
449 if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(cli, &fs_attrs))) {
451 * We can't determine the case sensitivity of
452 * the share. We have no choice but to use the
453 * user-specified case sensitivity setting.
455 if (! smbc_getOptionCaseSensitive(context)) {
456 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
458 } else {
459 if (! (fs_attrs & FILE_CASE_SENSITIVE_SEARCH)) {
460 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
464 /* See if DFS is supported */
465 if ((cli->capabilities & CAP_DFS) && cli->dfsroot) {
466 flags |= SMBC_VFS_FEATURE_DFS;
469 #if HAVE_STATVFS_F_FLAG
470 st->f_flag = flags;
471 #elif HAVE_STATVFS_F_FLAGS
472 st->f_flags = flags;
473 #endif
475 return 0;