move to SAFE_FREE()
[samba.git] / source / smbd / notify_hash.c
1 /*
2    Unix SMB/Netbios implementation.
3    Version 3.0
4    change notify handling - hash based implementation
5    Copyright (C) Jeremy Allison 1994-1998
6    Copyright (C) Andrew Tridgell 2000
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
25 extern int DEBUGLEVEL;
26
27
28 struct change_data {
29         time_t last_check_time; /* time we last checked this entry */
30         time_t modify_time; /* Info from the directory we're monitoring. */ 
31         time_t status_time; /* Info from the directory we're monitoring. */
32         time_t total_time; /* Total time of all directory entries - don't care if it wraps. */
33         unsigned int num_entries; /* Zero or the number of files in the directory. */
34 };
35
36
37 /****************************************************************************
38  Create the hash we will use to determine if the contents changed.
39 *****************************************************************************/
40 static BOOL notify_hash(connection_struct *conn, char *path, uint32 flags, 
41                         struct change_data *data, struct change_data *old_data)
42 {
43         SMB_STRUCT_STAT st;
44         pstring full_name;
45         char *p;
46         char *fname;
47         size_t remaining_len;
48         size_t fullname_len;
49         void *dp;
50
51         ZERO_STRUCTP(data);
52
53         if(vfs_stat(conn,path, &st) == -1) return False;
54
55         data->modify_time = st.st_mtime;
56         data->status_time = st.st_ctime;
57
58         if (old_data) {
59                 /*
60                  * Shortcut to avoid directory scan if the time
61                  * has changed - we always must return true then.
62                  */
63                 if (old_data->modify_time != data->modify_time ||
64                         old_data->status_time != data->status_time ) {
65                                 return True;
66                 }
67         }
68  
69         /*
70          * If we are to watch for changes that are only stored
71          * in inodes of files, not in the directory inode, we must
72          * scan the directory and produce a unique identifier with
73          * which we can determine if anything changed. We use the
74          * modify and change times from all the files in the
75          * directory, added together (ignoring wrapping if it's
76          * larger than the max time_t value).
77          */
78
79         if (!(flags & (FILE_NOTIFY_CHANGE_SIZE|FILE_NOTIFY_CHANGE_LAST_WRITE))) return True;
80
81         dp = OpenDir(conn, path, True);
82         if (dp == NULL) return False;
83
84         data->num_entries = 0;
85         
86         pstrcpy(full_name, path);
87         pstrcat(full_name, "/");
88         
89         fullname_len = strlen(full_name);
90         remaining_len = sizeof(full_name) - fullname_len - 1;
91         p = &full_name[fullname_len];
92         
93         while ((fname = ReadDirName(dp))) {
94                 if(strequal(fname, ".") || strequal(fname, "..")) continue;             
95
96                 data->num_entries++;
97                 safe_strcpy(p, fname, remaining_len);
98
99                 ZERO_STRUCT(st);
100
101                 /*
102                  * Do the stat - but ignore errors.
103                  */             
104                 vfs_stat(conn,full_name, &st);
105                 data->total_time += (st.st_mtime + st.st_ctime);
106         }
107         
108         CloseDir(dp);
109         
110         return True;
111 }
112
113
114 /****************************************************************************
115 register a change notify request
116 *****************************************************************************/
117 static void *hash_register_notify(connection_struct *conn, char *path, uint32 flags)
118 {
119         struct change_data data;
120
121         if (!notify_hash(conn, path, flags, &data, NULL)) return NULL;
122
123         data.last_check_time = time(NULL);
124
125         return (void *)memdup(&data, sizeof(data));
126 }
127
128 /****************************************************************************
129  Check if a change notify should be issued.
130  A time of zero means instantaneous check - don't modify the last check time.
131 *****************************************************************************/
132 static BOOL hash_check_notify(connection_struct *conn, uint16 vuid, char *path, uint32 flags, void *datap, time_t t)
133 {
134         struct change_data *data = (struct change_data *)datap;
135         struct change_data data2;
136
137         if (t && t < data->last_check_time + lp_change_notify_timeout()) return False;
138
139         if (!become_user(conn,vuid)) return True;
140         if (!become_service(conn,True)) {
141                 unbecome_user();
142                 return True;
143         }
144
145         if (!notify_hash(conn, path, flags, &data2, data) ||
146             data2.modify_time != data->modify_time ||
147             data2.status_time != data->status_time ||
148             data2.total_time != data->total_time ||
149             data2.num_entries != data->num_entries) {
150                 unbecome_user();
151                 return True;
152         }
153
154         if (t)
155                 data->last_check_time = t;
156
157         unbecome_user();
158
159         return False;
160 }
161
162 /****************************************************************************
163 remove a change notify data structure
164 *****************************************************************************/
165 static void hash_remove_notify(void *datap)
166 {
167         SAFE_FREE(datap);
168 }
169
170
171 /****************************************************************************
172 setup hash based change notify
173 ****************************************************************************/
174 struct cnotify_fns *hash_notify_init(void) 
175 {
176         static struct cnotify_fns cnotify;
177
178         cnotify.register_notify = hash_register_notify;
179         cnotify.check_notify = hash_check_notify;
180         cnotify.remove_notify = hash_remove_notify;
181         cnotify.select_time = lp_change_notify_timeout();
182
183         return &cnotify;
184 }
185
186
187 /*
188   change_notify_reply_packet(cnbp->request_buf,ERRSRV,ERRaccess);
189   change_notify_reply_packet(cnbp->request_buf,0,NT_STATUS_NOTIFY_ENUM_DIR);
190
191   chain_size = 0;
192   file_chain_reset();
193
194   uint16 vuid = (lp_security() == SEC_SHARE) ? UID_FIELD_INVALID : 
195   SVAL(cnbp->request_buf,smb_uid);
196 */