2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1992-2004.
6 Copyright (C) Volker Lendecke 2005
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.
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.
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.
25 /****************************************************************************
26 Run a file if it is a magic script.
27 ****************************************************************************/
29 static void check_magic(files_struct *fsp,connection_struct *conn)
31 if (!*lp_magicscript(SNUM(conn)))
34 DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
38 if (!(p = strrchr_m(fsp->fsp_name,'/')))
43 if (!strequal(lp_magicscript(SNUM(conn)),p))
54 pstrcpy(fname,fsp->fsp_name);
55 if (*lp_magicoutput(SNUM(conn)))
56 pstrcpy(magic_output,lp_magicoutput(SNUM(conn)));
58 slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);
61 ret = smbrun(fname,&tmp_fd);
62 DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
64 if (ret != 0 || tmp_fd == -1) {
69 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
75 if (sys_fstat(tmp_fd,&st) == -1) {
81 transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
87 /****************************************************************************
88 Common code to close a file or a directory.
89 ****************************************************************************/
91 static int close_filestruct(files_struct *fsp)
93 connection_struct *conn = fsp->conn;
96 if (fsp->fh->fd != -1) {
97 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1)
100 delete_write_cache(fsp);
103 conn->num_files_open--;
104 SAFE_FREE(fsp->wbmpx_ptr);
109 /****************************************************************************
110 If any deferred opens are waiting on this close, notify them.
111 ****************************************************************************/
113 static void notify_deferred_opens(files_struct *fsp)
115 deferred_open_entry *de_array = NULL;
116 int num_de_entries, i;
117 pid_t mypid = sys_getpid();
119 if (!lp_defer_sharing_violations()) {
123 num_de_entries = get_deferred_opens(fsp->conn, fsp->dev, fsp->inode, &de_array);
124 for (i = 0; i < num_de_entries; i++) {
125 deferred_open_entry *entry = &de_array[i];
126 if (entry->pid == mypid) {
128 * We need to notify ourself to retry the open.
129 * Do this by finding the queued SMB record, moving it
130 * to the head of the queue and changing the wait time to zero.
132 schedule_sharing_violation_open_smb_message(entry->mid);
134 send_deferred_open_retry_message(entry);
140 /****************************************************************************
143 If normal_close is 1 then this came from a normal SMBclose (or equivalent)
144 operation otherwise it came as the result of some other operation such as
145 the closing of the connection. In the latter case printing and
146 magic scripts are not run.
147 ****************************************************************************/
149 static int close_normal_file(files_struct *fsp, BOOL normal_close)
151 share_mode_entry *share_entry = NULL;
152 size_t share_entry_count = 0;
153 BOOL delete_file = False;
154 connection_struct *conn = fsp->conn;
159 remove_pending_lock_requests_by_fid(fsp);
161 if (fsp->aio_write_behind) {
163 * If we're finishing write behind on a close we can get a write
164 * error here, we must remember this.
166 int ret = wait_for_aio_completion(fsp);
172 cancel_aio_by_fsp(fsp);
176 * If we're flushing on a close we can get a write
177 * error here, we must remember this.
180 if (close_filestruct(fsp) == -1) {
185 if (fsp->print_file) {
186 print_fsp_end(fsp, normal_close);
192 * Lock the share entries, and determine if we should delete
193 * on close. If so delete whilst the lock is still in effect.
194 * This prevents race conditions with the file being created. JRA.
197 lock_share_entry_fsp(fsp);
199 share_entry_count = del_share_mode(fsp, &share_entry,
202 DEBUG(10,("close_normal_file: share_entry_count = %lu for file %s\n",
203 (unsigned long)share_entry_count, fsp->fsp_name ));
205 if (share_entry_count != 0) {
206 /* We're not the last ones -- don't delete */
210 SAFE_FREE(share_entry);
212 /* Notify any deferred opens waiting on this close. */
213 notify_deferred_opens(fsp);
216 * NT can set delete_on_close of the last open
217 * reference to a file.
220 if (normal_close && delete_file) {
221 DEBUG(5,("close_file: file %s. Delete on close was set - deleting file.\n",
223 if(SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
225 * This call can potentially fail as another smbd may have
226 * had the file open with delete on close set and deleted
227 * it when its last reference to this file went away. Hence
228 * we log this but not at debug level zero.
231 DEBUG(5,("close_file: file %s. Delete on close was set and unlink failed \
232 with error %s\n", fsp->fsp_name, strerror(errno) ));
234 process_pending_change_notify_queue((time_t)0);
237 unlock_share_entry_fsp(fsp);
240 release_file_oplock(fsp);
242 locking_close_file(fsp);
244 err = fd_close(conn, fsp);
246 /* Only save errno if fd_close failed and we don't already
247 have an errno saved from a flush call. */
248 if ((err1 != -1) && (err == -1)) {
252 /* check for magic scripts */
254 check_magic(fsp,conn);
258 * Ensure pending modtime is set after close.
261 if(fsp->pending_modtime && fsp->pending_modtime_owner) {
262 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
263 } else if (fsp->last_write_time) {
264 set_filetime(conn, fsp->fsp_name, fsp->last_write_time);
267 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
268 conn->user,fsp->fsp_name,
269 conn->num_files_open,
270 (err == -1 || err1 == -1) ? strerror(saved_errno) : ""));
273 string_free(&fsp->fsp_name);
277 if (err == -1 || err1 == -1) {
285 /****************************************************************************
286 Close a directory opened by an NT SMB call.
287 ****************************************************************************/
289 static int close_directory(files_struct *fsp, BOOL normal_close)
291 remove_pending_change_notify_requests_by_fid(fsp);
294 * NT can set delete_on_close of the last open
295 * reference to a directory also.
299 get_delete_on_close_flag(fsp->dev, fsp->inode)) {
300 BOOL ok = rmdir_internals(fsp->conn, fsp->fsp_name);
301 DEBUG(5,("close_directory: %s. Delete on close was set - deleting directory %s.\n",
302 fsp->fsp_name, ok ? "succeeded" : "failed" ));
305 * Ensure we remove any change notify requests that would
306 * now fail as the directory has been deleted.
310 remove_pending_change_notify_requests_by_filename(fsp);
312 process_pending_change_notify_queue((time_t)0);
316 * Do the code common to files and directories.
318 close_filestruct(fsp);
321 string_free(&fsp->fsp_name);
328 /****************************************************************************
329 Close a 'stat file' opened internally.
330 ****************************************************************************/
332 static int close_stat(files_struct *fsp)
335 * Do the code common to files and directories.
337 close_filestruct(fsp);
340 string_free(&fsp->fsp_name);
346 /****************************************************************************
347 Close a files_struct.
348 ****************************************************************************/
350 int close_file(files_struct *fsp, BOOL normal_close)
352 if(fsp->is_directory)
353 return close_directory(fsp, normal_close);
354 else if (fsp->is_stat)
355 return close_stat(fsp);
357 return close_normal_file(fsp, normal_close);