2 Unix SMB/CIFS implementation.
4 Copyright (C) Andrew Tridgell 1992-1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 /****************************************************************************
24 Run a file if it is a magic script.
25 ****************************************************************************/
27 static void check_magic(files_struct *fsp,connection_struct *conn)
29 if (!*lp_magicscript(SNUM(conn)))
32 DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
36 if (!(p = strrchr_m(fsp->fsp_name,'/')))
41 if (!strequal(lp_magicscript(SNUM(conn)),p))
52 pstrcpy(fname,fsp->fsp_name);
53 if (*lp_magicoutput(SNUM(conn)))
54 pstrcpy(magic_output,lp_magicoutput(SNUM(conn)));
56 slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);
59 ret = smbrun(fname,&tmp_fd);
60 DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
62 if (ret != 0 || tmp_fd == -1) {
67 outfd = open(magic_output, O_CREAT|O_EXCL|O_RDWR, 0600);
73 if (sys_fstat(tmp_fd,&st) == -1) {
79 transfer_file(tmp_fd,outfd,(SMB_OFF_T)st.st_size);
85 /****************************************************************************
86 Common code to close a file or a directory.
87 ****************************************************************************/
89 static int close_filestruct(files_struct *fsp)
91 connection_struct *conn = fsp->conn;
95 if(flush_write_cache(fsp, CLOSE_FLUSH) == -1)
98 delete_write_cache(fsp);
101 conn->num_files_open--;
102 SAFE_FREE(fsp->wbmpx_ptr);
107 /****************************************************************************
110 If normal_close is 1 then this came from a normal SMBclose (or equivalent)
111 operation otherwise it came as the result of some other operation such as
112 the closing of the connection. In the latter case printing and
113 magic scripts are not run.
114 ****************************************************************************/
116 static int close_normal_file(files_struct *fsp, BOOL normal_close)
118 share_mode_entry *share_entry = NULL;
119 size_t share_entry_count = 0;
120 BOOL delete_on_close = False;
121 connection_struct *conn = fsp->conn;
125 remove_pending_lock_requests_by_fid(fsp);
128 * If we're flushing on a close we can get a write
129 * error here, we must remember this.
132 if (close_filestruct(fsp) == -1)
135 if (fsp->print_file) {
136 print_fsp_end(fsp, normal_close);
142 * Lock the share entries, and determine if we should delete
143 * on close. If so delete whilst the lock is still in effect.
144 * This prevents race conditions with the file being created. JRA.
147 lock_share_entry_fsp(fsp);
149 if (fsp->delete_on_close) {
152 * Modify the share mode entry for all files open
153 * on this device and inode to tell other smbds we have
154 * changed the delete on close flag. The last closer will delete the file
158 NTSTATUS status =set_delete_on_close_over_all(fsp, fsp->delete_on_close);
159 if (NT_STATUS_V(status) != NT_STATUS_V(NT_STATUS_OK))
160 DEBUG(0,("close_normal_file: failed to change delete on close flag for file %s\n",
164 share_entry_count = del_share_mode(fsp, &share_entry);
166 DEBUG(10,("close_normal_file: share_entry_count = %d for file %s\n",
167 share_entry_count, fsp->fsp_name ));
170 * We delete on close if it's the last open, and the
171 * delete on close flag was set in the entry we just deleted.
174 if ((share_entry_count == 0) && share_entry &&
175 GET_DELETE_ON_CLOSE_FLAG(share_entry->share_mode) )
176 delete_on_close = True;
178 SAFE_FREE(share_entry);
181 * NT can set delete_on_close of the last open
182 * reference to a file.
185 if (normal_close && delete_on_close) {
186 DEBUG(5,("close_file: file %s. Delete on close was set - deleting file.\n",
188 if(fsp->conn->vfs_ops.unlink(conn,fsp->fsp_name) != 0) {
190 * This call can potentially fail as another smbd may have
191 * had the file open with delete on close set and deleted
192 * it when its last reference to this file went away. Hence
193 * we log this but not at debug level zero.
196 DEBUG(5,("close_file: file %s. Delete on close was set and unlink failed \
197 with error %s\n", fsp->fsp_name, strerror(errno) ));
201 unlock_share_entry_fsp(fsp);
203 if(EXCLUSIVE_OPLOCK_TYPE(fsp->oplock_type))
204 release_file_oplock(fsp);
206 locking_close_file(fsp);
208 err = fd_close(conn, fsp);
210 /* check for magic scripts */
212 check_magic(fsp,conn);
216 * Ensure pending modtime is set after close.
219 if(fsp->pending_modtime) {
220 int saved_errno = errno;
221 set_filetime(conn, fsp->fsp_name, fsp->pending_modtime);
225 DEBUG(2,("%s closed file %s (numopen=%d) %s\n",
226 conn->user,fsp->fsp_name,
227 conn->num_files_open, err ? strerror(err) : ""));
230 string_free(&fsp->fsp_name);
234 if (err == -1 || err1 == -1)
240 /****************************************************************************
241 Close a directory opened by an NT SMB call.
242 ****************************************************************************/
244 static int close_directory(files_struct *fsp, BOOL normal_close)
246 remove_pending_change_notify_requests_by_fid(fsp);
249 * NT can set delete_on_close of the last open
250 * reference to a directory also.
253 if (normal_close && fsp->directory_delete_on_close) {
254 BOOL ok = rmdir_internals(fsp->conn, fsp->fsp_name);
255 DEBUG(5,("close_directory: %s. Delete on close was set - deleting directory %s.\n",
256 fsp->fsp_name, ok ? "succeeded" : "failed" ));
259 * Ensure we remove any change notify requests that would
260 * now fail as the directory has been deleted.
264 remove_pending_change_notify_requests_by_filename(fsp);
268 * Do the code common to files and directories.
270 close_filestruct(fsp);
273 string_free(&fsp->fsp_name);
279 /****************************************************************************
280 Close a 'stat file' opened internally.
281 ****************************************************************************/
283 static int close_stat(files_struct *fsp)
286 * Do the code common to files and directories.
288 close_filestruct(fsp);
291 string_free(&fsp->fsp_name);
297 /****************************************************************************
298 Close a files_struct.
299 ****************************************************************************/
301 int close_file(files_struct *fsp, BOOL normal_close)
303 if(fsp->is_directory)
304 return close_directory(fsp, normal_close);
305 else if (fsp->is_stat)
306 return close_stat(fsp);
308 return close_normal_file(fsp, normal_close);