Imported Upstream version 2.5.1
[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 /*
29  * $Id: output-null.c,v 1.9 2006/06/02 00:56:06 paddy_s Exp $
30  *
31  * tapeio.c virtual tape interface for a null device.
32  */
33
34 #include "amanda.h"
35
36 #include "tapeio.h"
37 #include "output-null.h"
38 #include "fileheader.h"
39 #ifndef R_OK
40 #define R_OK 4
41 #define W_OK 2
42 #endif
43
44 static off_t *amount_written = NULL;
45 static size_t open_count = 0;
46
47 int
48 null_tape_open(
49     char *      filename,
50     int         flags,
51     mode_t      mask)
52 {
53     int fd;
54     off_t **amount_written_p = &amount_written;
55
56     (void)filename;     /* Quiet unused parameter warning */
57
58     if ((flags & 3) != O_RDONLY) {
59         flags &= ~3;
60         flags |= O_RDWR;
61     }
62     if ((fd = open("/dev/null", flags, mask)) >= 0) {
63         tapefd_setinfo_fake_label(fd, 1);
64         amtable_alloc((void **)amount_written_p,
65                       &open_count,
66                       SIZEOF(*amount_written),
67                       (size_t)(fd + 1),
68                       10,
69                       NULL);
70         amount_written[fd] = (off_t)0;
71     }
72     return fd;
73 }
74
75 ssize_t
76 null_tapefd_read(
77     int         fd,
78     void *      buffer,
79     size_t      count)
80 {
81     return read(fd, buffer, count);
82 }
83
84 ssize_t
85 null_tapefd_write(
86     int         fd,
87     const void *buffer,
88     size_t      count)
89 {
90     ssize_t write_count = (ssize_t)count;
91     off_t length;
92     off_t kbytes_left;
93     ssize_t r;
94
95     if (write_count <= 0) {
96         return 0;                               /* special case */
97     }
98
99     if ((length = tapefd_getinfo_length(fd)) > (off_t)0) {
100         kbytes_left = length - amount_written[fd];
101         if ((off_t)(write_count / 1024) > kbytes_left) {
102             write_count = (ssize_t)kbytes_left * 1024;
103         }
104     }
105     amount_written[fd] += (off_t)((write_count + 1023) / 1024);
106     if (write_count <= 0) {
107         errno = ENOSPC;
108         r = -1;
109     } else {
110         r = write(fd, buffer, (size_t)write_count);
111     }
112     return r;
113 }
114
115 int
116 null_tapefd_close(
117     int fd)
118 {
119     return close(fd);
120 }
121
122 void
123 null_tapefd_resetofs(
124     int fd)
125 {
126     (void)fd;   /* Quiet unused parameter warning */
127 }
128
129 int
130 null_tapefd_status(
131     int                  fd,
132     struct am_mt_status *stat)
133 {
134     (void)fd;   /* Quiet unused parameter warning */
135
136     memset((void *)stat, 0, SIZEOF(*stat));
137     stat->online_valid = 1;
138     stat->online = 1;
139     return 0;
140 }
141
142 int
143 null_tape_stat(
144      char *       filename,
145      struct stat *buf)
146 {
147     (void)filename;     /* Quiet unused parameter warning */
148
149      return stat("/dev/null", buf);
150 }
151
152 int
153 null_tape_access(
154      char *     filename,
155      int        mode)
156 {
157     (void)filename;     /* Quiet unused parameter warning */
158
159      return access("/dev/null", mode);
160 }
161
162 int
163 null_tapefd_rewind(
164     int fd)
165 {
166     amount_written[fd] = (off_t)0;
167     return 0;
168 }
169
170 int
171 null_tapefd_unload(
172     int fd)
173 {
174     amount_written[fd] = (off_t)0;
175     return 0;
176 }
177
178 int
179 null_tapefd_fsf(
180     int         fd,
181     off_t       count)
182 {
183     (void)fd;           /* Quiet unused parameter warning */
184     (void)count;        /* Quiet unused parameter warning */
185
186     return 0;
187 }
188
189 int
190 null_tapefd_weof(
191     int         fd,
192     off_t       count)
193 {
194     (void)fd;           /* Quiet unused parameter warning */
195     (void)count;        /* Quiet unused parameter warning */
196
197     return 0;
198 }
199
200 int 
201 null_tapefd_can_fork(
202     int fd)
203 {
204     (void)fd;           /* Quiet unused parameter warning */
205
206     return 0;
207 }
208