1c361f498e223aeaee25aad789da8a595257ec72
[debian/amanda] / tape-src / output-null.c
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1991-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author: James da Silva, Systems Design and Analysis Group
24  *                         Computer Science Department
25  *                         University of Maryland at College Park
26  */
27
28 /* NOTE: this driver is *deprecated* and should not be used.  See the Device API
29  * in device-src/ for the new implementation.
30  */
31
32 /*
33  * $Id: output-null.c,v 1.9 2006/06/02 00:56:06 paddy_s Exp $
34  *
35  * tapeio.c virtual tape interface for a null device.
36  */
37
38 #include "amanda.h"
39
40 #include "tapeio.h"
41 #include "output-null.h"
42 #include "fileheader.h"
43 #ifndef R_OK
44 #define R_OK 4
45 #define W_OK 2
46 #endif
47
48 static off_t *amount_written = NULL;
49 static size_t open_count = 0;
50
51 int
52 null_tape_open(
53     char *      filename,
54     int         flags,
55     mode_t      mask)
56 {
57     int fd;
58     off_t **amount_written_p = &amount_written;
59
60     (void)filename;     /* Quiet unused parameter warning */
61
62     if ((flags & 3) != O_RDONLY) {
63         flags &= ~3;
64         flags |= O_RDWR;
65     }
66     if ((fd = open("/dev/null", flags, mask)) >= 0) {
67         tapefd_setinfo_fake_label(fd, 1);
68         amtable_alloc((void **)amount_written_p,
69                       &open_count,
70                       SIZEOF(*amount_written),
71                       (size_t)(fd + 1),
72                       10,
73                       NULL);
74         amount_written[fd] = (off_t)0;
75     }
76     return fd;
77 }
78
79 ssize_t
80 null_tapefd_read(
81     int         fd,
82     void *      buffer,
83     size_t      count)
84 {
85     return read(fd, buffer, count);
86 }
87
88 ssize_t
89 null_tapefd_write(
90     int         fd,
91     const void *buffer,
92     size_t      count)
93 {
94     ssize_t write_count = (ssize_t)count;
95     off_t length;
96     off_t kbytes_left;
97     ssize_t r;
98
99     if (write_count <= 0) {
100         return 0;                               /* special case */
101     }
102
103     if ((length = tapefd_getinfo_length(fd)) > (off_t)0) {
104         kbytes_left = length - amount_written[fd];
105         if ((off_t)(write_count / 1024) > kbytes_left) {
106             write_count = (ssize_t)kbytes_left * 1024;
107         }
108     }
109     amount_written[fd] += (off_t)((write_count + 1023) / 1024);
110     if (write_count <= 0) {
111         errno = ENOSPC;
112         r = -1;
113     } else {
114         r = write(fd, buffer, (size_t)write_count);
115     }
116     return r;
117 }
118
119 int
120 null_tapefd_close(
121     int fd)
122 {
123     return close(fd);
124 }
125
126 void
127 null_tapefd_resetofs(
128     int fd)
129 {
130     (void)fd;   /* Quiet unused parameter warning */
131 }
132
133 int
134 null_tapefd_status(
135     int                  fd,
136     struct am_mt_status *stat)
137 {
138     (void)fd;   /* Quiet unused parameter warning */
139
140     memset((void *)stat, 0, SIZEOF(*stat));
141     stat->online_valid = 1;
142     stat->online = 1;
143     return 0;
144 }
145
146 int
147 null_tape_stat(
148      char *       filename,
149      struct stat *buf)
150 {
151     (void)filename;     /* Quiet unused parameter warning */
152
153      return stat("/dev/null", buf);
154 }
155
156 int
157 null_tape_access(
158      char *     filename,
159      int        mode)
160 {
161     (void)filename;     /* Quiet unused parameter warning */
162
163      return access("/dev/null", mode);
164 }
165
166 int
167 null_tapefd_rewind(
168     int fd)
169 {
170     amount_written[fd] = (off_t)0;
171     return 0;
172 }
173
174 int
175 null_tapefd_unload(
176     int fd)
177 {
178     amount_written[fd] = (off_t)0;
179     return 0;
180 }
181
182 int
183 null_tapefd_fsf(
184     int         fd,
185     off_t       count)
186 {
187     (void)fd;           /* Quiet unused parameter warning */
188     (void)count;        /* Quiet unused parameter warning */
189
190     return 0;
191 }
192
193 int
194 null_tapefd_weof(
195     int         fd,
196     off_t       count)
197 {
198     (void)fd;           /* Quiet unused parameter warning */
199     (void)count;        /* Quiet unused parameter warning */
200
201     return 0;
202 }
203
204 int 
205 null_tapefd_can_fork(
206     int fd)
207 {
208     (void)fd;           /* Quiet unused parameter warning */
209
210     return 0;
211 }
212