2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
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 extern int DEBUGLEVEL;
26 extern int32 global_oplocks_open;
29 /****************************************************************************
30 run a file if it is a magic script
31 ****************************************************************************/
32 static void check_magic(files_struct *fsp,connection_struct *conn)
34 if (!*lp_magicscript(SNUM(conn)))
37 DEBUG(5,("checking magic for %s\n",fsp->fsp_name));
41 if (!(p = strrchr(fsp->fsp_name,'/')))
46 if (!strequal(lp_magicscript(SNUM(conn)),p))
54 pstrcpy(fname,fsp->fsp_name);
56 if (*lp_magicoutput(SNUM(conn)))
57 pstrcpy(magic_output,lp_magicoutput(SNUM(conn)));
59 slprintf(magic_output,sizeof(fname)-1, "%s.out",fname);
62 ret = smbrun(fname,magic_output,False);
63 DEBUG(3,("Invoking magic command %s gave %d\n",fname,ret));
68 /****************************************************************************
69 Common code to close a file or a directory.
70 ****************************************************************************/
71 static void close_filestruct(files_struct *fsp)
73 connection_struct *conn = fsp->conn;
76 fsp->is_directory = False;
78 conn->num_files_open--;
80 free((char *)fsp->wbmpx_ptr);
81 fsp->wbmpx_ptr = NULL;
86 munmap(fsp->mmap_ptr,fsp->mmap_size);
92 /****************************************************************************
93 Close a file - possibly invalidating the read prediction.
95 If normal_close is 1 then this came from a normal SMBclose (or equivalent)
96 operation otherwise it came as the result of some other operation such as
97 the closing of the connection. In the latter case printing and
98 magic scripts are not run.
99 ****************************************************************************/
100 void close_file(files_struct *fsp, BOOL normal_close)
102 SMB_DEV_T dev = fsp->fd_ptr->dev;
103 SMB_INO_T inode = fsp->fd_ptr->inode;
105 BOOL last_reference = False;
106 BOOL delete_on_close = fsp->fd_ptr->delete_on_close;
107 connection_struct *conn = fsp->conn;
109 remove_pending_lock_requests_by_fid(fsp);
111 close_filestruct(fsp);
113 #if USE_READ_PREDICTION
114 invalidate_read_prediction(fsp->fd_ptr->fd);
117 if (lp_share_modes(SNUM(conn))) {
118 lock_share_entry(conn, dev, inode, &token);
119 del_share_mode(token, fsp);
122 if(fd_attempt_close(fsp) == 0)
123 last_reference = True;
127 if (lp_share_modes(SNUM(conn)))
128 unlock_share_entry(conn, dev, inode, token);
130 /* NT uses smbclose to start a print - weird */
131 if (normal_close && fsp->print_file)
132 print_file(conn, SNUM(conn), fsp);
134 /* check for magic scripts */
136 check_magic(fsp,conn);
140 * NT can set delete_on_close of the last open
141 * reference to a file.
144 if (normal_close && last_reference && delete_on_close) {
145 DEBUG(5,("close_file: file %s. Delete on close was set - deleting file.\n",
147 if(fsp->conn->vfs_ops.unlink(dos_to_unix(fsp->fsp_name, False)) != 0) {
149 * This call can potentially fail as another smbd may have
150 * had the file open with delete on close set and deleted
151 * it when its last reference to this file went away. Hence
152 * we log this but not at debug level zero.
155 DEBUG(5,("close_file: file %s. Delete on close was set and unlink failed \
156 with error %s\n", fsp->fsp_name, strerror(errno) ));
160 if(fsp->granted_oplock == True)
161 global_oplocks_open--;
163 fsp->sent_oplock_break = False;
165 DEBUG(2,("%s closed file %s (numopen=%d)\n",
166 conn->user,fsp->fsp_name,
167 conn->num_files_open));
170 string_free(&fsp->fsp_name);
176 /****************************************************************************
177 Close a directory opened by an NT SMB call.
178 ****************************************************************************/
180 void close_directory(files_struct *fsp)
182 remove_pending_change_notify_requests_by_fid(fsp);
185 * Do the code common to files and directories.
187 close_filestruct(fsp);
190 string_free(&fsp->fsp_name);