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"
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
{
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
)
53 struct rap_jobid_key jinfo
;
56 DEBUG(10,("pjobid_to_rap: called.\n"));
59 /* Create the in-memory tdb. */
60 rap_tdb
= tdb_open_log(NULL
, 0, TDB_INTERNAL
, (O_RDWR
|O_CREAT
), 0644);
66 fstrcpy( jinfo
.sharename
, sharename
);
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);
75 DEBUG(10,("pjobid_to_rap: jobid %u maps to RAP jobid %u\n",
76 (unsigned int)jobid
, (unsigned int)rap_jobid
));
80 /* Not found - create and store mapping. */
81 rap_jobid
= ++next_rap_jobid
;
83 rap_jobid
= ++next_rap_jobid
;
84 SSVAL(buf
,0,rap_jobid
);
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
));
95 bool rap_to_pjobid(uint16_t rap_jobid
, fstring sharename
, uint32_t *pjobid
)
100 DEBUG(10,("rap_to_pjobid called.\n"));
105 SSVAL(buf
,0,rap_jobid
);
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
);
122 DEBUG(10,("rap_to_pjobid: Failed to lookup RAP jobid %u\n",
123 (unsigned int)rap_jobid
));
124 SAFE_FREE(data
.dptr
);
128 void rap_jobid_delete(const char* sharename
, uint32_t jobid
)
132 struct rap_jobid_key jinfo
;
135 DEBUG(10,("rap_jobid_delete: called.\n"));
140 ZERO_STRUCT( jinfo
);
141 fstrcpy( jinfo
.sharename
, sharename
);
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
);
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
);
161 data
.dsize
= sizeof(rap_jobid
);
162 tdb_delete(rap_tdb
, key
);
163 tdb_delete(rap_tdb
, data
);