r3545: initial support for using extended attributes to hold extended dos attributes...
[samba.git] / source / 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 2 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, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "vfs_posix.h"
25
26
27 /* UNIX filetype mappings. */
28 #define UNIX_TYPE_FILE 0
29 #define UNIX_TYPE_DIR 1
30 #define UNIX_TYPE_SYMLINK 2
31 #define UNIX_TYPE_CHARDEV 3
32 #define UNIX_TYPE_BLKDEV 4
33 #define UNIX_TYPE_FIFO 5
34 #define UNIX_TYPE_SOCKET 6
35 #define UNIX_TYPE_UNKNOWN 0xFFFFFFFF
36
37
38 /****************************************************************************
39  Change a unix mode to a dos mode.
40 ****************************************************************************/
41 static uint32_t dos_mode_from_stat(struct pvfs_state *pvfs, struct stat *st)
42 {
43         int result = 0;
44
45         if ((st->st_mode & S_IWUSR) == 0)
46                 result |= FILE_ATTRIBUTE_READONLY;
47         
48         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
49                 if ((pvfs->flags & PVFS_FLAG_MAP_ARCHIVE) && ((st->st_mode & S_IXUSR) != 0))
50                         result |= FILE_ATTRIBUTE_ARCHIVE;
51
52                 if ((pvfs->flags & PVFS_FLAG_MAP_SYSTEM) && ((st->st_mode & S_IXGRP) != 0))
53                         result |= FILE_ATTRIBUTE_SYSTEM;
54                 
55                 if ((pvfs->flags & PVFS_FLAG_MAP_HIDDEN) && ((st->st_mode & S_IXOTH) != 0))
56                         result |= FILE_ATTRIBUTE_HIDDEN;
57         }
58   
59         if (S_ISDIR(st->st_mode))
60                 result = FILE_ATTRIBUTE_DIRECTORY | (result & FILE_ATTRIBUTE_READONLY);
61
62         if (!(result & 
63               (FILE_ATTRIBUTE_READONLY|
64                FILE_ATTRIBUTE_ARCHIVE|
65                FILE_ATTRIBUTE_SYSTEM|
66                FILE_ATTRIBUTE_HIDDEN|
67                FILE_ATTRIBUTE_DIRECTORY))) {
68                 result |= FILE_ATTRIBUTE_NORMAL;
69         }
70  
71         return result;
72 }
73
74
75
76 /*
77   fill in the dos file attributes for a file
78 */
79 NTSTATUS pvfs_fill_dos_info(struct pvfs_state *pvfs, struct pvfs_filename *name, int fd)
80 {
81         /* make directories appear as size 0 */
82         if (S_ISDIR(name->st.st_mode)) {
83                 name->st.st_size = 0;
84         }
85
86         /* for now just use the simple samba mapping */
87         unix_to_nt_time(&name->dos.create_time, name->st.st_ctime);
88         unix_to_nt_time(&name->dos.access_time, name->st.st_atime);
89         unix_to_nt_time(&name->dos.write_time,  name->st.st_mtime);
90         unix_to_nt_time(&name->dos.change_time, name->st.st_ctime);
91 #ifdef HAVE_STAT_TV_NSEC
92         name->dos.create_time += name->st.st_ctim.tv_nsec / 100;
93         name->dos.access_time += name->st.st_atim.tv_nsec / 100;
94         name->dos.write_time  += name->st.st_mtim.tv_nsec / 100;
95         name->dos.change_time += name->st.st_ctim.tv_nsec / 100;
96 #endif
97         name->dos.attrib = dos_mode_from_stat(pvfs, &name->st);
98         name->dos.alloc_size = name->st.st_size;
99         name->dos.nlink = name->st.st_nlink;
100         name->dos.ea_size = 0;
101         name->dos.file_id = (((uint64_t)name->st.st_dev)<<32) | name->st.st_ino;
102
103 #if HAVE_XATTR_SUPPORT
104         if (pvfs->flags & PVFS_FLAG_XATTR_ENABLE) {
105                 return pvfs_xattr_load(pvfs, name, fd);
106         }
107 #endif
108
109         return NT_STATUS_OK;
110 }
111
112
113 /*
114   return a set of unix file permissions for a new file or directory
115 */
116 mode_t pvfs_fileperms(struct pvfs_state *pvfs, uint32 attrib)
117 {
118         mode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
119
120         if (attrib & FILE_ATTRIBUTE_DIRECTORY) {
121                 mode |= S_IXUSR | S_IXGRP | S_IXOTH;
122         }
123
124         if (!(attrib & FILE_ATTRIBUTE_READONLY)) {
125                 mode |= S_IWUSR;
126         }
127
128         if (!(pvfs->flags & PVFS_FLAG_XATTR_ENABLE)) {
129                 if ((attrib & FILE_ATTRIBUTE_ARCHIVE) &&
130                     (pvfs->flags & PVFS_FLAG_MAP_ARCHIVE)) {
131                         mode |= S_IXUSR;
132                 }
133                 
134                 if ((attrib & FILE_ATTRIBUTE_SYSTEM) &&
135                     (pvfs->flags & PVFS_FLAG_MAP_SYSTEM)) {
136                         mode |= S_IXGRP;
137                 }
138                 
139                 if ((attrib & FILE_ATTRIBUTE_HIDDEN) &&
140                     (pvfs->flags & PVFS_FLAG_MAP_HIDDEN)) {
141                         mode |= S_IXOTH;
142                 }
143         }
144
145         return mode;
146 }