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
) {
45 if (!*name
) return 2; /* FIXME, why 2 ??? */
46 return (ino_t
)str_checksum(name
);
51 * Routine to put basic stat info into a stat structure ... Used by stat and
56 setup_stat(SMBCCTX
*context
,
62 TALLOC_CTX
*frame
= talloc_stackframe();
66 if (IS_DOS_DIR(mode
)) {
67 st
->st_mode
= SMBC_DIR_MODE
;
69 st
->st_mode
= SMBC_FILE_MODE
;
72 if (IS_DOS_ARCHIVE(mode
)) st
->st_mode
|= S_IXUSR
;
73 if (IS_DOS_SYSTEM(mode
)) st
->st_mode
|= S_IXGRP
;
74 if (IS_DOS_HIDDEN(mode
)) st
->st_mode
|= S_IXOTH
;
75 if (!IS_DOS_READONLY(mode
)) st
->st_mode
|= S_IWUSR
;
78 #ifdef HAVE_STAT_ST_BLKSIZE
81 #ifdef HAVE_STAT_ST_BLOCKS
82 st
->st_blocks
= (size
+511)/512;
84 #ifdef HAVE_STRUCT_STAT_ST_RDEV
87 st
->st_uid
= getuid();
88 st
->st_gid
= getgid();
90 if (IS_DOS_DIR(mode
)) {
96 if (st
->st_ino
== 0) {
97 st
->st_ino
= generate_inode(context
, fname
);
101 return True
; /* FIXME: Is this needed ? */
106 * Routine to stat a file given a name
110 SMBC_stat_ctx(SMBCCTX
*context
,
118 char *password
= NULL
;
119 char *workgroup
= NULL
;
121 struct timespec write_time_ts
;
122 struct timespec access_time_ts
;
123 struct timespec change_time_ts
;
127 TALLOC_CTX
*frame
= talloc_stackframe();
129 if (!context
|| !context
->internal
->initialized
) {
131 errno
= EINVAL
; /* Best I can think of ... */
142 DEBUG(4, ("smbc_stat(%s)\n", fname
));
144 if (SMBC_parse_path(frame
,
159 if (!user
|| user
[0] == (char)0) {
160 user
= talloc_strdup(frame
, smbc_getUser(context
));
168 srv
= SMBC_server(frame
, context
, True
,
169 server
, share
, &workgroup
, &user
, &password
);
173 return -1; /* errno set by SMBC_server */
176 if (!SMBC_getatr(context
, srv
, path
, &mode
, &size
,
182 errno
= SMBC_errno(context
, srv
->cli
);
189 setup_stat(context
, st
, (char *) fname
, size
, mode
);
191 set_atimespec(st
, access_time_ts
);
192 set_ctimespec(st
, change_time_ts
);
193 set_mtimespec(st
, write_time_ts
);
194 st
->st_dev
= srv
->dev
;
202 * Routine to stat a file given an fd
206 SMBC_fstat_ctx(SMBCCTX
*context
,
210 struct timespec change_time_ts
;
211 struct timespec access_time_ts
;
212 struct timespec write_time_ts
;
218 char *password
= NULL
;
220 char *targetpath
= NULL
;
221 struct cli_state
*targetcli
= NULL
;
223 TALLOC_CTX
*frame
= talloc_stackframe();
225 if (!context
|| !context
->internal
->initialized
) {
232 if (!file
|| !SMBC_dlist_contains(context
->internal
->files
, file
)) {
240 return smbc_getFunctionFstatdir(context
)(context
, file
, st
);
243 /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
244 if (SMBC_parse_path(frame
,
259 /*d_printf(">>>fstat: resolving %s\n", path);*/
260 if (!cli_resolve_path(frame
, "", file
->srv
->cli
, path
,
261 &targetcli
, &targetpath
)) {
262 d_printf("Could not resolve %s\n", path
);
266 /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
268 if (!cli_qfileinfo(targetcli
, file
->cli_fd
, &mode
, &size
,
275 time_t change_time
, access_time
, write_time
;
277 if (!cli_getattrE(targetcli
, file
->cli_fd
, &mode
, &size
,
278 &change_time
, &access_time
, &write_time
)) {
285 change_time_ts
= convert_time_t_to_timespec(change_time
);
286 access_time_ts
= convert_time_t_to_timespec(access_time
);
287 write_time_ts
= convert_time_t_to_timespec(write_time
);
292 setup_stat(context
, st
, file
->fname
, size
, mode
);
294 set_atimespec(st
, access_time_ts
);
295 set_ctimespec(st
, change_time_ts
);
296 set_mtimespec(st
, write_time_ts
);
297 st
->st_dev
= file
->srv
->dev
;
306 * Routine to obtain file system information given a path
309 SMBC_statvfs_ctx(SMBCCTX
*context
,
318 /* Determine if the provided path is a file or a folder */
319 if (SMBC_stat_ctx(context
, path
, &statbuf
) < 0) {
323 /* Is it a file or a directory? */
324 if (S_ISDIR(statbuf
.st_mode
)) {
325 /* It's a directory. */
326 if ((pFile
= SMBC_opendir_ctx(context
, path
)) == NULL
) {
330 } else if (S_ISREG(statbuf
.st_mode
)) {
332 if ((pFile
= SMBC_open_ctx(context
, path
,
333 O_RDONLY
, 0)) == NULL
) {
338 /* It's neither a file nor a directory. Not supported. */
343 /* Now we have an open file handle, so just use SMBC_fstatvfs */
344 ret
= SMBC_fstatvfs_ctx(context
, pFile
, st
);
346 /* Close the file or directory */
348 SMBC_closedir_ctx(context
, pFile
);
350 SMBC_close_ctx(context
, pFile
);
358 * Routine to obtain file system information given an fd
362 SMBC_fstatvfs_ctx(SMBCCTX
*context
,
366 unsigned long flags
= 0;
368 struct cli_state
*cli
= file
->srv
->cli
;
371 /* Initialize all fields (at least until we actually use them) */
372 memset(st
, 0, sizeof(*st
));
375 * The state of each flag is such that the same bits are unset as
376 * would typically be unset on a local file system on a POSIX OS. Thus
377 * the bit is on, for example, only for case-insensitive file systems
378 * since most POSIX file systems are case sensitive and fstatvfs()
379 * would typically return zero in these bits on such a local file
383 /* See if the server has UNIX CIFS support */
384 if (! SERVER_HAS_UNIX_CIFS(cli
)) {
385 uint64_t total_allocation_units
;
386 uint64_t caller_allocation_units
;
387 uint64_t actual_allocation_units
;
388 uint64_t sectors_per_allocation_unit
;
389 uint64_t bytes_per_sector
;
391 /* Nope. If size data is available... */
392 if (cli_get_fs_full_size_info(cli
,
393 &total_allocation_units
,
394 &caller_allocation_units
,
395 &actual_allocation_units
,
396 §ors_per_allocation_unit
,
397 &bytes_per_sector
)) {
399 /* ... then provide it */
401 (unsigned long) bytes_per_sector
;
404 (unsigned long) sectors_per_allocation_unit
;
407 (fsblkcnt_t
) total_allocation_units
;
409 (fsblkcnt_t
) actual_allocation_units
;
412 flags
|= SMBC_VFS_FEATURE_NO_UNIXCIFS
;
414 uint32 optimal_transfer_size
;
416 uint64_t total_blocks
;
417 uint64_t blocks_available
;
418 uint64_t user_blocks_available
;
419 uint64_t total_file_nodes
;
420 uint64_t free_file_nodes
;
421 uint64_t fs_identifier
;
423 /* Has UNIXCIFS. If POSIX filesystem info is available... */
424 if (cli_get_posix_fs_info(cli
,
425 &optimal_transfer_size
,
429 &user_blocks_available
,
434 /* ... then what's provided here takes precedence. */
436 (unsigned long) block_size
;
438 (fsblkcnt_t
) total_blocks
;
440 (fsblkcnt_t
) blocks_available
;
442 (fsblkcnt_t
) user_blocks_available
;
444 (fsfilcnt_t
) total_file_nodes
;
446 (fsfilcnt_t
) free_file_nodes
;
449 (unsigned long) fs_identifier
;
454 /* See if the share is case sensitive */
455 if (!cli_get_fs_attr_info(cli
, &fs_attrs
)) {
457 * We can't determine the case sensitivity of
458 * the share. We have no choice but to use the
459 * user-specified case sensitivity setting.
461 if (! smbc_getOptionCaseSensitive(context
)) {
462 flags
|= SMBC_VFS_FEATURE_CASE_INSENSITIVE
;
465 if (! (fs_attrs
& FILE_CASE_SENSITIVE_SEARCH
)) {
466 flags
|= SMBC_VFS_FEATURE_CASE_INSENSITIVE
;
470 /* See if DFS is supported */
471 if ((cli
->capabilities
& CAP_DFS
) && cli
->dfsroot
) {
472 flags
|= SMBC_VFS_FEATURE_DFS
;
475 #if HAVE_STATVFS_F_FLAG
477 #elif HAVE_STATVFS_F_FLAGS