make read/write to internal pipes available externally
[Samba.git] / source / printing / printfsp.c
blobc6749226fd1319f0c6cac4bee72ff63fb206b00b
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
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.
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, see <http://www.gnu.org/licenses/>.
21 #include "includes.h"
23 /***************************************************************************
24 open a print file and setup a fsp for it. This is a wrapper around
25 print_job_start().
26 ***************************************************************************/
28 NTSTATUS print_fsp_open(connection_struct *conn, const char *fname,
29 uint16_t current_vuid, files_struct **result)
31 int jobid;
32 SMB_STRUCT_STAT sbuf;
33 files_struct *fsp;
34 fstring name;
35 NTSTATUS status;
37 status = file_new(conn, &fsp);
38 if(!NT_STATUS_IS_OK(status)) {
39 return status;
42 fstrcpy( name, "Remote Downlevel Document");
43 if (fname) {
44 const char *p = strrchr(fname, '/');
45 fstrcat(name, " ");
46 if (!p) {
47 p = fname;
49 fstrcat(name, p);
52 jobid = print_job_start(conn->server_info, SNUM(conn), name, NULL);
53 if (jobid == -1) {
54 status = map_nt_error_from_unix(errno);
55 file_free(fsp);
56 return status;
59 /* Convert to RAP id. */
60 fsp->rap_print_jobid = pjobid_to_rap(lp_const_servicename(SNUM(conn)), jobid);
61 if (fsp->rap_print_jobid == 0) {
62 /* We need to delete the entry in the tdb. */
63 pjob_delete(lp_const_servicename(SNUM(conn)), jobid);
64 file_free(fsp);
65 return NT_STATUS_ACCESS_DENIED; /* No errno around here */
68 /* setup a full fsp */
69 fsp->fh->fd = print_job_fd(lp_const_servicename(SNUM(conn)),jobid);
70 GetTimeOfDay(&fsp->open_time);
71 fsp->vuid = current_vuid;
72 fsp->fh->pos = -1;
73 fsp->can_lock = True;
74 fsp->can_read = False;
75 fsp->access_mask = FILE_GENERIC_WRITE;
76 fsp->can_write = True;
77 fsp->print_file = True;
78 fsp->modified = False;
79 fsp->oplock_type = NO_OPLOCK;
80 fsp->sent_oplock_break = NO_BREAK_SENT;
81 fsp->is_directory = False;
82 string_set(&fsp->fsp_name,print_job_fname(lp_const_servicename(SNUM(conn)),jobid));
83 fsp->wcp = NULL;
84 SMB_VFS_FSTAT(fsp, &sbuf);
85 fsp->mode = sbuf.st_mode;
86 fsp->file_id = vfs_file_id_from_sbuf(conn, &sbuf);
88 conn->num_files_open++;
90 *result = fsp;
91 return NT_STATUS_OK;
94 /****************************************************************************
95 Print a file - called on closing the file.
96 ****************************************************************************/
98 void print_fsp_end(files_struct *fsp, enum file_close_type close_type)
100 uint32 jobid;
101 fstring sharename;
103 if (fsp->fh->private_options & FILE_DELETE_ON_CLOSE) {
105 * Truncate the job. print_job_end will take
106 * care of deleting it for us. JRA.
108 sys_ftruncate(fsp->fh->fd, 0);
111 if (fsp->fsp_name) {
112 string_free(&fsp->fsp_name);
115 if (!rap_to_pjobid(fsp->rap_print_jobid, sharename, &jobid)) {
116 DEBUG(3,("print_fsp_end: Unable to convert RAP jobid %u to print jobid.\n",
117 (unsigned int)fsp->rap_print_jobid ));
118 return;
121 print_job_end(SNUM(fsp->conn),jobid, close_type);