smbd: Save a few lines in smbXsrv_client_global_init()
[Samba.git] / source3 / printing / rap_jobid.c
blob7af3d4702d6ae153c974c27c14996b1a3913d39b
1 /*
2 * Maintain rap vs spoolss jobids
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 the printing backend revolves around a tdb database that stores the
20 SMB view of the print queue
22 The key for this database is a jobid - a internally generated number that
23 uniquely identifies a print job
25 reading the print queue involves two steps:
26 - possibly running lpq and updating the internal database from that
27 - reading entries from the database
29 jobids are assigned when a job starts spooling.
32 #include "rap_jobid.h"
33 #include <tdb.h>
34 #include "source3/include/util_tdb.h"
35 #include "lib/util/string_wrappers.h"
37 static TDB_CONTEXT *rap_tdb;
38 static uint16_t next_rap_jobid;
39 struct rap_jobid_key {
40 fstring sharename;
41 uint32_t jobid;
44 /***************************************************************************
45 Nightmare. LANMAN jobid's are 16 bit numbers..... We must map them to 32
46 bit RPC jobids.... JRA.
47 ***************************************************************************/
49 uint16_t pjobid_to_rap(const char* sharename, uint32_t jobid)
51 uint16_t rap_jobid;
52 TDB_DATA data, key;
53 struct rap_jobid_key jinfo;
54 uint8_t buf[2];
56 DEBUG(10,("pjobid_to_rap: called.\n"));
58 if (!rap_tdb) {
59 /* Create the in-memory tdb. */
60 rap_tdb = tdb_open_log(NULL, 0, TDB_INTERNAL, (O_RDWR|O_CREAT), 0644);
61 if (!rap_tdb)
62 return 0;
65 ZERO_STRUCT( jinfo );
66 fstrcpy( jinfo.sharename, sharename );
67 jinfo.jobid = jobid;
68 key.dptr = (uint8_t *)&jinfo;
69 key.dsize = sizeof(jinfo);
71 data = tdb_fetch(rap_tdb, key);
72 if (data.dptr && data.dsize == sizeof(uint16_t)) {
73 rap_jobid = SVAL(data.dptr, 0);
74 SAFE_FREE(data.dptr);
75 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
76 (unsigned int)jobid, (unsigned int)rap_jobid));
77 return rap_jobid;
79 SAFE_FREE(data.dptr);
80 /* Not found - create and store mapping. */
81 rap_jobid = ++next_rap_jobid;
82 if (rap_jobid == 0)
83 rap_jobid = ++next_rap_jobid;
84 SSVAL(buf,0,rap_jobid);
85 data.dptr = buf;
86 data.dsize = sizeof(rap_jobid);
87 tdb_store(rap_tdb, key, data, TDB_REPLACE);
88 tdb_store(rap_tdb, data, key, TDB_REPLACE);
90 DEBUG(10,("pjobid_to_rap: created jobid %u maps to RAP jobid %u\n",
91 (unsigned int)jobid, (unsigned int)rap_jobid));
92 return rap_jobid;
95 bool rap_to_pjobid(uint16_t rap_jobid, fstring sharename, uint32_t *pjobid)
97 TDB_DATA data, key;
98 uint8_t buf[2];
100 DEBUG(10,("rap_to_pjobid called.\n"));
102 if (!rap_tdb)
103 return False;
105 SSVAL(buf,0,rap_jobid);
106 key.dptr = buf;
107 key.dsize = sizeof(rap_jobid);
108 data = tdb_fetch(rap_tdb, key);
109 if ( data.dptr && data.dsize == sizeof(struct rap_jobid_key) )
111 struct rap_jobid_key *jinfo = (struct rap_jobid_key*)data.dptr;
112 if (sharename != NULL) {
113 fstrcpy( sharename, jinfo->sharename );
115 *pjobid = jinfo->jobid;
116 DEBUG(10,("rap_to_pjobid: jobid %u maps to RAP jobid %u\n",
117 (unsigned int)*pjobid, (unsigned int)rap_jobid));
118 SAFE_FREE(data.dptr);
119 return True;
122 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
123 (unsigned int)rap_jobid));
124 SAFE_FREE(data.dptr);
125 return False;
128 void rap_jobid_delete(const char* sharename, uint32_t jobid)
130 TDB_DATA key, data;
131 uint16_t rap_jobid;
132 struct rap_jobid_key jinfo;
133 uint8_t buf[2];
135 DEBUG(10,("rap_jobid_delete: called.\n"));
137 if (!rap_tdb)
138 return;
140 ZERO_STRUCT( jinfo );
141 fstrcpy( jinfo.sharename, sharename );
142 jinfo.jobid = jobid;
143 key.dptr = (uint8_t *)&jinfo;
144 key.dsize = sizeof(jinfo);
146 data = tdb_fetch(rap_tdb, key);
147 if (!data.dptr || (data.dsize != sizeof(uint16_t))) {
148 DEBUG(10,("rap_jobid_delete: cannot find jobid %u\n",
149 (unsigned int)jobid ));
150 SAFE_FREE(data.dptr);
151 return;
154 DEBUG(10,("rap_jobid_delete: deleting jobid %u\n",
155 (unsigned int)jobid ));
157 rap_jobid = SVAL(data.dptr, 0);
158 SAFE_FREE(data.dptr);
159 SSVAL(buf,0,rap_jobid);
160 data.dptr = buf;
161 data.dsize = sizeof(rap_jobid);
162 tdb_delete(rap_tdb, key);
163 tdb_delete(rap_tdb, data);