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/>.
26 #include "libsmbclient.h"
27 #include "libsmb_internal.h"
31 * Generate an inode number from file name for those things that need it
35 generate_inode(SMBCCTX *context,
38 if (!context || !context->internal->initialized) {
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
53 setup_stat(SMBCCTX *context,
59 TALLOC_CTX *frame = talloc_stackframe();
63 if (IS_DOS_DIR(mode)) {
64 st->st_mode = SMBC_DIR_MODE;
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;
75 #ifdef HAVE_STAT_ST_BLKSIZE
78 #ifdef HAVE_STAT_ST_BLOCKS
79 st->st_blocks = (size+511)/512;
81 #ifdef HAVE_STRUCT_STAT_ST_RDEV
84 st->st_uid = getuid();
85 st->st_gid = getgid();
87 if (IS_DOS_DIR(mode)) {
93 if (st->st_ino == 0) {
94 st->st_ino = generate_inode(context, fname);
98 return True; /* FIXME: Is this needed ? */
102 * Routine to stat a file given a name
106 SMBC_stat_ctx(SMBCCTX *context,
114 char *password = NULL;
115 char *workgroup = NULL;
117 struct timespec write_time_ts;
118 struct timespec access_time_ts;
119 struct timespec change_time_ts;
123 TALLOC_CTX *frame = talloc_stackframe();
125 if (!context || !context->internal->initialized) {
126 errno = EINVAL; /* Best I can think of ... */
137 DEBUG(4, ("smbc_stat(%s)\n", fname));
139 if (SMBC_parse_path(frame,
154 if (!user || user[0] == (char)0) {
155 user = talloc_strdup(frame, smbc_getUser(context));
163 srv = SMBC_server(frame, context, True,
164 server, share, &workgroup, &user, &password);
167 return -1; /* errno set by SMBC_server */
170 if (!SMBC_getatr(context, srv, path, &mode, &size,
176 errno = SMBC_errno(context, srv->cli);
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;
195 * Routine to stat a file given an fd
199 SMBC_fstat_ctx(SMBCCTX *context,
203 struct timespec change_time_ts;
204 struct timespec access_time_ts;
205 struct timespec write_time_ts;
211 char *password = NULL;
213 char *targetpath = NULL;
214 struct cli_state *targetcli = NULL;
216 TALLOC_CTX *frame = talloc_stackframe();
218 if (!context || !context->internal->initialized) {
224 if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
232 return smbc_getFunctionFstatdir(context)(context, file, st);
235 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
236 if (SMBC_parse_path(frame,
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);
260 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
262 if (!cli_qfileinfo_basic(targetcli, file->cli_fd, &mode, &size,
268 time_t change_time, access_time, write_time;
270 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd, &mode, &size,
271 &change_time, &access_time, &write_time))) {
276 change_time_ts = convert_time_t_to_timespec(change_time);
277 access_time_ts = convert_time_t_to_timespec(access_time);
278 write_time_ts = convert_time_t_to_timespec(write_time);
283 setup_stat(context, st, file->fname, size, mode);
285 st->st_atime = convert_timespec_to_time_t(access_time_ts);
286 st->st_ctime = convert_timespec_to_time_t(change_time_ts);
287 st->st_mtime = convert_timespec_to_time_t(write_time_ts);
288 st->st_dev = file->srv->dev;
296 * Routine to obtain file system information given a path
299 SMBC_statvfs_ctx(SMBCCTX *context,
308 /* Determine if the provided path is a file or a folder */
309 if (SMBC_stat_ctx(context, path, &statbuf) < 0) {
313 /* Is it a file or a directory? */
314 if (S_ISDIR(statbuf.st_mode)) {
315 /* It's a directory. */
316 if ((pFile = SMBC_opendir_ctx(context, path)) == NULL) {
320 } else if (S_ISREG(statbuf.st_mode)) {
322 if ((pFile = SMBC_open_ctx(context, path,
323 O_RDONLY, 0)) == NULL) {
328 /* It's neither a file nor a directory. Not supported. */
333 /* Now we have an open file handle, so just use SMBC_fstatvfs */
334 ret = SMBC_fstatvfs_ctx(context, pFile, st);
336 /* Close the file or directory */
338 SMBC_closedir_ctx(context, pFile);
340 SMBC_close_ctx(context, pFile);
348 * Routine to obtain file system information given an fd
352 SMBC_fstatvfs_ctx(SMBCCTX *context,
356 unsigned long flags = 0;
358 struct cli_state *cli = file->srv->cli;
360 /* Initialize all fields (at least until we actually use them) */
361 memset(st, 0, sizeof(*st));
364 * The state of each flag is such that the same bits are unset as
365 * would typically be unset on a local file system on a POSIX OS. Thus
366 * the bit is on, for example, only for case-insensitive file systems
367 * since most POSIX file systems are case sensitive and fstatvfs()
368 * would typically return zero in these bits on such a local file
372 /* See if the server has UNIX CIFS support */
373 if (! SERVER_HAS_UNIX_CIFS(cli)) {
374 uint64_t total_allocation_units;
375 uint64_t caller_allocation_units;
376 uint64_t actual_allocation_units;
377 uint64_t sectors_per_allocation_unit;
378 uint64_t bytes_per_sector;
380 /* Nope. If size data is available... */
381 if (cli_get_fs_full_size_info(cli,
382 &total_allocation_units,
383 &caller_allocation_units,
384 &actual_allocation_units,
385 §ors_per_allocation_unit,
386 &bytes_per_sector)) {
388 /* ... then provide it */
390 (unsigned long) bytes_per_sector;
393 (unsigned long) sectors_per_allocation_unit;
396 (fsblkcnt_t) total_allocation_units;
398 (fsblkcnt_t) actual_allocation_units;
401 flags |= SMBC_VFS_FEATURE_NO_UNIXCIFS;
403 uint32 optimal_transfer_size;
405 uint64_t total_blocks;
406 uint64_t blocks_available;
407 uint64_t user_blocks_available;
408 uint64_t total_file_nodes;
409 uint64_t free_file_nodes;
410 uint64_t fs_identifier;
412 /* Has UNIXCIFS. If POSIX filesystem info is available... */
413 if (cli_get_posix_fs_info(cli,
414 &optimal_transfer_size,
418 &user_blocks_available,
423 /* ... then what's provided here takes precedence. */
425 (unsigned long) block_size;
427 (fsblkcnt_t) total_blocks;
429 (fsblkcnt_t) blocks_available;
431 (fsblkcnt_t) user_blocks_available;
433 (fsfilcnt_t) total_file_nodes;
435 (fsfilcnt_t) free_file_nodes;
438 (unsigned long) fs_identifier;
443 /* See if the share is case sensitive */
444 if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(cli, &fs_attrs))) {
446 * We can't determine the case sensitivity of
447 * the share. We have no choice but to use the
448 * user-specified case sensitivity setting.
450 if (! smbc_getOptionCaseSensitive(context)) {
451 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
454 if (! (fs_attrs & FILE_CASE_SENSITIVE_SEARCH)) {
455 flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
459 /* See if DFS is supported */
460 if ((cli->capabilities & CAP_DFS) && cli->dfsroot) {
461 flags |= SMBC_VFS_FEATURE_DFS;
464 #if HAVE_STATVFS_F_FLAG
466 #elif HAVE_STATVFS_F_FLAGS