Imported Upstream version 3.1.0
[debian/amanda] / ndmp-src / ndma_cops_labels.c
1 /*
2  * Copyright (c) 1998,1999,2000
3  *      Traakan, Inc., Los Altos, CA
4  *      All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice unmodified, this list of conditions, and the following
11  *    disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28
29 /*
30  * Project:  NDMJOB
31  * Ident:    $Id: $
32  *
33  * Description:
34  *
35  */
36
37
38 #include "ndmagents.h"
39
40
41 #ifndef NDMOS_OPTION_NO_CONTROL_AGENT
42
43
44 int
45 ndmca_op_init_labels (struct ndm_session *sess)
46 {
47         struct ndm_control_agent *ca = &sess->control_acb;
48         struct ndm_job_param *  job = &ca->job;
49         struct ndm_media_table *mtab = &job->media_tab;
50         int                     n_media = mtab->n_media;
51         struct ndmmedia *       me;
52         int                     rc, i, errors;
53
54         ca->tape_mode = NDMP9_TAPE_RDWR_MODE;
55         ca->is_label_op = 1;
56
57         if (n_media <= 0) {
58                 ndmalogf (sess, 0, 0, "No media entries in table");
59                 return -1;
60         }
61
62         errors = 0;
63         for (i = 0; i < n_media; i++) {
64                 me = &mtab->media[i];
65
66                 if (me->valid_label)
67                         continue;
68
69                 ndmalogf (sess, 0, 0, "media #%d missing a label", i+1);
70                 errors++;
71         }
72         if (errors)
73                 return -1;
74
75         rc = ndmca_op_robot_startup (sess, 1);
76         if (rc) return rc;      /* already tattled */
77
78         rc = ndmca_connect_tape_agent (sess);
79         if (rc) {
80                 ndmconn_destruct (sess->plumb.tape);
81                 return rc;      /* already tattled */
82         }
83
84         for (i = 0; i < n_media; i++) {
85                 ca->cur_media_ix = i;
86
87                 me = &mtab->media[i];
88
89                 rc = ndmca_media_load_current (sess);
90                 if (rc) {
91                         /* already tattled */
92                         continue;
93                 }
94
95                 rc = ndmca_media_write_label (sess, 'm', me->label);
96                 if (rc) {
97                         ndmalogf (sess, 0, 0, "failed label write");
98                 }
99
100                 ndmca_media_write_filemarks(sess);
101                 ndmca_media_unload_current (sess);
102         }
103
104         return rc;
105 }
106
107 int
108 ndmca_op_list_labels (struct ndm_session *sess)
109 {
110         struct ndm_control_agent *ca = &sess->control_acb;
111         struct ndm_job_param *  job = &ca->job;
112         struct ndm_media_table *mtab = &job->media_tab;
113         int                     n_media;
114         char                    labbuf[NDMMEDIA_LABEL_MAX];
115         char                    buf[200];
116         struct ndmmedia *       me;
117         int                     rc, i;
118
119         ca->tape_mode = NDMP9_TAPE_READ_MODE;
120         ca->is_label_op = 1;
121
122         rc = ndmca_op_robot_startup (sess, 0);
123         if (rc) return rc;      /* already tattled */
124
125         if (job->media_tab.n_media == 0) {
126                 if (job->have_robot) {
127                         rc = ndmca_robot_synthesize_media (sess);
128                         if (rc) return rc;      /* already tattled */
129                 } else {
130                         /*
131                          * No fixup. Should be done by now.
132                          * See ndma_job_auto_adjust()
133                          */
134                 }
135         }
136
137         if ((rc = ndmca_connect_tape_agent (sess)) != 0) {
138                 ndmconn_destruct (sess->plumb.tape);
139                 return rc;      /* already tattled */
140         }
141
142         n_media = mtab->n_media;
143
144         for (i = 0; i < n_media; i++) {
145                 ca->cur_media_ix = i;
146
147                 me = &mtab->media[i];
148
149                 rc = ndmca_media_load_current (sess);
150                 if (rc) {
151                         /* already tattled */
152                         continue;
153                 }
154
155                 rc = ndmca_media_read_label (sess, labbuf);
156                 if (rc == 'm' || rc == 'V') {
157                         strcpy (me->label, labbuf);
158                         me->valid_label = 1;
159                         ndmmedia_to_str (me, buf);
160                         ndmalogf (sess, "ME", 0, "%s", buf);
161                 } else {
162                         ndmalogf (sess, 0, 0, "failed label read");
163                 }
164                 ndmca_media_unload_current (sess);
165         }
166
167         return rc;
168 }
169
170 #endif /* !NDMOS_OPTION_NO_CONTROL_AGENT */