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"
32 * Generate an inode number from file name for those things that need it
35 static ino_t
generate_inode(const char *name
)
40 return (ino_t
)str_checksum(name
);
44 * Routine to put basic stat info into a stat structure ... Used by stat and
48 void setup_stat(struct stat
*st
,
54 struct timespec access_time_ts
,
55 struct timespec change_time_ts
,
56 struct timespec write_time_ts
)
60 if (IS_DOS_DIR(mode
)) {
61 st
->st_mode
= SMBC_DIR_MODE
;
63 st
->st_mode
= SMBC_FILE_MODE
;
66 if (IS_DOS_ARCHIVE(mode
)) {
67 st
->st_mode
|= S_IXUSR
;
69 if (IS_DOS_SYSTEM(mode
)) {
70 st
->st_mode
|= S_IXGRP
;
72 if (IS_DOS_HIDDEN(mode
)) {
73 st
->st_mode
|= S_IXOTH
;
75 if (!IS_DOS_READONLY(mode
)) {
76 st
->st_mode
|= S_IWUSR
;
80 #ifdef HAVE_STAT_ST_BLKSIZE
83 #ifdef HAVE_STAT_ST_BLOCKS
84 st
->st_blocks
= (size
+511)/512;
86 #ifdef HAVE_STRUCT_STAT_ST_RDEV
89 st
->st_uid
= getuid();
90 st
->st_gid
= getgid();
92 if (IS_DOS_DIR(mode
)) {
101 st
->st_ino
= generate_inode(fname
);
105 st
->st_atime
= convert_timespec_to_time_t(access_time_ts
);
106 st
->st_ctime
= convert_timespec_to_time_t(change_time_ts
);
107 st
->st_mtime
= convert_timespec_to_time_t(write_time_ts
);
110 void setup_stat_from_stat_ex(const struct stat_ex
*stex
,
114 st
->st_atime
= convert_timespec_to_time_t(stex
->st_ex_atime
);
115 st
->st_ctime
= convert_timespec_to_time_t(stex
->st_ex_ctime
);
116 st
->st_mtime
= convert_timespec_to_time_t(stex
->st_ex_mtime
);
118 st
->st_mode
= stex
->st_ex_mode
;
119 st
->st_size
= stex
->st_ex_size
;
120 #ifdef HAVE_STAT_ST_BLKSIZE
121 st
->st_blksize
= 512;
123 #ifdef HAVE_STAT_ST_BLOCKS
124 st
->st_blocks
= (st
->st_size
+ 511) / 512;
126 #ifdef HAVE_STRUCT_STAT_ST_RDEV
129 st
->st_uid
= stex
->st_ex_uid
;
130 st
->st_gid
= stex
->st_ex_gid
;
132 st
->st_nlink
= stex
->st_ex_nlink
;
134 if (stex
->st_ex_ino
== 0) {
137 st
->st_ino
= generate_inode(fname
);
140 st
->st_ino
= stex
->st_ex_ino
;
143 st
->st_dev
= stex
->st_ex_dev
;
148 * Routine to stat a file given a name
152 SMBC_stat_ctx(SMBCCTX
*context
,
160 char *password
= NULL
;
161 char *workgroup
= NULL
;
164 TALLOC_CTX
*frame
= talloc_stackframe();
166 if (!context
|| !context
->internal
->initialized
) {
167 errno
= EINVAL
; /* Best I can think of ... */
178 DEBUG(4, ("smbc_stat(%s)\n", fname
));
180 if (SMBC_parse_path(frame
,
196 if (!user
|| user
[0] == (char)0) {
197 user
= talloc_strdup(frame
, smbc_getUser(context
));
205 srv
= SMBC_server(frame
, context
, True
,
206 server
, port
, share
, &workgroup
, &user
, &password
);
209 return -1; /* errno set by SMBC_server */
212 if (!SMBC_getatr(context
, srv
, path
, st
)) {
213 errno
= SMBC_errno(context
, srv
->cli
);
223 * Routine to stat a file given an fd
227 SMBC_fstat_ctx(SMBCCTX
*context
,
231 struct timespec change_time_ts
;
232 struct timespec access_time_ts
;
233 struct timespec write_time_ts
;
239 char *password
= NULL
;
241 char *targetpath
= NULL
;
242 struct cli_state
*targetcli
= NULL
;
245 TALLOC_CTX
*frame
= talloc_stackframe();
248 if (!context
|| !context
->internal
->initialized
) {
254 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
262 return smbc_getFunctionFstatdir(context
)(context
, file
, st
);
265 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
266 if (SMBC_parse_path(frame
,
282 /*d_printf(">>>fstat: resolving %s\n", path);*/
283 status
= cli_resolve_path(frame
, "", context
->internal
->auth_info
,
284 file
->srv
->cli
, path
,
285 &targetcli
, &targetpath
);
286 if (!NT_STATUS_IS_OK(status
)) {
287 d_printf("Could not resolve %s\n", path
);
292 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
294 if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
295 targetcli
, file
->cli_fd
, &mode
, &size
,
301 time_t change_time
, access_time
, write_time
;
303 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli
, file
->cli_fd
, &mode
, &size
,
304 &change_time
, &access_time
, &write_time
))) {
309 change_time_ts
= convert_time_t_to_timespec(change_time
);
310 access_time_ts
= convert_time_t_to_timespec(access_time
);
311 write_time_ts
= convert_time_t_to_timespec(write_time
);
330 * Routine to obtain file system information given a path
333 SMBC_statvfs_ctx(SMBCCTX
*context
,
341 TALLOC_CTX
*frame
= talloc_stackframe();
343 /* Determine if the provided path is a file or a folder */
344 if (SMBC_stat_ctx(context
, path
, &statbuf
) < 0) {
349 /* Is it a file or a directory? */
350 if (S_ISDIR(statbuf
.st_mode
)) {
351 /* It's a directory. */
352 if ((pFile
= SMBC_opendir_ctx(context
, path
)) == NULL
) {
357 } else if (S_ISREG(statbuf
.st_mode
)) {
359 if ((pFile
= SMBC_open_ctx(context
, path
,
360 O_RDONLY
, 0)) == NULL
) {
366 /* It's neither a file nor a directory. Not supported. */
372 /* Now we have an open file handle, so just use SMBC_fstatvfs */
373 ret
= SMBC_fstatvfs_ctx(context
, pFile
, st
);
375 /* Close the file or directory */
377 SMBC_closedir_ctx(context
, pFile
);
379 SMBC_close_ctx(context
, pFile
);
388 * Routine to obtain file system information given an fd
392 SMBC_fstatvfs_ctx(SMBCCTX
*context
,
396 unsigned long flags
= 0;
397 uint32_t fs_attrs
= 0;
398 struct cli_state
*cli
= file
->srv
->cli
;
399 struct smbXcli_tcon
*tcon
;
400 TALLOC_CTX
*frame
= talloc_stackframe();
402 if (smbXcli_conn_protocol(cli
->conn
) >= PROTOCOL_SMB2_02
) {
403 tcon
= cli
->smb2
.tcon
;
405 tcon
= cli
->smb1
.tcon
;
408 /* Initialize all fields (at least until we actually use them) */
409 memset(st
, 0, sizeof(*st
));
412 * The state of each flag is such that the same bits are unset as
413 * would typically be unset on a local file system on a POSIX OS. Thus
414 * the bit is on, for example, only for case-insensitive file systems
415 * since most POSIX file systems are case sensitive and fstatvfs()
416 * would typically return zero in these bits on such a local file
420 /* See if the server has UNIX CIFS support */
421 if (! SERVER_HAS_UNIX_CIFS(cli
)) {
422 uint64_t total_allocation_units
;
423 uint64_t caller_allocation_units
;
424 uint64_t actual_allocation_units
;
425 uint64_t sectors_per_allocation_unit
;
426 uint64_t bytes_per_sector
;
429 /* Nope. If size data is available... */
430 status
= cli_get_fs_full_size_info(cli
,
431 &total_allocation_units
,
432 &caller_allocation_units
,
433 &actual_allocation_units
,
434 §ors_per_allocation_unit
,
436 if (NT_STATUS_IS_OK(status
)) {
438 /* ... then provide it */
440 (unsigned long) bytes_per_sector
;
443 (unsigned long) sectors_per_allocation_unit
;
446 (fsblkcnt_t
) total_allocation_units
;
448 (fsblkcnt_t
) actual_allocation_units
;
450 (fsblkcnt_t
) caller_allocation_units
;
453 flags
|= SMBC_VFS_FEATURE_NO_UNIXCIFS
;
455 uint32_t optimal_transfer_size
;
457 uint64_t total_blocks
;
458 uint64_t blocks_available
;
459 uint64_t user_blocks_available
;
460 uint64_t total_file_nodes
;
461 uint64_t free_file_nodes
;
462 uint64_t fs_identifier
;
465 /* Has UNIXCIFS. If POSIX filesystem info is available... */
466 status
= cli_get_posix_fs_info(cli
,
467 &optimal_transfer_size
,
471 &user_blocks_available
,
475 if (NT_STATUS_IS_OK(status
)) {
477 /* ... then what's provided here takes precedence. */
479 (unsigned long) block_size
;
481 (fsblkcnt_t
) total_blocks
;
483 (fsblkcnt_t
) blocks_available
;
485 (fsblkcnt_t
) user_blocks_available
;
487 (fsfilcnt_t
) total_file_nodes
;
489 (fsfilcnt_t
) free_file_nodes
;
492 (unsigned long) fs_identifier
;
497 /* See if the share is case sensitive */
498 if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(cli
, &fs_attrs
))) {
500 * We can't determine the case sensitivity of
501 * the share. We have no choice but to use the
502 * user-specified case sensitivity setting.
504 if (! smbc_getOptionCaseSensitive(context
)) {
505 flags
|= SMBC_VFS_FEATURE_CASE_INSENSITIVE
;
508 if (! (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
)) {
509 flags
|= SMBC_VFS_FEATURE_CASE_INSENSITIVE
;
513 /* See if DFS is supported */
514 if (smbXcli_conn_dfs_supported(cli
->conn
) &&
515 smbXcli_tcon_is_dfs_share(tcon
))
517 flags
|= SMBC_VFS_FEATURE_DFS
;
520 #if defined(HAVE_STATVFS_F_FLAG)
522 #elif defined(HAVE_STATVFS_F_FLAGS)