r2503: the RAW-SEARCH test now mostly passes against the posix backend
[samba.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 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 "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  Return the major devicenumber for UNIX extensions.
40 */
41 static uint32_t unix_dev_major(dev_t dev)
42 {
43 #if defined(HAVE_DEVICE_MAJOR_FN)
44         return (uint32)major(dev);
45 #else
46         return (uint32)(dev >> 8);
47 #endif
48 }
49
50 /*
51  Return the minor devicenumber for UNIX extensions.
52 */
53 static uint32_t unix_dev_minor(dev_t dev)
54 {
55 #if defined(HAVE_DEVICE_MINOR_FN)
56         return (uint32)minor(dev);
57 #else
58         return (uint32)(dev & 0xff);
59 #endif
60 }
61
62 /*
63  Return the filetype for UNIX extensions
64 */
65 static uint32_t unix_filetype(mode_t mode)
66 {
67         if (S_ISREG(mode)) return UNIX_TYPE_FILE;
68         if (S_ISDIR(mode)) return UNIX_TYPE_DIR;
69 #ifdef S_ISLNK
70         if (S_ISLNK(mode)) return UNIX_TYPE_SYMLINK;
71 #endif
72 #ifdef S_ISCHR
73         if (S_ISCHR(mode)) return UNIX_TYPE_CHARDEV;
74 #endif
75 #ifdef S_ISBLK
76         if (S_ISBLK(mode)) return UNIX_TYPE_BLKDEV;
77 #endif
78 #ifdef S_ISFIFO
79         if (S_ISFIFO(mode)) return UNIX_TYPE_FIFO;
80 #endif
81 #ifdef S_ISSOCK
82         if (S_ISSOCK(mode)) return UNIX_TYPE_SOCKET;
83 #endif
84
85         DEBUG(0,("unix_filetype: unknown filetype %u", (unsigned)mode));
86         return UNIX_TYPE_UNKNOWN;
87 }
88
89
90 /****************************************************************************
91  Change a unix mode to a dos mode.
92 ****************************************************************************/
93 static uint32_t dos_mode_from_stat(struct pvfs_state *pvfs, struct stat *st)
94 {
95         int result = 0;
96
97         if ((st->st_mode & S_IWUSR) == 0)
98                 result |= FILE_ATTRIBUTE_READONLY;
99         
100         if ((pvfs->flags & PVFS_FLAG_MAP_ARCHIVE) && ((st->st_mode & S_IXUSR) != 0))
101                 result |= FILE_ATTRIBUTE_ARCHIVE;
102
103         if ((pvfs->flags & PVFS_FLAG_MAP_SYSTEM) && ((st->st_mode & S_IXGRP) != 0))
104                 result |= FILE_ATTRIBUTE_SYSTEM;
105         
106         if ((pvfs->flags & PVFS_FLAG_MAP_HIDDEN) && ((st->st_mode & S_IXOTH) != 0))
107                 result |= FILE_ATTRIBUTE_HIDDEN;
108   
109         if (S_ISDIR(st->st_mode))
110                 result = FILE_ATTRIBUTE_DIRECTORY | (result & FILE_ATTRIBUTE_READONLY);
111
112 #if defined (HAVE_STAT_ST_BLOCKS) && defined (HAVE_STAT_ST_BLKSIZE)
113         if (st->st_size > st->st_blocks * (off_t)st->st_blksize) {
114                 result |= FILE_ATTRIBUTE_SPARSE;
115         }
116 #endif
117
118         if (!(result & 
119               (FILE_ATTRIBUTE_READONLY|
120                FILE_ATTRIBUTE_ARCHIVE|
121                FILE_ATTRIBUTE_SYSTEM|
122                FILE_ATTRIBUTE_HIDDEN|
123                FILE_ATTRIBUTE_DIRECTORY))) {
124                 result |= FILE_ATTRIBUTE_NORMAL;
125         }
126  
127         return result;
128 }
129
130
131
132 /*
133   fill in the dos file attributes for a file
134 */
135 NTSTATUS pvfs_fill_dos_info(struct pvfs_state *pvfs, struct pvfs_filename *name)
136 {
137         /* for now just use the simple samba mapping */
138         unix_to_nt_time(&name->dos.create_time, name->st.st_ctime);
139         unix_to_nt_time(&name->dos.access_time, name->st.st_atime);
140         unix_to_nt_time(&name->dos.write_time, name->st.st_mtime);
141         unix_to_nt_time(&name->dos.change_time, name->st.st_mtime);
142         name->dos.attrib = dos_mode_from_stat(pvfs, &name->st);
143         name->dos.alloc_size = name->st.st_size;
144         name->dos.nlink = name->st.st_nlink;
145         name->dos.ea_size = 0;
146         name->dos.file_id = (((uint64_t)name->st.st_dev)<<32) | name->st.st_ino;
147
148         return NT_STATUS_OK;
149 }