virtual-backup.c
author nathan
Sat, 29 Dec 2007 15:28:06 +0100
branchtrunk
changeset 8 a306b5e43b44
parent 6 6262df5a6216
child 15 a9348bf5f6e7
permissions -rw-r--r--
release 0.7.0
     1 /* virtual-backup.c
     2 Copyright (c) 2000-2004 Craig Condit, Stefan Hülswitt.
     3 
     4 Redistribution and use in source and binary forms, with or without
     5 modification, are permitted provided that the following conditions are met: 
     6 
     7 1. Redistributions of source code must retain the above copyright notice,
     8    this list of conditions and the following disclaimer. 
     9 2. Redistributions in binary form must reproduce the above copyright notice,
    10    this list of conditions and the following disclaimer in the documentation
    11    and/or other materials provided with the distribution. 
    12 
    13 THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
    14 OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
    15 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
    16 DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR
    17 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
    18 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
    19 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
    20 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
    21 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
    22 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
    23 SUCH DAMAGE.
    24 */
    25 
    26 #define _LARGEFILE64_SOURCE
    27 #define _GNU_SOURCE
    28 
    29 #include <stdlib.h>
    30 #include <stdio.h>
    31 #include <unistd.h>
    32 #include <fcntl.h>
    33 #include <errno.h>
    34 #include <string.h>
    35 #include <sys/wait.h>
    36 #include <sys/stat.h>
    37 
    38 #include "virtual.h"
    39 #include "cdbackup.h"
    40 #include "cdrom.h"
    41 #include "misc.h"
    42 #include "debug.h"
    43 
    44 //#define DEBUGOUT
    45 
    46 extern int virtual, multi;
    47 
    48 extern int fd;
    49 extern struct virt_header *virt_header;
    50 extern int virtualMissing;
    51 extern char *real_virt_name;
    52 
    53 #ifndef DEBUGOUT
    54 static pid_t childpid;
    55 #endif
    56 static int vfd=-1;
    57 
    58 /****************************************************************************/
    59 
    60 void VnewTrack(void)
    61 {
    62   if(virtual) {
    63     long long pos;
    64     if(virtualMissing) {
    65       if((fd=open64(real_virt_name,O_RDWR|O_CREAT,S_IRUSR|S_IWUSR|S_IRGRP| S_IROTH))<0) error("Open failed (new track)");
    66       if(write(fd,virt_header,CD_FRAMESIZE)!=CD_FRAMESIZE) error("Write failed (new track)");
    67       virtualMissing=0;
    68       }
    69     if(virt_header->tracks>=MAX_VIRT_TRACKS-1) serror("Maximum number of virtual tracks reached");
    70     pos=(long long)(virt_header->start[virt_header->tracks]=virt_header->leadout)*CD_FRAMESIZE;
    71     virt_header->tracks++;
    72     if(lseek64(fd,pos,SEEK_SET)<0) error("Seek failed (new track)");
    73     }
    74   else {
    75 #ifndef DEBUGOUT
    76     int pd[2];
    77     Vclose();
    78     if(pipe(pd)<0) error("Pipe failed (new track)");
    79     if((childpid=fork())<0) error("Fork failed (new track)");
    80     if(childpid==0) { /* child */
    81       close(pd[1]);
    82       close(0);
    83       dup2(pd[0],0);
    84       start_cdrecord(); /* doesn't returns */
    85       }
    86     close(pd[0]); fd=pd[1];
    87 #else
    88     /* debug code; send data to /dev/null. */
    89     Vclose();
    90     fprintf(stderr,"DEBUG CODE: NO recording, sending data to /dev/null!\n");
    91     if((fd=open("/dev/null",O_WRONLY))<0) error("Open failed (/dev/null)");
    92 #endif
    93     cd_avail-=padsize*CD_FRAMESIZE;
    94     }
    95 }
    96 
    97 /****************************************************************************/
    98 
    99 int Vsize(void)
   100 {
   101   return virt_header->leadout;
   102 }
   103 
   104 /****************************************************************************/
   105 
   106 int VhasCont(void)
   107 {
   108   return virt_header->has_cont;
   109 }
   110 
   111 /****************************************************************************/
   112 
   113 void VprepareDump(void)
   114 {
   115   if((vfd=dup(fd))<0) error("Dup failed");
   116   Vclose();
   117 }
   118 
   119 /****************************************************************************/
   120 
   121 void VvirtRead(void *buf)
   122 {
   123   if(full_read(vfd,buf,CD_FRAMESIZE)!=CD_FRAMESIZE)
   124     serror("Unexpected EOF reading data");
   125 }
   126 
   127 /****************************************************************************/
   128 
   129 void VcloseTrack(int cont)
   130 {
   131   if(virtual) {
   132     if(lseek64(fd,0,SEEK_SET)<0) error("Seek failed (close track)");
   133     virt_header->has_cont=cont;
   134     if(write(fd,virt_header,CD_FRAMESIZE)!=CD_FRAMESIZE) error("Write failed (close track)");
   135     Vclose();
   136     }
   137   else {
   138     Vclose();
   139 #ifndef DEBUGOUT
   140     DEBUG("VcloseTrack: waiting for child termination\n");
   141     while(wait(0)!=childpid);
   142 #endif
   143     }
   144   if(vfd>=0) { close(vfd); vfd=-1; }
   145 }
   146 
   147 /****************************************************************************/
   148 
   149 void Vwrite(const void *buf)
   150 {
   151   if(write(fd,buf,CD_FRAMESIZE)!=CD_FRAMESIZE) error("Write failed");
   152   if(virtual) virt_header->leadout++;
   153   cd_avail-=CD_FRAMESIZE;
   154 }