1a56faea732afa5c041b3e48bcf7b5d6a014e15e
[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.7 2003/03/06 21:43:57 martinea 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 long *amount_written = NULL;
45 static int open_count = 0;
46
47 int
48 null_tape_open(filename, flags, mask)
49     char *filename;
50     int flags;
51     int mask;
52 {
53     int fd;
54
55     if ((flags & 3) != O_RDONLY) {
56         flags &= ~3;
57         flags |= O_RDWR;
58     }
59     if ((fd = open("/dev/null", flags, mask)) >= 0) {
60         tapefd_setinfo_fake_label(fd, 1);
61         amtable_alloc((void **)&amount_written,
62                       &open_count,
63                       sizeof(*amount_written),
64                       fd + 1,
65                       10,
66                       NULL);
67         amount_written[fd] = 0;
68     }
69     return fd;
70 }
71
72 ssize_t
73 null_tapefd_read(fd, buffer, count)
74     int fd;
75     void *buffer;
76     size_t count;
77 {
78     return read(fd, buffer, count);
79 }
80
81 ssize_t
82 null_tapefd_write(fd, buffer, count)
83     int fd;
84     const void *buffer;
85     size_t count;
86 {
87     int write_count = count;
88     long length;
89     long kbytes_left;
90     int r;
91
92     if (write_count <= 0) {
93         return 0;                               /* special case */
94     }
95
96     if ((length = tapefd_getinfo_length(fd)) > 0) {
97         kbytes_left = length - amount_written[fd];
98         if (write_count / 1024 > kbytes_left) {
99             write_count = kbytes_left * 1024;
100         }
101     }
102     amount_written[fd] += (write_count + 1023) / 1024;
103     if (write_count <= 0) {
104         errno = ENOSPC;
105         r = -1;
106     } else {
107         r = write(fd, buffer, write_count);
108     }
109     return r;
110 }
111
112 int
113 null_tapefd_close(fd)
114     int fd;
115 {
116     return close(fd);
117 }
118
119 void
120 null_tapefd_resetofs(fd)
121     int fd;
122 {
123 }
124
125 int
126 null_tapefd_status(fd, stat)
127     int fd;
128     struct am_mt_status *stat;
129 {
130     memset((void *)stat, 0, sizeof(*stat));
131     stat->online_valid = 1;
132     stat->online = 1;
133     return 0;
134 }
135
136 int
137 null_tape_stat(filename, buf)
138      char *filename;
139      struct stat *buf;
140 {
141      return stat("/dev/null", buf);
142 }
143
144 int
145 null_tape_access(filename, mode)
146      char *filename;
147      int mode;
148 {
149      return access("/dev/null", mode);
150 }
151
152 int
153 null_tapefd_rewind(fd)
154     int fd;
155 {
156     amount_written[fd] = 0;
157     return 0;
158 }
159
160 int
161 null_tapefd_unload(fd)
162     int fd;
163 {
164     amount_written[fd] = 0;
165     return 0;
166 }
167
168 int
169 null_tapefd_fsf(fd, count)
170     int fd, count;
171 {
172     return 0;
173 }
174
175 int
176 null_tapefd_weof(fd, count)
177     int fd, count;
178 {
179     return 0;
180 }
181
182 int 
183 null_tapefd_can_fork(fd)
184     int fd;
185 {
186     return 0;
187 }
188