2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1992-2004.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 /****************************************************************************
25 Run a file if it is a magic script.
26 ****************************************************************************/
28 static void check_magic(files_struct *fsp,connection_struct *conn)
30 if (!*lp_magicscript(SNUM(conn)))
33 DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
37 if (!(p = strrchr_m(fsp->fsp_name,'/')))
42 if (!strequal(lp_magicscript(SNUM(conn)),p))
53 pstrcpy(fname,fsp->fsp_name);
54 if (*lp_magicoutput(SNUM(conn)))
55 pstrcpy(magic_output,lp_magicoutput(SNUM(conn)));
57 slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);
60 ret = smbrun(fname,&tmp_fd);
61 DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
63 if (ret != 0 || tmp_fd == -1) {
68 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
74 if (sys_fstat(tmp_fd,&st) == -1) {
80 transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
86 /****************************************************************************
87 Common code to close a file or a directory.
88 ****************************************************************************/
90 static int close_filestruct(files_struct *fsp)
92 connection_struct *conn = fsp->conn;
96 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1)
99 delete_write_cache(fsp);
102 conn->num_files_open--;
103 SAFE_FREE(fsp->wbmpx_ptr);
108 /****************************************************************************
109 If any deferred opens are waiting on this close, notify them.
110 ****************************************************************************/
112 static void notify_deferred_opens(files_struct *fsp)
114 deferred_open_entry *de_array = NULL;
115 int num_de_entries, i;
116 pid_t mypid = sys_getpid();
118 if (!lp_defer_sharing_violations()) {
122 num_de_entries = get_deferred_opens(fsp->conn, fsp->dev, fsp->inode, &de_array);
123 for (i = 0; i < num_de_entries; i++) {
124 deferred_open_entry *entry = &de_array[i];
125 if (entry->pid == mypid) {
127 * We need to notify ourself to retry the open.
128 * Do this by finding the queued SMB record, moving it
129 * to the head of the queue and changing the wait time to zero.
131 schedule_sharing_violation_open_smb_message(entry->mid);
133 send_deferred_open_retry_message(entry);
138 /****************************************************************************
141 If normal_close is 1 then this came from a normal SMBclose (or equivalent)
142 operation otherwise it came as the result of some other operation such as
143 the closing of the connection. In the latter case printing and
144 magic scripts are not run.
145 ****************************************************************************/
147 static int close_normal_file(files_struct *fsp, BOOL normal_close)
149 share_mode_entry *share_entry = NULL;
150 size_t share_entry_count = 0;
151 BOOL delete_on_close = False;
152 connection_struct *conn = fsp->conn;
157 remove_pending_lock_requests_by_fid(fsp);
159 if (fsp->aio_write_behind) {
161 * If we're finishing write behind on a close we can get a write
162 * error here, we must remember this.
164 int ret = wait_for_aio_completion(fsp);
170 cancel_aio_by_fsp(fsp);
174 * If we're flushing on a close we can get a write
175 * error here, we must remember this.
178 if (close_filestruct(fsp) == -1) {
183 if (fsp->print_file) {
184 print_fsp_end(fsp, normal_close);
190 * Lock the share entries, and determine if we should delete
191 * on close. If so delete whilst the lock is still in effect.
192 * This prevents race conditions with the file being created. JRA.
195 lock_share_entry_fsp(fsp);
197 if (fsp->delete_on_close) {
200 * Modify the share mode entry for all files open
201 * on this device and inode to tell other smbds we have
202 * changed the delete on close flag. The last closer will delete the file
206 NTSTATUS status =set_delete_on_close_over_all(fsp, fsp->delete_on_close);
207 if (NT_STATUS_V(status) != NT_STATUS_V(NT_STATUS_OK))
208 DEBUG(0,("close_normal_file: failed to change delete on close flag for file %s\n",
212 share_entry_count = del_share_mode(fsp, &share_entry);
214 DEBUG(10,("close_normal_file: share_entry_count = %lu for file %s\n",
215 (unsigned long)share_entry_count, fsp->fsp_name ));
218 * We delete on close if it's the last open, and the
219 * delete on close flag was set in the entry we just deleted.
222 if ((share_entry_count == 0) && share_entry &&
223 GET_DELETE_ON_CLOSE_FLAG(share_entry->share_mode) )
224 delete_on_close = True;
226 SAFE_FREE(share_entry);
228 /* Notify any deferred opens waiting on this close. */
229 notify_deferred_opens(fsp);
232 * NT can set delete_on_close of the last open
233 * reference to a file.
236 if (normal_close && delete_on_close) {
237 DEBUG(5,("close_file: file %s. Delete on close was set - deleting file.\n",
239 if(SMB_VFS_UNLINK(conn,fsp->fsp_name) != 0) {
241 * This call can potentially fail as another smbd may have
242 * had the file open with delete on close set and deleted
243 * it when its last reference to this file went away. Hence
244 * we log this but not at debug level zero.
247 DEBUG(5,("close_file: file %s. Delete on close was set and unlink failed \
248 with error %s\n", fsp->fsp_name, strerror(errno) ));
250 process_pending_change_notify_queue((time_t)0);
253 unlock_share_entry_fsp(fsp);
256 release_file_oplock(fsp);
258 locking_close_file(fsp);
260 err = fd_close(conn, fsp);
262 /* Only save errno if fd_close failed and we don't already
263 have an errno saved from a flush call. */
264 if ((err1 != -1) && (err == -1)) {
268 /* check for magic scripts */
270 check_magic(fsp,conn);
274 * Ensure pending modtime is set after close.
277 if(fsp->pending_modtime && fsp->pending_modtime_owner) {
278 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
279 } else if (fsp->last_write_time) {
280 set_filetime(conn, fsp->fsp_name, fsp->last_write_time);
283 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
284 conn->user,fsp->fsp_name,
285 conn->num_files_open,
286 (err == -1 || err1 == -1) ? strerror(saved_errno) : ""));
289 string_free(&fsp->fsp_name);
293 if (err == -1 || err1 == -1) {
301 /****************************************************************************
302 Close a directory opened by an NT SMB call.
303 ****************************************************************************/
305 static int close_directory(files_struct *fsp, BOOL normal_close)
307 remove_pending_change_notify_requests_by_fid(fsp);
310 * NT can set delete_on_close of the last open
311 * reference to a directory also.
314 if (normal_close && fsp->directory_delete_on_close) {
315 BOOL ok = rmdir_internals(fsp->conn, fsp->fsp_name);
316 DEBUG(5,("close_directory: %s. Delete on close was set - deleting directory %s.\n",
317 fsp->fsp_name, ok ? "succeeded" : "failed" ));
320 * Ensure we remove any change notify requests that would
321 * now fail as the directory has been deleted.
325 remove_pending_change_notify_requests_by_filename(fsp);
326 process_pending_change_notify_queue((time_t)0);
330 * Do the code common to files and directories.
332 close_filestruct(fsp);
335 string_free(&fsp->fsp_name);
341 /****************************************************************************
342 Close a 'stat file' opened internally.
343 ****************************************************************************/
345 static int close_stat(files_struct *fsp)
348 * Do the code common to files and directories.
350 close_filestruct(fsp);
353 string_free(&fsp->fsp_name);
359 /****************************************************************************
360 Close a files_struct.
361 ****************************************************************************/
363 int close_file(files_struct *fsp, BOOL normal_close)
365 if(fsp->is_directory)
366 return close_directory(fsp, normal_close);
367 else if (fsp->is_stat)
368 return close_stat(fsp);
370 return close_normal_file(fsp, normal_close);