r24120: add a file_id_create() hook into the VFS layer
[tprouty/samba.git] / source3 / printing / printfsp.c
1 /* 
2    Unix SMB/CIFS implementation.
3    printing backend routines for smbd - using files_struct rather
4    than only snum
5    Copyright (C) Andrew Tridgell 1992-2000
6    
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 3 of the License, or
10    (at your option) any later version.
11    
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.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "includes.h"
22
23 extern struct current_user current_user;
24
25 /***************************************************************************
26 open a print file and setup a fsp for it. This is a wrapper around
27 print_job_start().
28 ***************************************************************************/
29
30 NTSTATUS print_fsp_open(connection_struct *conn, const char *fname,
31                         files_struct **result)
32 {
33         int jobid;
34         SMB_STRUCT_STAT sbuf;
35         files_struct *fsp;
36         fstring name;
37         NTSTATUS status;
38
39         status = file_new(conn, &fsp);
40         if(!NT_STATUS_IS_OK(status)) {
41                 return status;
42         }
43
44         fstrcpy( name, "Remote Downlevel Document");
45         if (fname) {
46                 const char *p = strrchr(fname, '/');
47                 fstrcat(name, " ");
48                 if (!p) {
49                         p = fname;
50                 }
51                 fstrcat(name, p);
52         }
53
54         jobid = print_job_start(&current_user, SNUM(conn), name, NULL);
55         if (jobid == -1) {
56                 status = map_nt_error_from_unix(errno);
57                 file_free(fsp);
58                 return status;
59         }
60
61         /* Convert to RAP id. */
62         fsp->rap_print_jobid = pjobid_to_rap(lp_const_servicename(SNUM(conn)), jobid);
63         if (fsp->rap_print_jobid == 0) {
64                 /* We need to delete the entry in the tdb. */
65                 pjob_delete(lp_const_servicename(SNUM(conn)), jobid);
66                 file_free(fsp);
67                 return NT_STATUS_ACCESS_DENIED; /* No errno around here */
68         }
69
70         /* setup a full fsp */
71         fsp->fh->fd = print_job_fd(lp_const_servicename(SNUM(conn)),jobid);
72         GetTimeOfDay(&fsp->open_time);
73         fsp->vuid = current_user.vuid;
74         fsp->fh->pos = -1;
75         fsp->can_lock = True;
76         fsp->can_read = False;
77         fsp->access_mask = FILE_GENERIC_WRITE;
78         fsp->can_write = True;
79         fsp->print_file = True;
80         fsp->modified = False;
81         fsp->oplock_type = NO_OPLOCK;
82         fsp->sent_oplock_break = NO_BREAK_SENT;
83         fsp->is_directory = False;
84         string_set(&fsp->fsp_name,print_job_fname(lp_const_servicename(SNUM(conn)),jobid));
85         fsp->wbmpx_ptr = NULL;      
86         fsp->wcp = NULL; 
87         SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf);
88         fsp->mode = sbuf.st_mode;
89         fsp->file_id = vfs_file_id_from_sbuf(conn, &sbuf);
90
91         conn->num_files_open++;
92
93         *result = fsp;
94         return NT_STATUS_OK;
95 }
96
97 /****************************************************************************
98  Print a file - called on closing the file.
99 ****************************************************************************/
100
101 void print_fsp_end(files_struct *fsp, enum file_close_type close_type)
102 {
103         uint32 jobid;
104         fstring sharename;
105
106         if (fsp->fh->private_options & FILE_DELETE_ON_CLOSE) {
107                 /*
108                  * Truncate the job. print_job_end will take
109                  * care of deleting it for us. JRA.
110                  */
111                 sys_ftruncate(fsp->fh->fd, 0);
112         }
113
114         if (fsp->fsp_name) {
115                 string_free(&fsp->fsp_name);
116         }
117
118         if (!rap_to_pjobid(fsp->rap_print_jobid, sharename, &jobid)) {
119                 DEBUG(3,("print_fsp_end: Unable to convert RAP jobid %u to print jobid.\n",
120                         (unsigned int)fsp->rap_print_jobid ));
121                 return;
122         }
123
124         print_job_end(SNUM(fsp->conn),jobid, close_type);
125 }