Convert all uint32/16/8 to _t in source3/libsmb.
[bbaumbach/samba-autobuild/.git] / source3 / libsmb / libsmb_stat.c
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
10
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.
15
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.
20
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/>.
23 */
24
25 #include "includes.h"
26 #include "libsmb/libsmb.h"
27 #include "libsmbclient.h"
28 #include "libsmb_internal.h"
29 #include "../libcli/smb/smbXcli_base.h"
30
31 /*
32  * Generate an inode number from file name for those things that need it
33  */
34
35 static ino_t
36 generate_inode(SMBCCTX *context,
37                const char *name)
38 {
39         if (!context || !context->internal->initialized) {
40                 errno = EINVAL;
41                 return -1;
42         }
43
44         if (!*name) return 2; /* FIXME, why 2 ??? */
45         return (ino_t)str_checksum(name);
46 }
47
48 /*
49  * Routine to put basic stat info into a stat structure ... Used by stat and
50  * fstat below.
51  */
52
53 static int
54 setup_stat(SMBCCTX *context,
55            struct stat *st,
56            const char *fname,
57            off_t size,
58            int mode)
59 {
60         TALLOC_CTX *frame = talloc_stackframe();
61
62         st->st_mode = 0;
63
64         if (IS_DOS_DIR(mode)) {
65                 st->st_mode = SMBC_DIR_MODE;
66         } else {
67                 st->st_mode = SMBC_FILE_MODE;
68         }
69
70         if (IS_DOS_ARCHIVE(mode)) st->st_mode |= S_IXUSR;
71         if (IS_DOS_SYSTEM(mode)) st->st_mode |= S_IXGRP;
72         if (IS_DOS_HIDDEN(mode)) st->st_mode |= S_IXOTH;
73         if (!IS_DOS_READONLY(mode)) st->st_mode |= S_IWUSR;
74
75         st->st_size = size;
76 #ifdef HAVE_STAT_ST_BLKSIZE
77         st->st_blksize = 512;
78 #endif
79 #ifdef HAVE_STAT_ST_BLOCKS
80         st->st_blocks = (size+511)/512;
81 #endif
82 #ifdef HAVE_STRUCT_STAT_ST_RDEV
83         st->st_rdev = 0;
84 #endif
85         st->st_uid = getuid();
86         st->st_gid = getgid();
87
88         if (IS_DOS_DIR(mode)) {
89                 st->st_nlink = 2;
90         } else {
91                 st->st_nlink = 1;
92         }
93
94         if (st->st_ino == 0) {
95                 st->st_ino = generate_inode(context, fname);
96         }
97
98         TALLOC_FREE(frame);
99         return True;  /* FIXME: Is this needed ? */
100 }
101
102 /*
103  * Routine to stat a file given a name
104  */
105
106 int
107 SMBC_stat_ctx(SMBCCTX *context,
108               const char *fname,
109               struct stat *st)
110 {
111         SMBCSRV *srv = NULL;
112         char *server = NULL;
113         char *share = NULL;
114         char *user = NULL;
115         char *password = NULL;
116         char *workgroup = NULL;
117         char *path = NULL;
118         struct timespec write_time_ts;
119         struct timespec access_time_ts;
120         struct timespec change_time_ts;
121         off_t size = 0;
122         uint16_t mode = 0;
123         uint16_t port = 0;
124         SMB_INO_T ino = 0;
125         TALLOC_CTX *frame = talloc_stackframe();
126
127         if (!context || !context->internal->initialized) {
128                 errno = EINVAL;  /* Best I can think of ... */
129                 TALLOC_FREE(frame);
130                 return -1;
131         }
132
133         if (!fname) {
134                 errno = EINVAL;
135                 TALLOC_FREE(frame);
136                 return -1;
137         }
138
139         DEBUG(4, ("smbc_stat(%s)\n", fname));
140
141         if (SMBC_parse_path(frame,
142                             context,
143                             fname,
144                             &workgroup,
145                             &server,
146                             &port,
147                             &share,
148                             &path,
149                             &user,
150                             &password,
151                             NULL)) {
152                 errno = EINVAL;
153                 TALLOC_FREE(frame);
154                 return -1;
155         }
156
157         if (!user || user[0] == (char)0) {
158                 user = talloc_strdup(frame, smbc_getUser(context));
159                 if (!user) {
160                         errno = ENOMEM;
161                         TALLOC_FREE(frame);
162                         return -1;
163                 }
164         }
165
166         srv = SMBC_server(frame, context, True,
167                           server, port, share, &workgroup, &user, &password);
168         if (!srv) {
169                 TALLOC_FREE(frame);
170                 return -1;  /* errno set by SMBC_server */
171         }
172
173         if (!SMBC_getatr(context, srv, path, &mode, &size,
174                          NULL,
175                          &access_time_ts,
176                          &write_time_ts,
177                          &change_time_ts,
178                          &ino)) {
179                 errno = SMBC_errno(context, srv->cli);
180                 TALLOC_FREE(frame);
181                 return -1;
182         }
183
184         st->st_ino = ino;
185
186         setup_stat(context, st, fname, size, mode);
187
188         st->st_atime = convert_timespec_to_time_t(access_time_ts);
189         st->st_ctime = convert_timespec_to_time_t(change_time_ts);
190         st->st_mtime = convert_timespec_to_time_t(write_time_ts);
191         st->st_dev   = srv->dev;
192
193         TALLOC_FREE(frame);
194         return 0;
195 }
196
197 /*
198  * Routine to stat a file given an fd
199  */
200
201 int
202 SMBC_fstat_ctx(SMBCCTX *context,
203                SMBCFILE *file,
204                struct stat *st)
205 {
206         struct timespec change_time_ts;
207         struct timespec access_time_ts;
208         struct timespec write_time_ts;
209         off_t size;
210         uint16_t mode;
211         char *server = NULL;
212         char *share = NULL;
213         char *user = NULL;
214         char *password = NULL;
215         char *path = NULL;
216         char *targetpath = NULL;
217         struct cli_state *targetcli = NULL;
218         SMB_INO_T ino = 0;
219         uint16_t port = 0;
220         TALLOC_CTX *frame = talloc_stackframe();
221         NTSTATUS status;
222
223         if (!context || !context->internal->initialized) {
224                 errno = EINVAL;
225                 TALLOC_FREE(frame);
226                 return -1;
227         }
228
229         if (!file || !SMBC_dlist_contains(context->internal->files, file)) {
230                 errno = EBADF;
231                 TALLOC_FREE(frame);
232                 return -1;
233         }
234
235         if (!file->file) {
236                 TALLOC_FREE(frame);
237                 return smbc_getFunctionFstatdir(context)(context, file, st);
238         }
239
240         /*d_printf(">>>fstat: parsing %s\n", file->fname);*/
241         if (SMBC_parse_path(frame,
242                             context,
243                             file->fname,
244                             NULL,
245                             &server,
246                             &port,
247                             &share,
248                             &path,
249                             &user,
250                             &password,
251                             NULL)) {
252                 errno = EINVAL;
253                 TALLOC_FREE(frame);
254                 return -1;
255         }
256
257         /*d_printf(">>>fstat: resolving %s\n", path);*/
258         status = cli_resolve_path(frame, "", context->internal->auth_info,
259                                   file->srv->cli, path,
260                                   &targetcli, &targetpath);
261         if (!NT_STATUS_IS_OK(status)) {
262                 d_printf("Could not resolve %s\n", path);
263                 errno = ENOENT;
264                 TALLOC_FREE(frame);
265                 return -1;
266         }
267         /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/
268
269         if (!NT_STATUS_IS_OK(cli_qfileinfo_basic(
270                                      targetcli, file->cli_fd, &mode, &size,
271                                      NULL,
272                                      &access_time_ts,
273                                      &write_time_ts,
274                                      &change_time_ts,
275                                      &ino))) {
276                 time_t change_time, access_time, write_time;
277
278                 if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd, &mode, &size,
279                                   &change_time, &access_time, &write_time))) {
280                         errno = EINVAL;
281                         TALLOC_FREE(frame);
282                         return -1;
283                 }
284                 change_time_ts = convert_time_t_to_timespec(change_time);
285                 access_time_ts = convert_time_t_to_timespec(access_time);
286                 write_time_ts = convert_time_t_to_timespec(write_time);
287         }
288
289         st->st_ino = ino;
290
291         setup_stat(context, st, file->fname, size, mode);
292
293         st->st_atime = convert_timespec_to_time_t(access_time_ts);
294         st->st_ctime = convert_timespec_to_time_t(change_time_ts);
295         st->st_mtime = convert_timespec_to_time_t(write_time_ts);
296         st->st_dev = file->srv->dev;
297
298         TALLOC_FREE(frame);
299         return 0;
300 }
301
302
303 /*
304  * Routine to obtain file system information given a path
305  */
306 int
307 SMBC_statvfs_ctx(SMBCCTX *context,
308                  char *path,
309                  struct statvfs *st)
310 {
311         int             ret;
312         bool            bIsDir;
313         struct stat     statbuf;
314         SMBCFILE *      pFile;
315         TALLOC_CTX *frame = talloc_stackframe();
316
317         /* Determine if the provided path is a file or a folder */
318         if (SMBC_stat_ctx(context, path, &statbuf) < 0) {
319                 TALLOC_FREE(frame);
320                 return -1;
321         }
322
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) {
327                         TALLOC_FREE(frame);
328                         return -1;
329                 }
330                 bIsDir = true;
331         } else if (S_ISREG(statbuf.st_mode)) {
332                 /* It's a file. */
333                 if ((pFile = SMBC_open_ctx(context, path,
334                                            O_RDONLY, 0)) == NULL) {
335                         TALLOC_FREE(frame);
336                         return -1;
337                 }
338                 bIsDir = false;
339         } else {
340                 /* It's neither a file nor a directory. Not supported. */
341                 TALLOC_FREE(frame);
342                 errno = ENOSYS;
343                 return -1;
344         }
345
346         /* Now we have an open file handle, so just use SMBC_fstatvfs */
347         ret = SMBC_fstatvfs_ctx(context, pFile, st);
348
349         /* Close the file or directory */
350         if (bIsDir) {
351                 SMBC_closedir_ctx(context, pFile);
352         } else {
353                 SMBC_close_ctx(context, pFile);
354         }
355
356         TALLOC_FREE(frame);
357         return ret;
358 }
359
360
361 /*
362  * Routine to obtain file system information given an fd
363  */
364
365 int
366 SMBC_fstatvfs_ctx(SMBCCTX *context,
367                   SMBCFILE *file,
368                   struct statvfs *st)
369 {
370         unsigned long flags = 0;
371         uint32_t fs_attrs = 0;
372         struct cli_state *cli = file->srv->cli;
373         struct smbXcli_tcon *tcon;
374         TALLOC_CTX *frame = talloc_stackframe();
375
376         if (smbXcli_conn_protocol(cli->conn) >= PROTOCOL_SMB2_02) {
377                 tcon = cli->smb2.tcon;
378         } else {
379                 tcon = cli->smb1.tcon;
380         }
381
382         /* Initialize all fields (at least until we actually use them) */
383         memset(st, 0, sizeof(*st));
384
385         /*
386          * The state of each flag is such that the same bits are unset as
387          * would typically be unset on a local file system on a POSIX OS. Thus
388          * the bit is on, for example, only for case-insensitive file systems
389          * since most POSIX file systems are case sensitive and fstatvfs()
390          * would typically return zero in these bits on such a local file
391          * system.
392          */
393
394         /* See if the server has UNIX CIFS support */
395         if (! SERVER_HAS_UNIX_CIFS(cli)) {
396                 uint64_t total_allocation_units;
397                 uint64_t caller_allocation_units;
398                 uint64_t actual_allocation_units;
399                 uint64_t sectors_per_allocation_unit;
400                 uint64_t bytes_per_sector;
401                 NTSTATUS status;
402
403                 /* Nope. If size data is available... */
404                 status = cli_get_fs_full_size_info(cli,
405                                                    &total_allocation_units,
406                                                    &caller_allocation_units,
407                                                    &actual_allocation_units,
408                                                    &sectors_per_allocation_unit,
409                                                    &bytes_per_sector);
410                 if (NT_STATUS_IS_OK(status)) {
411
412                         /* ... then provide it */
413                         st->f_bsize =
414                                 (unsigned long) bytes_per_sector;
415 #if HAVE_FRSIZE
416                         st->f_frsize =
417                                 (unsigned long) sectors_per_allocation_unit;
418 #endif
419                         st->f_blocks =
420                                 (fsblkcnt_t) total_allocation_units;
421                         st->f_bfree =
422                                 (fsblkcnt_t) actual_allocation_units;
423                 }
424
425                 flags |= SMBC_VFS_FEATURE_NO_UNIXCIFS;
426         } else {
427                 uint32_t optimal_transfer_size;
428                 uint32_t block_size;
429                 uint64_t total_blocks;
430                 uint64_t blocks_available;
431                 uint64_t user_blocks_available;
432                 uint64_t total_file_nodes;
433                 uint64_t free_file_nodes;
434                 uint64_t fs_identifier;
435                 NTSTATUS status;
436
437                 /* Has UNIXCIFS. If POSIX filesystem info is available... */
438                 status = cli_get_posix_fs_info(cli,
439                                                &optimal_transfer_size,
440                                                &block_size,
441                                                &total_blocks,
442                                                &blocks_available,
443                                                &user_blocks_available,
444                                                &total_file_nodes,
445                                                &free_file_nodes,
446                                                &fs_identifier);
447                 if (NT_STATUS_IS_OK(status)) {
448
449                         /* ... then what's provided here takes precedence. */
450                         st->f_bsize =
451                                 (unsigned long) block_size;
452                         st->f_blocks =
453                                 (fsblkcnt_t) total_blocks;
454                         st->f_bfree =
455                                 (fsblkcnt_t) blocks_available;
456                         st->f_bavail =
457                                 (fsblkcnt_t) user_blocks_available;
458                         st->f_files =
459                                 (fsfilcnt_t) total_file_nodes;
460                         st->f_ffree =
461                                 (fsfilcnt_t) free_file_nodes;
462 #if HAVE_FSID_INT
463                         st->f_fsid =
464                                 (unsigned long) fs_identifier;
465 #endif
466                 }
467         }
468
469         /* See if the share is case sensitive */
470         if (!NT_STATUS_IS_OK(cli_get_fs_attr_info(cli, &fs_attrs))) {
471                 /*
472                  * We can't determine the case sensitivity of
473                  * the share. We have no choice but to use the
474                  * user-specified case sensitivity setting.
475                  */
476                 if (! smbc_getOptionCaseSensitive(context)) {
477                         flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
478                 }
479         } else {
480                 if (! (fs_attrs & FILE_CASE_SENSITIVE_SEARCH)) {
481                         flags |= SMBC_VFS_FEATURE_CASE_INSENSITIVE;
482                 }
483         }
484
485         /* See if DFS is supported */
486         if (smbXcli_conn_dfs_supported(cli->conn) &&
487             smbXcli_tcon_is_dfs_share(tcon))
488         {
489                 flags |= SMBC_VFS_FEATURE_DFS;
490         }
491
492 #if HAVE_STATVFS_F_FLAG
493         st->f_flag = flags;
494 #elif HAVE_STATVFS_F_FLAGS
495         st->f_flags = flags;
496 #endif
497
498         TALLOC_FREE(frame);
499         return 0;
500 }