r23792: convert Samba4 to GPLv3
[garming/samba-autobuild/.git] / source4 / ntvfs / posix / pvfs_fileinfo.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - 
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "vfs_posix.h"
24
25 /****************************************************************************
26  Change a unix mode to a dos mode.
27 ****************************************************************************/
28 static uint32_t dos_mode_from_stat(struct pvfs_state *pvfs, struct stat *st)
29 {
30         int result = 0;
31
32         if ((st->st_mode & S_IWUSR) == 0)
33                 result |= FILE_ATTRIBUTE_READONLY;
34         
35         if ((pvfs->flags & PVFS_FLAG_MAP_ARCHIVE) && ((st->st_mode & S_IXUSR) != 0))
36                 result |= FILE_ATTRIBUTE_ARCHIVE;
37         
38         if ((pvfs->flags & PVFS_FLAG_MAP_SYSTEM) && ((st->st_mode & S_IXGRP) != 0))
39                 result |= FILE_ATTRIBUTE_SYSTEM;
40         
41         if ((pvfs->flags & PVFS_FLAG_MAP_HIDDEN) && ((st->st_mode & S_IXOTH) != 0))
42                 result |= FILE_ATTRIBUTE_HIDDEN;
43   
44         if (S_ISDIR(st->st_mode))
45                 result = FILE_ATTRIBUTE_DIRECTORY | (result & FILE_ATTRIBUTE_READONLY);
46
47         return result;
48 }
49
50
51
52 /*
53   fill in the dos file attributes for a file
54 */
55 NTSTATUS pvfs_fill_dos_info(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
56 {
57         /* make directories appear as size 0 with 1 link */
58         if (S_ISDIR(name->st.st_mode)) {
59                 name->st.st_size = 0;
60                 name->st.st_nlink = 1;
61         }
62
63         /* for now just use the simple samba mapping */
64         unix_to_nt_time(&name->dos.create_time, name->st.st_ctime);
65         unix_to_nt_time(&name->dos.access_time, name->st.st_atime);
66         unix_to_nt_time(&name->dos.write_time,  name->st.st_mtime);
67         unix_to_nt_time(&name->dos.change_time, name->st.st_ctime);
68 #ifdef HAVE_STAT_TV_NSEC
69         name->dos.create_time += name->st.st_ctim.tv_nsec / 100;
70         name->dos.access_time += name->st.st_atim.tv_nsec / 100;
71         name->dos.write_time  += name->st.st_mtim.tv_nsec / 100;
72         name->dos.change_time += name->st.st_ctim.tv_nsec / 100;
73 #endif
74         name->dos.attrib = dos_mode_from_stat(pvfs, &name->st);
75         name->dos.alloc_size = pvfs_round_alloc_size(pvfs, name->st.st_size);
76         name->dos.nlink = name->st.st_nlink;
77         name->dos.ea_size = 4;
78         name->dos.file_id = (((uint64_t)name->st.st_dev)<<32) | name->st.st_ino;
79         name->dos.flags = 0;
80
81         return pvfs_dosattrib_load(pvfs, name, fd);
82 }
83
84
85 /*
86   return a set of unix file permissions for a new file or directory
87 */
88 mode_t pvfs_fileperms(struct pvfs_state *pvfs, uint32_t attrib)
89 {
90         mode_t mode = (S_IRUSR | S_IRGRP | S_IROTH | S_IWUSR | S_IWGRP | S_IWOTH);
91
92         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE) &&
93             (attrib & FILE_ATTRIBUTE_READONLY)) {
94                 mode &= ~(S_IWUSR | S_IWGRP | S_IWOTH);
95         }
96
97         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
98                 if ((attrib & FILE_ATTRIBUTE_ARCHIVE) &&
99                     (pvfs->flags & PVFS_FLAG_MAP_ARCHIVE)) {
100                         mode |= S_IXUSR;
101                 }
102                 if ((attrib & FILE_ATTRIBUTE_SYSTEM) &&
103                     (pvfs->flags & PVFS_FLAG_MAP_SYSTEM)) {
104                         mode |= S_IXGRP;
105                 }
106                 if ((attrib & FILE_ATTRIBUTE_HIDDEN) &&
107                     (pvfs->flags & PVFS_FLAG_MAP_HIDDEN)) {
108                         mode |= S_IXOTH;
109                 }
110         }
111
112         if (attrib & FILE_ATTRIBUTE_DIRECTORY) {
113                 mode |= (S_IFDIR | S_IWUSR);
114                 mode |= (S_IXUSR | S_IXGRP | S_IXOTH);                 
115                 mode &= pvfs->options.dir_mask;
116                 mode |= pvfs->options.force_dir_mode;
117         } else {
118                 mode &= pvfs->options.create_mask;
119                 mode |= pvfs->options.force_create_mode;
120         }
121
122         return mode;
123 }
124
125