s3-printing: remove print_parse_jobid() calls from printing.c
[ddiss/samba.git] / source3 / printing / printing.c
index 0d943a38dfe3774f91f46cf45002fc14e7811c5c..11feae28222bcf343cd1f002b39afbfd5f0b02fc 100644 (file)
@@ -380,18 +380,20 @@ done:
  unpack a pjob from a tdb buffer
 ***********************************************************************/
 
-static int unpack_pjob( uint8 *buf, int buflen, struct printjob *pjob )
+static int unpack_pjob(uint8 *buf, int buflen, struct printjob *pjob)
 {
        int     len = 0;
        int     used;
-       uint32 pjpid, pjsysjob, pjfd, pjstarttime, pjstatus;
+       uint32 pjpid, pjjobid, pjsysjob, pjfd, pjstarttime, pjstatus;
        uint32 pjsize, pjpage_count, pjspooled, pjsmbjob;
 
-       if ( !buf || !pjob )
+       if (!buf || !pjob) {
                return -1;
+       }
 
-       len += tdb_unpack(buf+len, buflen-len, "dddddddddfffff",
+       len += tdb_unpack(buf+len, buflen-len, "ddddddddddfffff",
                                &pjpid,
+                               &pjjobid,
                                &pjsysjob,
                                &pjfd,
                                &pjstarttime,
@@ -406,8 +408,9 @@ static int unpack_pjob( uint8 *buf, int buflen, struct printjob *pjob )
                                pjob->clientmachine,
                                pjob->queuename);
 
-       if ( len == -1 )
+       if (len == -1) {
                return -1;
+       }
 
         used = unpack_devicemode(NULL, buf+len, buflen-len, &pjob->devmode);
         if (used == -1) {
@@ -417,6 +420,7 @@ static int unpack_pjob( uint8 *buf, int buflen, struct printjob *pjob )
        len += used;
 
        pjob->pid = pjpid;
+       pjob->jobid = pjjobid;
        pjob->sysjob = pjsysjob;
        pjob->fd = pjfd;
        pjob->starttime = pjstarttime;
@@ -470,6 +474,7 @@ static struct printjob *print_job_find(const char *sharename, uint32 jobid)
 
        DEBUG(10,("print_job_find: returning system job %d for jobid %u.\n",
                        (int)pjob.sysjob, (unsigned int)jobid ));
+       SMB_ASSERT(pjob.jobid == jobid);
 
        return &pjob;
 }
@@ -496,15 +501,25 @@ static int unixjob_traverse_fn(TDB_CONTEXT *the_tdb, TDB_DATA key,
                return 0;
 
        if (state->sysjob == pjob->sysjob) {
-               uint32 jobid = IVAL(key.dptr,0);
-
-               state->sysjob_to_jobid_value = jobid;
+               state->sysjob_to_jobid_value = pjob->jobid;
                return 1;
        }
 
        return 0;
 }
 
+static uint32 sysjob_to_jobid_pdb(struct tdb_print_db *pdb, int sysjob)
+{
+       struct unixjob_traverse_state state;
+
+       state.sysjob = sysjob;
+       state.sysjob_to_jobid_value = (uint32)-1;
+
+       tdb_traverse(pdb->tdb, unixjob_traverse_fn, &state);
+
+       return state.sysjob_to_jobid_value;
+}
+
 /****************************************************************************
  This is a *horribly expensive call as we have to iterate through all the
  current printer tdb's. Don't do this often ! JRA.
@@ -744,8 +759,9 @@ static bool pjob_store(struct tevent_context *ev,
        do {
                len = 0;
                buflen = newlen;
-               len += tdb_pack(buf+len, buflen-len, "dddddddddfffff",
+               len += tdb_pack(buf+len, buflen-len, "ddddddddddfffff",
                                (uint32)pjob->pid,
+                               (uint32)pjob->jobid,
                                (uint32)pjob->sysjob,
                                (uint32)pjob->fd,
                                (uint32)pjob->starttime,
@@ -873,7 +889,7 @@ static void print_unix_job(struct tevent_context *ev,
        struct printjob pj, *old_pj;
 
        if (jobid == (uint32)-1)
-               jobid = q->job + UNIX_JOB_START;
+               jobid = q->sysjob + UNIX_JOB_START;
 
        /* Preserve the timestamp on an existing unix print job */
 
@@ -882,7 +898,8 @@ static void print_unix_job(struct tevent_context *ev,
        ZERO_STRUCT(pj);
 
        pj.pid = (pid_t)-1;
-       pj.sysjob = q->job;
+       pj.jobid = jobid;
+       pj.sysjob = q->sysjob;
        pj.fd = -1;
        pj.starttime = old_pj ? old_pj->starttime : q->time;
        pj.status = q->status;
@@ -928,19 +945,17 @@ static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void
        if (  key.dsize != sizeof(jobid) )
                return 0;
 
-       jobid = IVAL(key.dptr, 0);
-       if ( unpack_pjob( data.dptr, data.dsize, &pjob ) == -1 )
+       if (unpack_pjob(data.dptr, data.dsize, &pjob) == -1)
                return 0;
        talloc_free(pjob.devmode);
-
+       jobid = pjob.jobid;
 
        if (!pjob.smbjob) {
                /* remove a unix job if it isn't in the system queue any more */
-
                for (i=0;i<ts->qcount;i++) {
-                       uint32 u_jobid = (ts->queue[i].job + UNIX_JOB_START);
-                       if (jobid == u_jobid)
+                       if (ts->queue[i].sysjob == pjob.sysjob) {
                                break;
+                       }
                }
                if (i == ts->qcount) {
                        DEBUG(10,("traverse_fn_delete: pjob %u deleted due to !smbjob\n",
@@ -971,16 +986,12 @@ static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void
 
        /* this check only makes sense for jobs submitted from Windows clients */
 
-       if ( pjob.smbjob ) {
+       if (pjob.smbjob) {
                for (i=0;i<ts->qcount;i++) {
-                       uint32 curr_jobid;
-
                        if ( pjob.status == LPQ_DELETED )
                                continue;
 
-                       curr_jobid = print_parse_jobid(ts->queue[i].fs_file);
-
-                       if (jobid == curr_jobid) {
+                       if (ts->queue[i].sysjob == pjob.sysjob) {
 
                                /* try to clean up any jobs that need to be deleted */
 
@@ -1035,11 +1046,8 @@ static int traverse_fn_delete(TDB_CONTEXT *t, TDB_DATA key, TDB_DATA data, void
                return 0;
        }
 
-       /* Save the pjob attributes we will store.
-          FIXME!!! This is the only place where queue->job
-          represents the SMB jobid      --jerry */
-
-       ts->queue[i].job = jobid;
+       /* Save the pjob attributes we will store. */
+       ts->queue[i].sysjob = pjob.sysjob;
        ts->queue[i].size = pjob.size;
        ts->queue[i].page_count = pjob.page_count;
        ts->queue[i].status = pjob.status;
@@ -1190,7 +1198,7 @@ static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct
 
                qcount++;
                data.dsize += tdb_pack(NULL, 0, "ddddddff",
-                               (uint32)queue[i].job,
+                               (uint32)queue[i].sysjob,
                                (uint32)queue[i].size,
                                (uint32)queue[i].page_count,
                                (uint32)queue[i].status,
@@ -1210,7 +1218,7 @@ static void store_queue_struct(struct tdb_print_db *pdb, struct traverse_struct
                        continue;
 
                len += tdb_pack(data.dptr + len, data.dsize - len, "ddddddff",
-                               (uint32)queue[i].job,
+                               (uint32)queue[i].sysjob,
                                (uint32)queue[i].size,
                                (uint32)queue[i].page_count,
                                (uint32)queue[i].status,
@@ -1324,11 +1332,11 @@ done:
  main work for updating the lpq cache for a printer queue
 ****************************************************************************/
 
-static void print_queue_update_internal( struct tevent_context *ev,
-                                        struct messaging_context *msg_ctx,
-                                        const char *sharename,
-                                         struct printif *current_printif,
-                                         char *lpq_command, char *lprm_command )
+static void print_queue_update_internal(struct tevent_context *ev,
+                                       struct messaging_context *msg_ctx,
+                                       const char *sharename,
+                                        struct printif *current_printif,
+                                        char *lpq_command, char *lprm_command)
 {
        int i, qcount;
        print_queue_struct *queue = NULL;
@@ -1386,8 +1394,7 @@ static void print_queue_update_internal( struct tevent_context *ev,
        jcdata = get_jobs_added_data(pdb);
 
        for (i=0; i<qcount; i++) {
-               uint32 jobid = print_parse_jobid(queue[i].fs_file);
-
+               uint32 jobid = sysjob_to_jobid_pdb(pdb, queue[i].sysjob);
                if (jobid == (uint32)-1) {
                        /* assume its a unix print job */
                        print_unix_job(ev, msg_ctx,
@@ -1401,12 +1408,15 @@ static void print_queue_update_internal( struct tevent_context *ev,
                        /* err, somethings wrong. Probably smbd was restarted
                           with jobs in the queue. All we can do is treat them
                           like unix jobs. Pity. */
+                       DEBUG(1, ("queued print job %d not found in jobs list, "
+                                 "assuming unix job\n", jobid));
                        print_unix_job(ev, msg_ctx,
                                       sharename, &queue[i], jobid);
                        continue;
                }
 
-               pjob->sysjob = queue[i].job;
+               /* FIXME this is already confirmed by sysjob_to_jobid_pdb() */
+               pjob->sysjob = queue[i].sysjob;
 
                /* don't reset the status on jobs to be deleted */
 
@@ -2721,6 +2731,7 @@ WERROR print_job_start(const struct auth_session_info *server_info,
        ZERO_STRUCT(pjob);
 
        pjob.pid = getpid();
+       pjob.jobid = jobid;
        pjob.sysjob = -1;
        pjob.fd = -1;
        pjob.starttime = time(NULL);
@@ -2969,7 +2980,7 @@ static bool get_stored_queue_info(struct messaging_context *msg_ctx,
                                &qtime,
                                queue[i].fs_user,
                                queue[i].fs_file);
-               queue[i].job = qjob;
+               queue[i].sysjob = qjob;
                queue[i].size = qsize;
                queue[i].page_count = qpage_count;
                queue[i].status = qstatus;
@@ -2993,7 +3004,7 @@ static bool get_stored_queue_info(struct messaging_context *msg_ctx,
                        continue;
                }
 
-               queue[total_count].job = jobid;
+               queue[total_count].sysjob = jobid;
                queue[total_count].size = pjob->size;
                queue[total_count].page_count = pjob->page_count;
                queue[total_count].status = pjob->status;
@@ -3011,7 +3022,7 @@ static bool get_stored_queue_info(struct messaging_context *msg_ctx,
                bool found = false;
 
                for (j = 0; j < total_count; j++) {
-                       if (queue[j].job == jobid) {
+                       if (queue[j].sysjob == jobid) {
                                found = true;
                                break;
                        }
@@ -3032,7 +3043,7 @@ static bool get_stored_queue_info(struct messaging_context *msg_ctx,
                                continue;
                        }
 
-                       queue[j].job = jobid;
+                       queue[j].sysjob = jobid;
                        queue[j].size = pjob->size;
                        queue[j].page_count = pjob->page_count;
                        queue[j].status = pjob->status;
@@ -3234,11 +3245,11 @@ WERROR print_queue_purge(const struct auth_session_info *server_info,
 
        for (i=0;i<njobs;i++) {
                bool owner = is_owner(server_info, lp_const_servicename(snum),
-                                     queue[i].job);
+                                     queue[i].sysjob);
 
                if (owner || can_job_admin) {
                        print_job_delete1(server_event_context(), msg_ctx,
-                                         snum, queue[i].job);
+                                         snum, queue[i].sysjob);
                }
        }