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 "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
)
41 return (ino_t
)str_checksum(name
);
45 * Routine to put basic stat info into a stat structure ... Used by stat and
49 void setup_stat(struct stat
*st
,
55 struct timespec access_time_ts
,
56 struct timespec change_time_ts
,
57 struct timespec write_time_ts
)
61 if (attr
& FILE_ATTRIBUTE_DIRECTORY
) {
62 st
->st_mode
= (S_IFDIR
| 0555);
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
;
81 #ifdef HAVE_STAT_ST_BLKSIZE
84 #ifdef HAVE_STAT_ST_BLOCKS
85 st
->st_blocks
= (size
+511)/512;
87 #ifdef HAVE_STRUCT_STAT_ST_RDEV
90 st
->st_uid
= getuid();
91 st
->st_gid
= getgid();
93 if (attr
& FILE_ATTRIBUTE_DIRECTORY
) {
102 st
->st_ino
= generate_inode(fname
);
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
,
130 char *password
= NULL
;
131 char *workgroup
= NULL
;
135 TALLOC_CTX
*frame
= talloc_stackframe();
137 if (!context
|| !context
->internal
->initialized
) {
138 errno
= EINVAL
; /* Best I can think of ... */
149 DEBUG(4, ("smbc_stat(%s)\n", fname
));
151 if (SMBC_parse_path(frame
,
167 if (!user
|| user
[0] == (char)0) {
168 user
= talloc_strdup(frame
, smbc_getUser(context
));
176 srv
= SMBC_server(frame
, context
, True
,
177 server
, port
, share
, &workgroup
, &user
, &password
);
180 return -1; /* errno set by SMBC_server */
183 status
= SMBC_getatr(context
, srv
, path
, st
);
184 if (!NT_STATUS_IS_OK(status
)) {
186 errno
= cli_status_to_errno(status
);
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
;
217 struct cli_credentials
*creds
= NULL
;
218 TALLOC_CTX
*frame
= talloc_stackframe();
221 if (!context
|| !context
->internal
->initialized
) {
227 if (!SMBC_dlist_contains(context
->internal
->files
, file
)) {
235 return smbc_getFunctionFstatdir(context
)(context
, file
, st
);
238 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
239 if (SMBC_parse_path(frame
,
255 creds
= context
->internal
->creds
;
257 /*d_printf(">>>fstat: resolving %s\n", path);*/
258 status
= cli_resolve_path(frame
, "",
260 file
->srv
->cli
, path
,
261 &targetcli
, &targetpath
);
262 if (!NT_STATUS_IS_OK(status
)) {
263 d_printf("Could not resolve %s\n", path
);
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
,
298 * Routine to obtain file system information given a path
301 SMBC_statvfs_ctx(SMBCCTX
*context
,
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) {
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
) {
325 } else if (S_ISREG(statbuf
.st_mode
)) {
327 if ((pFile
= SMBC_open_ctx(context
, path
,
328 O_RDONLY
, 0)) == NULL
) {
334 /* It's neither a file nor a directory. Not supported. */
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 */
345 SMBC_closedir_ctx(context
, pFile
);
347 SMBC_close_ctx(context
, pFile
);
356 * Routine to obtain file system information given an fd
360 SMBC_fstatvfs_ctx(SMBCCTX
*context
,
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
;
373 tcon
= cli
->smb1
.tcon
;
376 /* Initialize all fields (at least until we actually use them) */
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
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
;
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 §ors_per_allocation_unit
,
404 if (NT_STATUS_IS_OK(status
)) {
406 /* ... then provide it */
408 (unsigned long) bytes_per_sector
;
411 (unsigned long) sectors_per_allocation_unit
;
414 (fsblkcnt_t
) total_allocation_units
;
416 (fsblkcnt_t
) actual_allocation_units
;
418 (fsblkcnt_t
) caller_allocation_units
;
421 flags
|= SMBC_VFS_FEATURE_NO_UNIXCIFS
;
423 uint32_t optimal_transfer_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
;
433 /* Has UNIXCIFS. If POSIX filesystem info is available... */
434 status
= cli_get_posix_fs_info(cli
,
435 &optimal_transfer_size
,
439 &user_blocks_available
,
443 if (NT_STATUS_IS_OK(status
)) {
445 /* ... then what's provided here takes precedence. */
447 (unsigned long) block_size
;
449 (fsblkcnt_t
) total_blocks
;
451 (fsblkcnt_t
) blocks_available
;
453 (fsblkcnt_t
) user_blocks_available
;
455 (fsfilcnt_t
) total_file_nodes
;
457 (fsfilcnt_t
) free_file_nodes
;
460 (unsigned long) fs_identifier
;
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
;
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)
490 #elif defined(HAVE_STATVFS_F_FLAGS)