]> git.sur5r.net Git - bacula/bacula/blob - bacula/patches/testing/faketape.patch
Attempt to fix bug #1128 InChanger flag cleared during Migration
[bacula/bacula] / bacula / patches / testing / faketape.patch
1 Index: src/baconfig.h
2 ===================================================================
3 --- src/baconfig.h      (révision 7044)
4 +++ src/baconfig.h      (copie de travail)
5 @@ -91,7 +91,6 @@
6  #define  tape_read            win32_tape_read
7  #define  tape_write           win32_tape_write
8  #define  tape_close           win32_tape_close
9 -#define  IS_TAPE(x)           S_ISCHR(x)
10  
11  #define sbrk(x)  0
12  
13 @@ -117,21 +116,11 @@
14  
15  #define  OSDependentInit()
16  
17 -#if defined(USE_FAKETAPE)
18 -#  define  tape_open            faketape_open
19 -#  define  tape_ioctl           faketape_ioctl
20 -#  define  tape_read            faketape_read
21 -#  define  tape_write           faketape_write
22 -#  define  tape_close           faketape_close
23 -#  define  IS_TAPE(x)           S_ISREG(x)
24 -#else  /* UNIX && !FAKETAPE */
25 -#  define  tape_open            ::open
26 -#  define  tape_ioctl           ::ioctl
27 -#  define  tape_read            ::read
28 -#  define  tape_write           ::write
29 -#  define  tape_close           ::close
30 -#  define  IS_TAPE(x)           S_ISCHR(x)
31 -#endif
32 +#define  tape_open            ::open
33 +#define  tape_ioctl           ::ioctl
34 +#define  tape_read            ::read
35 +#define  tape_write           ::write
36 +#define  tape_close           ::close
37  
38  #endif /* HAVE_WIN32 */
39  
40 Index: src/stored/stored_conf.c
41 ===================================================================
42 --- src/stored/stored_conf.c    (révision 7044)
43 +++ src/stored/stored_conf.c    (copie de travail)
44 @@ -213,6 +213,7 @@
45     {"dvd",           B_DVD_DEV},
46     {"fifo",          B_FIFO_DEV},
47     {"vtl",           B_VTL_DEV},
48 +   {"faketape",      B_FAKETAPE_DEV},
49     {NULL,            0}
50  };
51  
52 Index: src/stored/dev.c
53 ===================================================================
54 --- src/stored/dev.c    (révision 7044)
55 +++ src/stored/dev.c    (copie de travail)
56 @@ -123,10 +123,17 @@
57        }
58        if (S_ISDIR(statp.st_mode)) {
59           device->dev_type = B_FILE_DEV;
60 -      } else if (IS_TAPE(statp.st_mode)) { /* char device or fake tape */
61 +      } else if (S_ISCHR(statp.st_mode)) {
62           device->dev_type = B_TAPE_DEV;
63        } else if (S_ISFIFO(statp.st_mode)) {
64           device->dev_type = B_FIFO_DEV;
65 +#ifdef USE_FAKETAPE
66 +      /* must set DeviceType = Faketape 
67 +       * in normal mode, autodetection is disabled
68 +       */
69 +      } else if (S_ISREG(statp.st_mode)) { 
70 +         device->dev_type = B_FAKETAPE_DEV;
71 +#endif
72        } else if (!(device->cap_bits & CAP_REQMOUNT)) {
73           Jmsg2(jcr, M_ERROR, 0, _("%s is an unknown device type. Must be tape or directory\n"
74                 " or have RequiresMount=yes for DVD. st_mode=%x\n"),
75 @@ -162,6 +169,7 @@
76     dev->drive_index = device->drive_index;
77     dev->autoselect = device->autoselect;
78     dev->dev_type = device->dev_type;
79 +   dev->init_backend();
80     if (dev->is_tape()) { /* No parts on tapes */
81        dev->max_part_size = 0;
82     } else {
83 @@ -264,6 +272,38 @@
84     return dev;
85  }
86  
87 +void DEVICE::init_backend()
88 +{
89 +
90 +#ifdef USE_FAKETAPE
91 +   /* in this mode, force FAKETAPE driver */
92 +   dev_type = (dev_type == B_TAPE_DEV)?B_FAKETAPE_DEV:dev_type;
93 +#endif
94 +
95 +   if (is_faketape()) {
96 +      d_open = faketape_open;
97 +      d_write = faketape_write;
98 +      d_close = faketape_close;
99 +      d_ioctl = faketape_ioctl;
100 +      d_read = faketape_read;
101 +      dev_type = B_TAPE_DEV;    /* do exactly as real tape */
102 +
103 +   } else if (is_tape()) {
104 +      d_open = tape_open;
105 +      d_write = tape_write;
106 +      d_close = tape_close;
107 +      d_ioctl = tape_ioctl;
108 +      d_read = tape_read;
109 +
110 +   } else {
111 +      d_open = ::open;
112 +      d_write = ::write;
113 +      d_close = ::close;
114 +      d_ioctl = ::ioctl;
115 +      d_read = ::read;
116 +   }
117 +}
118 +
119  /*
120   * Open the device with the operating system and
121   * initialize buffer pointers.
122 @@ -285,11 +325,7 @@
123        if (openmode == omode) {
124           return m_fd;
125        } else {
126 -         if (is_tape()) {
127 -            tape_close(m_fd);
128 -         } else {
129 -            ::close(m_fd);
130 -         }
131 +         d_close(m_fd);
132           clear_opened();
133           Dmsg0(100, "Close fd for mode change.\n");
134           preserve = state & (ST_LABEL|ST_APPEND|ST_READ);
135 @@ -369,7 +405,7 @@
136  #if defined(HAVE_WIN32)
137  
138     /*   Windows Code */
139 -   if ((m_fd = tape_open(dev_name, mode)) < 0) {
140 +   if ((m_fd = d_open(dev_name, mode)) < 0) {
141        dev_errno = errno;
142     }
143  
144 @@ -379,7 +415,7 @@
145     /* If busy retry each second for max_open_wait seconds */
146     for ( ;; ) {
147        /* Try non-blocking open */
148 -      m_fd = tape_open(dev_name, mode+O_NONBLOCK);
149 +      m_fd = d_open(dev_name, mode+O_NONBLOCK);
150        if (m_fd < 0) {
151           berrno be;
152           dev_errno = errno;
153 @@ -391,10 +427,10 @@
154           mt_com.mt_op = MTREW;
155           mt_com.mt_count = 1;
156           /* rewind only if dev is a tape */
157 -         if (is_tape() && (tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0)) {
158 +         if (is_tape() && (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0)) {
159              berrno be;
160              dev_errno = errno;           /* set error status from rewind */
161 -            tape_close(m_fd);
162 +            d_close(m_fd);
163              clear_opened();
164              Dmsg2(100, "Rewind error on %s close: ERR=%s\n", print_name(),
165                    be.bstrerror(dev_errno));
166 @@ -404,8 +440,8 @@
167              }
168           } else {
169              /* Got fd and rewind worked, so we must have medium in drive */
170 -            tape_close(m_fd);
171 -            m_fd = tape_open(dev_name, mode);  /* open normally */
172 +            d_close(m_fd);
173 +            m_fd = d_open(dev_name, mode);  /* open normally */
174              if (m_fd < 0) {
175                 berrno be;
176                 dev_errno = errno;
177 @@ -689,7 +725,7 @@
178                 be.bstrerror());
179           Dmsg1(100, "open failed: %s", errmsg);
180           /* Use system close() */
181 -         ::close(m_fd);
182 +         d_close(m_fd);
183           clear_opened();
184        } else {
185           part_size = filestat.st_size;
186 @@ -733,7 +769,7 @@
187         * retrying every 5 seconds.
188         */
189        for (i=max_rewind_wait; ; i -= 5) {
190 -         if (tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
191 +         if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
192              berrno be;
193              clrerror(MTREW);
194              if (i == max_rewind_wait) {
195 @@ -747,7 +783,7 @@
196               */
197              if (first && dcr) {
198                 int open_mode = openmode;
199 -               tape_close(m_fd);
200 +               d_close(m_fd);
201                 clear_opened();
202                 open(dcr, open_mode);
203                 if (m_fd < 0) {
204 @@ -887,7 +923,7 @@
205           mt_com.mt_count = 1;
206        }
207  
208 -      if (tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
209 +      if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
210           berrno be;
211           clrerror(mt_com.mt_op);
212           Dmsg1(50, "ioctl error: %s\n", be.bstrerror());
213 @@ -1031,7 +1067,7 @@
214        stat |= BMT_TAPE;
215        Pmsg0(-20,_(" Bacula status:"));
216        Pmsg2(-20,_(" file=%d block=%d\n"), dev->file, dev->block_num);
217 -      if (tape_ioctl(dev->fd(), MTIOCGET, (char *)&mt_stat) < 0) {
218 +      if (dev->d_ioctl(dev->fd(), MTIOCGET, (char *)&mt_stat) < 0) {
219           berrno be;
220           dev->dev_errno = errno;
221           Mmsg2(dev->errmsg, _("ioctl MTIOCGET error on %s. ERR=%s.\n"),
222 @@ -1158,7 +1194,7 @@
223     dev->file_addr = 0;
224     mt_com.mt_op = MTLOAD;
225     mt_com.mt_count = 1;
226 -   if (tape_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
227 +   if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
228        berrno be;
229        dev->dev_errno = errno;
230        Mmsg2(dev->errmsg, _("ioctl MTLOAD error on %s. ERR=%s.\n"),
231 @@ -1189,7 +1225,7 @@
232     unlock_door();
233     mt_com.mt_op = MTOFFL;
234     mt_com.mt_count = 1;
235 -   if (tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
236 +   if (d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com) < 0) {
237        berrno be;
238        dev_errno = errno;
239        Mmsg2(errmsg, _("ioctl MTOFFL error on %s. ERR=%s.\n"),
240 @@ -1264,7 +1300,7 @@
241        int my_errno = 0;
242        mt_com.mt_op = MTFSF;
243        mt_com.mt_count = num;
244 -      stat = tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
245 +      stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
246        if (stat < 0) {
247           my_errno = errno;            /* save errno */
248        } else if ((os_file=get_os_tape_file()) < 0) {
249 @@ -1345,7 +1381,7 @@
250           }
251  
252           Dmsg0(100, "Doing MTFSF\n");
253 -         stat = tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
254 +         stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
255           if (stat < 0) {                 /* error => EOT */
256              berrno be;
257              set_eot();
258 @@ -1419,7 +1455,7 @@
259     file_size = 0;
260     mt_com.mt_op = MTBSF;
261     mt_com.mt_count = num;
262 -   stat = tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
263 +   stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
264     if (stat < 0) {
265        berrno be;
266        clrerror(MTBSF);
267 @@ -1459,7 +1495,7 @@
268     Dmsg1(100, "fsr %d\n", num);
269     mt_com.mt_op = MTFSR;
270     mt_com.mt_count = num;
271 -   stat = tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
272 +   stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
273     if (stat == 0) {
274        clear_eof();
275        block_num += num;
276 @@ -1518,7 +1554,7 @@
277     clear_eot();
278     mt_com.mt_op = MTBSR;
279     mt_com.mt_count = num;
280 -   stat = tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
281 +   stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
282     if (stat < 0) {
283        berrno be;
284        clrerror(MTBSR);
285 @@ -1534,7 +1570,7 @@
286     struct mtop mt_com;
287     mt_com.mt_op = MTLOCK;
288     mt_com.mt_count = 1;
289 -   tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
290 +   d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
291  #endif
292  }
293  
294 @@ -1544,7 +1580,7 @@
295     struct mtop mt_com;
296     mt_com.mt_op = MTUNLOCK;
297     mt_com.mt_count = 1;
298 -   tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
299 +   d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
300  #endif
301  }
302   
303 @@ -1656,7 +1692,7 @@
304     clear_eot();
305     mt_com.mt_op = MTWEOF;
306     mt_com.mt_count = num;
307 -   stat = tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
308 +   stat = d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
309     if (stat == 0) {
310        block_num = 0;
311        file += num;
312 @@ -1787,7 +1823,7 @@
313  /* Found on Solaris */
314  #ifdef MTIOCLRERR
315  {
316 -   tape_ioctl(m_fd, MTIOCLRERR);
317 +   d_ioctl(m_fd, MTIOCLRERR);
318     Dmsg0(200, "Did MTIOCLRERR\n");
319  }
320  #endif
321 @@ -1800,7 +1836,7 @@
322     union mterrstat mt_errstat;
323     Dmsg2(200, "Doing MTIOCERRSTAT errno=%d ERR=%s\n", dev_errno,
324        be.bstrerror(dev_errno));
325 -   tape_ioctl(m_fd, MTIOCERRSTAT, (char *)&mt_errstat);
326 +   d_ioctl(m_fd, MTIOCERRSTAT, (char *)&mt_errstat);
327  }
328  #endif
329  
330 @@ -1811,7 +1847,7 @@
331     mt_com.mt_op = MTCSE;
332     mt_com.mt_count = 1;
333     /* Clear any error condition on the tape */
334 -   tape_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
335 +   d_ioctl(m_fd, MTIOCTOP, (char *)&mt_com);
336     Dmsg0(200, "Did MTCSE\n");
337  }
338  #endif
339 @@ -1848,10 +1884,8 @@
340     case B_VTL_DEV:
341     case B_TAPE_DEV:
342        unlock_door(); 
343 -      tape_close(m_fd);
344 -      break;
345     default:
346 -      ::close(m_fd);
347 +      d_close(m_fd);
348     }
349  
350     /* Clean up device packet so it can be reused */
351 @@ -2251,11 +2285,7 @@
352  
353     get_timer_count();
354  
355 -   if (this->is_tape()) {
356 -      read_len = tape_read(m_fd, buf, len);
357 -   } else {
358 -      read_len = ::read(m_fd, buf, len);
359 -   }
360 +   read_len = d_read(m_fd, buf, len);
361  
362     last_tick = get_timer_count();
363  
364 @@ -2276,11 +2306,7 @@
365  
366     get_timer_count();
367  
368 -   if (this->is_tape()) {
369 -      write_len = tape_write(m_fd, buf, len);
370 -   } else {
371 -      write_len = ::write(m_fd, buf, len);
372 -   }
373 +   write_len = d_write(m_fd, buf, len);
374  
375     last_tick = get_timer_count();
376  
377 @@ -2306,7 +2332,7 @@
378     struct mtget mt_stat;
379  
380     if (has_cap(CAP_MTIOCGET) &&
381 -       tape_ioctl(m_fd, MTIOCGET, (char *)&mt_stat) == 0) {
382 +       d_ioctl(m_fd, MTIOCGET, (char *)&mt_stat) == 0) {
383        return mt_stat.mt_fileno;
384     }
385     return -1;
386 @@ -2431,7 +2457,7 @@
387        mt_com.mt_op = MTSETBLK;
388        mt_com.mt_count = 0;
389        Dmsg0(100, "Set block size to zero\n");
390 -      if (tape_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
391 +      if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
392           dev->clrerror(MTSETBLK);
393        }
394     }
395 @@ -2447,7 +2473,7 @@
396           mt_com.mt_count |= MT_ST_FAST_MTEOM;
397        }
398        Dmsg0(100, "MTSETDRVBUFFER\n");
399 -      if (tape_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
400 +      if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
401           dev->clrerror(MTSETDRVBUFFER);
402        }
403     }
404 @@ -2461,13 +2487,13 @@
405         dev->min_block_size == 0) {    /* variable block mode */
406        mt_com.mt_op = MTSETBSIZ;
407        mt_com.mt_count = 0;
408 -      if (tape_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
409 +      if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
410           dev->clrerror(MTSETBSIZ);
411        }
412        /* Get notified at logical end of tape */
413        mt_com.mt_op = MTEWARN;
414        mt_com.mt_count = 1;
415 -      if (tape_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
416 +      if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
417           dev->clrerror(MTEWARN);
418        }
419     }
420 @@ -2480,7 +2506,7 @@
421         dev->min_block_size == 0) {    /* variable block mode */
422        mt_com.mt_op = MTSETBSIZ;
423        mt_com.mt_count = 0;
424 -      if (tape_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
425 +      if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
426           dev->clrerror(MTSETBSIZ);
427        }
428     }
429 @@ -2491,7 +2517,7 @@
430     } else {
431        neof = 1;
432     }
433 -   if (tape_ioctl(dev->fd(), MTIOCSETEOTMODEL, (caddr_t)&neof) < 0) {
434 +   if (dev->d_ioctl(dev->fd(), MTIOCSETEOTMODEL, (caddr_t)&neof) < 0) {
435        berrno be;
436        dev->dev_errno = errno;         /* save errno */
437        Mmsg2(dev->errmsg, _("Unable to set eotmodel on device %s: ERR=%s\n"),
438 @@ -2508,7 +2534,7 @@
439         dev->min_block_size == 0) {    /* variable block mode */
440        mt_com.mt_op = MTSRSZ;
441        mt_com.mt_count = 0;
442 -      if (tape_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
443 +      if (dev->d_ioctl(dev->fd(), MTIOCTOP, (char *)&mt_com) < 0) {
444           dev->clrerror(MTSRSZ);
445        }
446     }
447 @@ -2520,7 +2546,7 @@
448  {
449     Dmsg0(100, "dev_get_os_pos\n");
450     return dev->has_cap(CAP_MTIOCGET) && 
451 -          tape_ioctl(dev->fd(), MTIOCGET, (char *)mt_stat) == 0 &&
452 +          dev->d_ioctl(dev->fd(), MTIOCGET, (char *)mt_stat) == 0 &&
453            mt_stat->mt_fileno >= 0;
454  }
455  
456 Index: src/stored/faketape.c
457 ===================================================================
458 --- src/stored/faketape.c       (révision 7044)
459 +++ src/stored/faketape.c       (copie de travail)
460 @@ -101,7 +101,7 @@
461  /* theses function will replace open/read/write/close/ioctl
462   * in bacula core
463   */
464 -int faketape_open(const char *pathname, int flags)
465 +int faketape_open(const char *pathname, int flags, ...)
466  {
467     ASSERT(pathname != NULL);
468  
469 Index: src/stored/dev.h
470 ===================================================================
471 --- src/stored/dev.h    (révision 7044)
472 +++ src/stored/dev.h    (copie de travail)
473 @@ -95,7 +95,8 @@
474     B_TAPE_DEV,
475     B_DVD_DEV,
476     B_FIFO_DEV,
477 -   B_VTL_DEV 
478 +   B_FAKETAPE_DEV,             /* change to B_TAPE_DEV after init */
479 +   B_VTL_DEV
480  };
481  
482  /* Generic status bits returned from status_dev() */
483 @@ -312,6 +313,7 @@
484     int is_fifo() const { return dev_type == B_FIFO_DEV; }
485     int is_dvd() const  { return dev_type == B_DVD_DEV; }
486     int is_vtl() const  { return dev_type == B_VTL_DEV; }
487 +   int is_faketape() const  { return dev_type == B_FAKETAPE_DEV; }
488     int is_open() const { return m_fd >= 0; }
489     int is_offline() const { return state & ST_OFFLINE; }
490     int is_labeled() const { return state & ST_LABEL; }
491 @@ -421,6 +423,14 @@
492     uint32_t get_block_num() const { return block_num; };
493     int fd() const { return m_fd; };
494  
495 +   /* low level operations */
496 +   void init_backend();
497 +   int (*d_open)(const char *pathname, int flags, ...);
498 +   int (*d_read)(int fd, void *buffer, unsigned int count);
499 +   int (*d_write)(int fd, const void *buffer, unsigned int count);
500 +   int (*d_close)(int fd);
501 +   int (*d_ioctl)(int fd, unsigned long int request, ...);
502 +
503     /* 
504      * Locking and blocking calls
505      */
506 Index: src/stored/faketape.h
507 ===================================================================
508 --- src/stored/faketape.h       (révision 7044)
509 +++ src/stored/faketape.h       (copie de travail)
510 @@ -45,7 +45,7 @@
511  /* 
512   * Theses functions will replace open/read/write
513   */
514 -int faketape_open(const char *pathname, int flags);
515 +int faketape_open(const char *pathname, int flags, ...);
516  int faketape_read(int fd, void *buffer, unsigned int count);
517  int faketape_write(int fd, const void *buffer, unsigned int count);
518  int faketape_close(int fd);