r16945: Sync trunk -> 3.0 for 3.0.24 code. Still need
[vlendec/samba-autobuild/.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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24 extern struct current_user current_user;
25
26 /***************************************************************************
27 open a print file and setup a fsp for it. This is a wrapper around
28 print_job_start().
29 ***************************************************************************/
30
31 NTSTATUS print_fsp_open(connection_struct *conn, const char *fname,
32                         files_struct **result)
33 {
34         int jobid;
35         SMB_STRUCT_STAT sbuf;
36         files_struct *fsp;
37         fstring name;
38         NTSTATUS status;
39
40         status = file_new(conn, &fsp);
41         if(!NT_STATUS_IS_OK(status)) {
42                 return status;
43         }
44
45         fstrcpy( name, "Remote Downlevel Document");
46         if (fname) {
47                 const char *p = strrchr(fname, '/');
48                 fstrcat(name, " ");
49                 if (!p) {
50                         p = fname;
51                 }
52                 fstrcat(name, p);
53         }
54
55         jobid = print_job_start(&current_user, SNUM(conn), name, NULL);
56         if (jobid == -1) {
57                 status = map_nt_error_from_unix(errno);
58                 file_free(fsp);
59                 return status;
60         }
61
62         /* Convert to RAP id. */
63         fsp->rap_print_jobid = pjobid_to_rap(lp_const_servicename(SNUM(conn)), jobid);
64         if (fsp->rap_print_jobid == 0) {
65                 /* We need to delete the entry in the tdb. */
66                 pjob_delete(lp_const_servicename(SNUM(conn)), jobid);
67                 file_free(fsp);
68                 return NT_STATUS_ACCESS_DENIED; /* No errno around here */
69         }
70
71         /* setup a full fsp */
72         fsp->fh->fd = print_job_fd(lp_const_servicename(SNUM(conn)),jobid);
73         GetTimeOfDay(&fsp->open_time);
74         fsp->vuid = current_user.vuid;
75         fsp->fh->pos = -1;
76         fsp->can_lock = True;
77         fsp->can_read = False;
78         fsp->access_mask = FILE_GENERIC_WRITE;
79         fsp->can_write = True;
80         fsp->print_file = True;
81         fsp->modified = False;
82         fsp->oplock_type = NO_OPLOCK;
83         fsp->sent_oplock_break = NO_BREAK_SENT;
84         fsp->is_directory = False;
85         string_set(&fsp->fsp_name,print_job_fname(lp_const_servicename(SNUM(conn)),jobid));
86         fsp->wbmpx_ptr = NULL;      
87         fsp->wcp = NULL; 
88         SMB_VFS_FSTAT(fsp,fsp->fh->fd, &sbuf);
89         fsp->mode = sbuf.st_mode;
90         fsp->inode = sbuf.st_ino;
91         fsp->dev = sbuf.st_dev;
92
93         conn->num_files_open++;
94
95         *result = fsp;
96         return NT_STATUS_OK;
97 }
98
99 /****************************************************************************
100  Print a file - called on closing the file.
101 ****************************************************************************/
102
103 void print_fsp_end(files_struct *fsp, enum file_close_type close_type)
104 {
105         uint32 jobid;
106         fstring sharename;
107
108         if (fsp->fh->private_options & FILE_DELETE_ON_CLOSE) {
109                 /*
110                  * Truncate the job. print_job_end will take
111                  * care of deleting it for us. JRA.
112                  */
113                 sys_ftruncate(fsp->fh->fd, 0);
114         }
115
116         if (fsp->fsp_name) {
117                 string_free(&fsp->fsp_name);
118         }
119
120         if (!rap_to_pjobid(fsp->rap_print_jobid, sharename, &jobid)) {
121                 DEBUG(3,("print_fsp_end: Unable to convert RAP jobid %u to print jobid.\n",
122                         (unsigned int)fsp->rap_print_jobid ));
123                 return;
124         }
125
126         print_job_end(SNUM(fsp->conn),jobid, close_type);
127 }