Beispiel #1
0
int bbcp_Protocol::Request_get(bbcp_FileSpec *fp)
{
   int blen;
   char *wp, buff[2048];

// Make sure there is enough space for this file
//
   if (!(bbcp_Config.Options & bbcp_NOSPCHK) && !(fs_obj->Enough(fp->Info.size,1)))
      {bbcp_Fmsg("Request_get", "Insufficient space to create file",
                       fp->targpath);
       return 28;
      }

// Construct the get command
//
   blen = snprintf(buff, sizeof(buff)-1, "get 0 %d %s %lld\n",
                   fp->seqno, fp->filereqn, fp->targetsz);

// Send off the command
//
   if (Remote->Put(buff, blen)) return 1;

// Get the response
//
   if (Remote->GetLine()        && Remote->GetToken()
   && (wp = Remote->GetToken()) && !strcmp(wp, "getok")) return 0;

// Improper response, we can't create the file
//
   bbcp_Fmsg("Request_get", "get from", Remote->NodeName(),
             "failed to create", fp->targpath);
   return 1;
}
Beispiel #2
0
int bbcp_File::verChkSum(bbcp_FileChkSum *csP)
{

// If there was a transmission error, simply return the rc
//
   if (csP->csRC) return csP->csRC;

// Check if we should verify this against a known value
//
   if (bbcp_Config.csOpts & bbcp_csVerOut)
      {int   csLen         = csP->csObj->csSize();
       char *csTxt, *csVal = csP->csObj->Final(&csTxt);
       if (*bbcp_Config.csString)
          {if (memcmp(csVal, bbcp_Config.csValue, csP->csObj->csSize()))
              {bbcp_Fmsg("Write", iofn, "target checksum", bbcp_Config.csString,
                         "does not match", csTxt);
               return -EILSEQ;
              }
          } else {
           memcpy(bbcp_Config.csValue, csVal, csLen);
           strcpy(bbcp_Config.csString, csTxt);
          }
      }

// All done
//
   DEBUG(csP->csObj->Type() <<": '" <<bbcp_Config.csString <<"' " <<iofn);
   return 0;
}
Beispiel #3
0
int bbcp_Version::Verify(char *node, char *nodever)
{
    int vchk = Check(nodever);
    char *vset;

    if (vchk < 0)         vset = (char *)"an older";
       else if (vchk > 0) vset = (char *)"a newer";
               else return 1;

    bbcp_Fmsg("Version", "Warning:",node,"is running",vset,"version of bbcp");

    if (bbcp_Config.Options & bbcp_BLAB)
       {bbcp_Fmsg("Version", node, "running version", nodever);
        bbcp_Fmsg("Version", bbcp_Config.MyHost, "running version", VData);
       }
    return 0;
};
Beispiel #4
0
int bbcp_Node::Recover(const char *who)
{
    char mbuff[256];
    sprintf(mbuff, "%d of %d data streams.", dlcount, bbcp_Config.Streams);
    bbcp_Fmsg(who, "Unable to allocate more than", mbuff);
    while(dlcount) data_link[--dlcount]->Close();
    return -1;
}
Beispiel #5
0
int bbcp_Protocol::Request_flist(long long &totsz)
{
   int retc, noteol, numfiles = 0;
   char *lp, *tfn;;
   int   tdln = strlen(tdir);
   bbcp_FileSpec *fp, *lastfp = 0, *lastdp = 0;
   const char flcmd[] = "flist\n";
   const int  flcsz   = sizeof(flcmd)-1;

// Set correct target file name
//
   if (bbcp_Config.snkSpec->Info.Otype == 'd') tfn = 0;
      else tfn = bbcp_Config.snkSpec->filename;

// Send request
//
   if (Remote->Put(flcmd, flcsz)) return -1;

// Now start getting all of the files to be copied
//
   while((lp = Remote->GetLine()) && (noteol = strcmp(lp, "eol")))
        {fp = new bbcp_FileSpec(fs_obj, Remote->NodeName());
         if (fp->Decode(lp)) {numfiles = -1; break;}

               if (fp->Compose(tdir_id, tdir, tdln, tfn)
               &&  (retc = fp->Xfr_Done()))
                  {delete fp; if (retc < 0) {numfiles = -1; break;}}
          else if (fp->Info.Otype == 'd')
                    {if (lastdp) lastdp->next = fp;
                        else bbcp_Config.srcPath = fp;
                     lastdp = fp;
                    }
/*PIPE*/  else if (fp->Info.Otype == 'f' || fp->Info.Otype == 'p')
                  {numfiles++;
                   totsz += fp->Info.size;
                   if (lastfp) lastfp->next = fp;
                   else bbcp_Config.srcSpec = fp;
                   lastfp = fp;
                  }
        }

// Flush the input queue if need be
//
   while(lp && strcmp(lp, "eol")) lp = Remote->GetLine();

// Check how we terminated
//
   if (numfiles >= 0 && noteol)
      return bbcp_Fmsg("Request_flist", "Premature end of file list from",
                       Remote->NodeName());

// All done
//
   return numfiles;
}
Beispiel #6
0
void *bbcp_ProtocolLS(void *pp)
{
   bbcp_FileSpec  *fP = bbcp_Config.srcSpec;
   char xBuff[128];
   time_t tNow, xMsg = time(0)+bbcp_Config.Progint;
   int numD = 0, numF = 0;
   int Blab = (bbcp_Config.Options & bbcp_VERBOSE) || bbcp_Config.Progint;

// Extend all directories with the files therein
//
   if (Blab) bbcp_Fmsg("Dirlist", "Indexing files to be copied...");
   while(fP)
        {if ('d' == fP->Info.Otype)
            {numD++; fP->ExtendFileSpec(bbcp_Config.srcSpec, numF);}
         fP = fP->next;
         if (bbcp_Config.Progint && (tNow = time(0)) >= xMsg)
            {sprintf(xBuff, "%d file%s in %d director%s so far...",
                     numF, (numF == 1 ? "" : "s"),
                     numD, (numD == 1 ? "y": "ies"));
             bbcp_Fmsg("Dirlist", "Found", xBuff);
             xMsg = tNow+bbcp_Config.Progint;
            }
        }

// Indicate what we found if so wanted
//
   if (Blab)
      {sprintf(xBuff, "%d file%s in %d director%s.",
               numF, (numF == 1 ? "" : "s"),
               numD, (numD == 1 ? "y": "ies"));
       bbcp_Fmsg("Source", "Copying", xBuff);
      }

// All done
//
   return 0;
}
Beispiel #7
0
int bbcp_Protocol::Process_exit()
{
    int  retc;
    char *wp;

// Get the return code (none indicates erro)
//
   if (!(wp = Remote->GetToken()))
      {bbcp_Fmsg("Process_exit", "missing return code."); retc = 22;}
      else if (bbcp_Config.a2n("return code", wp, retc, 0, 255)) retc = 22;

// Return to caller
//
   return (int)retc;
}
Beispiel #8
0
int bbcp_Protocol::Request_login(bbcp_Link *Net)
{
   const char *CtlLogin = "******";
   const char *DatLogin = "******";
   char buff[512], *id, *wp;
   int retc, blen;
   bbcp_Login_Stream loginStream(Net);
   bbcp_Node *np = loginStream.np;

// Determine wether this is a control or data path
//
   id = (Remote ? (char *)DatLogin : (char *)CtlLogin);

// Prepare the login request
//
   blen = sprintf(buff,id,bbcp_Config.SecToken,(bbcp_Net.AutoTune() ? "+" : ""),
                          bbcp_Config.Wsize, bbcp_Version.VData,
                          bbcp_Config.RWBsz);

// Send the request
//
   if ((retc = np->Put(buff, (ssize_t)blen)) < 0)
      return bbcp_Emsg( "Request_Login",-(np->LastError()),
                        "requesting", id, (char *)"path.");

// If this is a data stream, then tell caller to hold on to the net link
//
   if (Remote) {np->Detach(); return 0;}

// For a control connection, read the acknowledgement below
// nnn loginok wsz: <num> [<dsz>]
//
   if (np->GetLine())
      {if (np->GetToken()        && (wp = np->GetToken())
       && !strcmp(wp, "loginok") && (wp = np->GetToken())
       && !strcmp(wp, "wsz:")    && (wp = np->GetToken())
       &&  AdjustWS(wp, np->GetToken(), 1))
          {Remote = np;
           loginStream.np = 0;
           return 1;
          }
      }

// Invalid response
//
   return bbcp_Fmsg("Request_Login", "Invalid login ack sequence.");
}
Beispiel #9
0
int bbcp_System::Waitpid(pid_t *pvec, int *ent, int nomsg)
{
   pid_t Pdone;
   int i, retc = 0, sval;
#ifdef AIX
   union wait estat;
#else
   int estat;
#endif

// If we are waiting for only one pid, wait for that one, else wait for any
//
   while(1)
      {do {if (pvec[1]) Pdone = waitpid(     -1, (int *)&estat, 0);
              else      Pdone = waitpid(pvec[0], (int *)&estat, 0);
          }   while((Pdone < 0 && errno == EINTR) 
                 || (Pdone > 0 && WIFSTOPPED(estat)));

       if (Pdone < 0) break;

       i = 0;
       while(pvec[i] && pvec[i] != Pdone) i++;
       if (pvec[i])
          {if (WIFEXITED(estat)) retc = WEXITSTATUS(estat);
               else {retc = 255;
                     if (WIFSIGNALED(estat))
                        sval = WTERMSIG(estat);
                        if (nomsg) retc = (sval ? -sval : -255);
                           else {char pbuff[16], sbuff[16];
                                 sprintf(pbuff, "%d", Pdone);
                                 sprintf(sbuff, "%d", sval);
                                 bbcp_Fmsg("Waitpid", "Copy process", pbuff,
                                 (char *)"was killed via signal", sbuff);
                                }
                    }
           DEBUG("Process " <<Pdone <<" ended; rc=" <<retc);
           if (ent) *ent = i;
           return retc;
          }
      }

// An unknown error occured
//
   DEBUG("Waitpid ended with errno=" <<errno);
   if (ent) *ent = 0; 
   return -1;
}
Beispiel #10
0
int bbcp_Emsg(const char* sfx, int ecode, const char* txt1,
              const char* txt2, const char* txt3)
{
    int xcode;
    char ebuff[16], * etxt;

    xcode = (ecode < 0 ? -ecode : ecode);
    etxt = (char*)strerror(xcode);
    if (!strncmp(etxt, "Unknown", 7))
    {
        snprintf(ebuff, sizeof(ebuff), "Error %d", ecode);
        etxt = ebuff;
    }

    bbcp_Fmsg(sfx, etxt, txt1, txt2, txt3);

    return (ecode ? ecode : -1);
}
Beispiel #11
0
int bbcp_Protocol::getCBPort(bbcp_Node *Node)
{
   char *wp;
   int  pnum;

// The remote program should hve started, get the call back port
//
   if (wp = Node->GetLine())
      {if ((wp = Node->GetToken()) && !strcmp(wp, "200")
       &&  (wp = Node->GetToken()) && !strcmp(wp, "Port:")
       &&  (wp = Node->GetToken())
       &&  bbcp_Config.a2n("callback port", wp, pnum, 0, 65535) == 0)
          {bbcp_Config.CBport = pnum; return 0;}
      }

// Invalid response
//
   return bbcp_Fmsg("Protocol", "bbcp unexpectedly terminated on",
                     Node->NodeName());
}
Beispiel #12
0
int bbcp_Protocol::Process_get()
{
   bbcp_FileSpec *pp = 0, *fp = bbcp_Config.srcSpec;
   char *wp;
   int retc;
   int   fnum, snum;
   long long foffset = 0;
   const char getack[] = "204 getok\n";
   const int  getaln = strlen(getack);

// Get the file number and name from "get <strm> <fnum> <fname> [<offset>]"
//
   if (!(wp = Remote->GetToken()))
      {bbcp_Fmsg("Process_get", "missing stream number."); return 19;}
   if (bbcp_Config.a2n("stream number", wp, snum, 0, 255))  return 19;
   if (!(wp = Remote->GetToken()))
      {bbcp_Fmsg("Process_get", "missing file number."); return 19;}
   if (bbcp_Config.a2n("file number", wp, fnum, 0, 0x7ffffff))  return 19;
   if (!(wp = Remote->GetToken()))
      {bbcp_Fmsg("Process_get", "missing file name.");   return 19;}

// Locate the file
//
   while(fp && (fp->seqno != fnum || strcmp(wp, fp->filereqn)))
        {pp = fp; fp=fp->next;}
   if (!fp)
      {char buff[64];
       sprintf(buff, "%d", fnum);
       bbcp_Fmsg("Process_get", "file '", buff, wp, "' not found.");
       return 2;
      }

// Unchain the file specification (get allowed only once)
//
   if (pp) pp->next = fp->next;
      else bbcp_Config.srcSpec = fp->next;
   fp->next = 0;

// Get the optional offset
//
   if (wp = Remote->GetToken())
      {if (bbcp_Config.a2ll("file offset", wp, foffset, 0, -1)) return 22;
       if (foffset > fp->Info.size)
          {char buff[128];
           sprintf(buff, "(%lld>%lld)", foffset, fp->Info.size);
           bbcp_Fmsg("Process_get","Invalid offset",buff,"for",fp->pathname);
           return 29;
          }
       fp->targetsz = foffset;
      }

// Send the response
//
   if (Remote->Put(getack, getaln)) return 1;

// Send the file changing the sequence number to that requested by the caller
//
   fp->seqno = snum;
   retc = Local->SendFile(fp);
   delete fp;
   return retc;
}
Beispiel #13
0
int bbcp_Link::Buff2Net()
{
    bbcp_Buffer *outbuff;
    ssize_t wrsz, wlen = 0;
    const ssize_t hdrsz = (ssize_t)sizeof(bbcp_Header);
    int retc = 0, NotDone = 1, csLen = (csObj ? csObj->csSize() : 0);
    struct iovec iov[2] = {0, hdrsz, 0, 0};

// Establish logging options
//
   if (bbcp_Config.Options & bbcp_LOGOUT) IOB.Log(0, "NET");

// Do this until an error of eof
//
   while(NotDone)
      {
      // Obtain a buffer
      //
         if (!(outbuff = bbcp_BPool.getFullBuff()))
            {NotDone = -1; retc = ENOBUFS; break;}

      // Compose the header and see if control operation required
      //
         if (outbuff->blen <= 0)
            {if ((NotDone = Control_Out(outbuff)) < 0) {retc = 255; break;}}
            else bbcp_BPool.Encode(outbuff, BBCP_IO);

      // Check if we should generate a checksum
      //
         if (csObj && outbuff->blen) memcpy(outbuff->bHdr.cksm,
            csObj->Calc(outbuff->data,outbuff->blen), csLen);

      // Write all of the data (header & data are sequential)
      //
         iov[0].iov_base = (char *)&outbuff->bHdr;
         iov[1].iov_base =  outbuff->data; iov[1].iov_len = outbuff->blen;
         wrsz = (ssize_t)outbuff->blen + hdrsz;
         if ((wlen = IOB.Write(iov, 2)) != wrsz) break;

      // Queue buffer for re-use
      //
         if (NotDone) bbcp_BPool.putEmptyBuff(outbuff);
            else      bbcp_BPool.putFullBuff(outbuff);
         outbuff = 0;

      // Tell our buddy that it's ok to continue then do a rendezvous
      //
         if (Nudge) {Buddy->Rendezvous.Post(); if (Wait) Rendezvous.Wait();}
      }

// Check how we ended this loop
//
   if (outbuff) bbcp_BPool.putEmptyBuff(outbuff);
   if (NotDone > 0 && !wlen)
      {bbcp_BPool.Abort();
       if (wlen < 0)  retc=bbcp_Emsg("Link",-wlen,"writing data for",Lname);
          else if (wlen > 0) {bbcp_Fmsg("Link","Data lost on link", Lname);
                              retc = 100;
                             }
      } else if (NotDone) bbcp_BPool.Abort();

// All done
//
   if (Nudge) {Wait = 0; Buddy->Rendezvous.Post();}
   return (retc < 0 ? -retc : retc);
}
Beispiel #14
0
int bbcp_Link::Net2Buff()
{
    static const char *Etxt[] = {"",
                                 "Invalid header length",   // 1
                                 "Invalid buffer length",   // 2
                                 "Invalid data length",     // 3
                                 "Invalid checksum",        // 4
                                 "Invalid hdr checksum"     // 5
                                };
    enum err_type {NONE = 0, IHL = 1, IBL = 2, IDL = 3, ICS = 4, IHS = 5};
    err_type ecode = NONE;
    bbcp_Buffer *inbuff;
    ssize_t rdsz, rlen = 0;
    ssize_t hdrsz = (ssize_t)sizeof(bbcp_Header);
    int  maxrdsz  = bbcp_BPool.DataSize();
    int i, notdone = 1, csLen = (csObj ? csObj->csSize() : 0);

// Establish logging options
//
   if (bbcp_Config.Options & bbcp_LOGIN) IOB.Log("NET", 0);

// Do this until an error of eof
//
   while(notdone)
      {
      // Obtain a buffer
      //
         if (!(inbuff = bbcp_BPool.getEmptyBuff()))
            {rlen = ENOBUFS; notdone = 0; break;}

      // Read the header information into the header buffer
      //
         if ((rlen = IOB.Read((char *)&inbuff->bHdr, hdrsz)) != hdrsz)
            {if (rlen > 0) {ecode = IHL; rlen = EINVAL;} break;}

      // Decode the header and make sure it decoded correctly
      //
         if (!bbcp_BPool.Decode(inbuff)) {ecode = IHS; break;}

      // Make sure the read length does not overflow our buffer
      //
         if ((rdsz = inbuff->blen) > maxrdsz) {ecode = IBL; break;}

      // Read data into the buffer and do checksum if needed
      //
         if (rdsz)
            {if ((rlen = IOB.Read(inbuff->data, rdsz)) != rdsz)
                {if (rlen > 0) ecode = IDL; break;}
             if (csObj && memcmp(csObj->Calc(inbuff->data,inbuff->blen),
                                 inbuff->bHdr.cksm,csLen)) {ecode = ICS; break;}
            }

      // Check if this is a control operation or data operation
      //
         if (inbuff->bHdr.cmnd == (char)BBCP_IO)
            bbcp_BPool.putFullBuff(inbuff);
            else if ((notdone = Control_In(inbuff)) <= 0) break;

      // Tell our buddy that it's ok to continue then do a rendezvous
      //
         if (Nudge) {Buddy->Rendezvous.Post(); if (Wait) Rendezvous.Wait();}
      }

// If we ended the loop with an error, abort the buffer pool to force all
// threads dependent on the queue to abnormally terminate. Otherwise, post
// the buddy thread twice since that is the most that it may need to read.
//

   if (Nudge) {Wait = 0; i = bbcp_Config.Streams;
               do {Buddy->Rendezvous.Post();} while(i--);
              }
   if (notdone)
      {bbcp_BPool.Abort();
      if (ecode != NONE)
         {bbcp_Fmsg("Net2Buff", Etxt[(int)ecode], "from", Lname);
          return 128;
         }
      if (rlen >=0) return EPIPE;
      bbcp_Emsg("Net2Buff", rlen, "reading data from", Lname);
      return -rlen;
      }
   return 0;
}
Beispiel #15
0
int bbcp_Node::RecvFile(bbcp_FileSpec *fp, bbcp_Node *Remote)
{
   static const int wOnly = S_IWUSR;

   const char *Args = 0, *Act = "opening", *Path = fp->targpath;
   long tretc = 0;
   int i, oflag, retc, Mode = wOnly, progtid = 0;
   long long startoff = 0;
   pid_t Child[2] = {0,0};
   bbcp_File *outFile, *seqFile = 0;
   bbcp_ZCX *cxp = 0;
   pthread_t tid, link_tid[BBCP_MAXSTREAMS+4];
   bbcp_Timer Elapsed_Timer;
   bbcp_ProgMon *pmp = 0;
   float CRatio;

// Perform Force or Append processing
//
        if (bbcp_Config.Options & bbcp_XPIPE)
           {oflag = O_WRONLY;
            Path = bbcp_Config.snkSpec->pathname;
            Args = bbcp_Config.snkSpec->fileargs;
            if (bbcp_Config.snkSpec->Info.Otype != 'p') Act = "running";
               else {Mode |= S_IFIFO;
                     if (Args)
                        {bbcp_Fmsg("RecvFile",
                                   "Spaces disallowed in named output pipe",Path);
                         return -EINVAL;
                        }
                    }
           }
   else if (bbcp_Config.Options & bbcp_FORCE)
           {if (!(bbcp_Config.Options & bbcp_NOUNLINK))
               fp->FSys()->RM(Path);
            oflag = O_WRONLY | O_CREAT | O_TRUNC;
           }
   else if (bbcp_Config.Options & bbcp_APPEND)
           {if (retc = fp->WriteSigFile()) return retc;
            if (startoff = fp->targetsz) oflag = O_WRONLY;
               else oflag = O_CREAT | O_WRONLY;
           }
   else    oflag = O_WRONLY | O_CREAT | O_EXCL;

// Establish mode, we normally make the file write-only
//
   if ( bbcp_Config.Options &  bbcp_RTCSNK
   && !(bbcp_Config.Options & (bbcp_RTCHIDE|bbcp_XPIPE)))
      Mode = bbcp_Config.Mode|S_IWUSR|S_ISUID;

// Tell the user what we are bout to do
//
   if (bbcp_Config.Options & bbcp_BLAB | bbcp_Config.Progint)
      if (bbcp_Config.Options & bbcp_APPEND) 
         {char buff[32];
          sprintf(buff, "%lld", startoff);
          bbcp_Fmsg("RecvFile","Appending to",Path,"at offset",buff);
         }
         else bbcp_Fmsg("RecvFile", "Creating", Path);
      else DEBUG("Receiving " <<fp->pathname <<" as " <<Path <<" offset=" <<startoff);

// Receive the file in a sub-process so that we don't muck with this one
//
   if ((Child[0] = bbcp_OS.Fork()) < 0)
      return bbcp_Emsg("RecvFile", errno, "forking to create", Path);
   if (Child[0]) 
      {char buff[128];
       Parent_Monitor.Start(0,Remote);
       DEBUG("Waiting for child " <<Child[0] <<" to finish");
       retc = bbcp_OS.Waitpid(Child);
       Parent_Monitor.Stop();
       if (bbcp_Config.Options & bbcp_BLAB)
          write(STDERR_FILENO, buff, Usage("Target", buff, sizeof(buff)));
       return retc;
      }

/*******************************************************************************
   (net)->data_link[i]->BPool->CStage[1]->CStage[0]->CPool->outFile->(file)
*******************************************************************************/

// Set Concurrency
//
   bbcp_Thread_MT(bbcp_Config.MTLevel);

// Request direct I/O if so wanted
//
   if (bbcp_Config.Options & bbcp_ODIO) {fp->FSys()->DirectIO(1);
       DEBUG("Direct output requested.");}

// Open the file and set the starting offset
//
   Elapsed_Timer.Start();
   if (!(outFile = fp->FSys()->Open(Path, oflag, Mode, Args)))
      return bbcp_Emsg("RecvFile", errno, Act, Path);
   if (startoff && ((retc = outFile->Seek(startoff)) < 0))
      return bbcp_Emsg("RecvFile",retc,"setting write offset for",Path);
   outFile->setSize(fp->Info.size);

// If compression is wanted, set up the compression objects
//
   if (bbcp_Config.Options & bbcp_COMPRESS 
   && !(cxp = setup_CX(0, outFile->ioFD()))) return -ECANCELED;

// Start a thread for each data link we have
//
   for (i = 0; i < dlcount; i++)
       {if ((retc = bbcp_Thread_Start(bbcp_Net2Buff, 
                                (void *)data_link[i], &tid))<0)
           {bbcp_Emsg("RecvFile",retc,"starting net thread for",Path);
            _exit(100);
           }
        link_tid[i] = tid;
        DEBUG("Thread " <<tid <<" assigned to stream " <<i);
       }

// If we are compressing start the sequence thread now
//
   if (bbcp_Config.Options & bbcp_COMPRESS)
      {seqFile = new bbcp_File(Path, 0, 0);
       if ((retc = bbcp_Thread_Start(bbcp_doWrite, (void *)seqFile, &tid))<0)
          {bbcp_Emsg("RecvFile",retc,"starting disk thread for",Path);
           _exit(100);
          }
       link_tid[dlcount++] = tid;
       DEBUG("Thread " <<tid <<" assigned to sequencer as stream " <<i);
      }

// Start the parent process monitor. It is stopped at exit.
//
   Parent_Monitor.Start();

// If a periodic progress message is wanted, start a progress thread
//
   if (bbcp_Config.Progint) 
      {pmp = new bbcp_ProgMon();
       pmp->Start(outFile, cxp, bbcp_Config.Progint, fp->Info.size-startoff);
      }

// Write the whole file
//
   if (bbcp_Config.Options & bbcp_COMPRESS)
           retc = outFile->Write_All(bbcp_APool, 1);
      else retc = outFile->Write_All(bbcp_BPool, bbcp_Config.Streams);
   DEBUG("File write ended; rc=" <<retc);

// Wait for the expansion thread to end
//
   if (bbcp_Config.Options & bbcp_COMPRESS)
      {if (tretc = (long)bbcp_Thread_Wait(cxp->TID)) retc = 128;
       DEBUG("File expansion ended; rc=" <<tretc);
      }

// Kill the progress monitor
//
   if (pmp) 
      {DEBUG("Deleting progress monitor");
       delete pmp;
      }

// Make sure each thread has terminated normally
//
   for (i = 0; i < dlcount; i++)
       {if (tretc = (long)bbcp_Thread_Wait(link_tid[i])) retc = 128;
        DEBUG("Thread " <<link_tid[i] <<" stream " <<i <<" ended; rc=" <<tretc);
       }

// Make sure that all of the bytes were transfered
//
   if (!retc && strncmp(Path, "/dev/null/", 10))
      {bbcp_FileInfo Info;
       if ((retc = fp->FSys()->Stat(Path, &Info)) < 0)
          {retc = -retc;
           bbcp_Emsg("RecvFile", retc, "finding", Path);
          }
          else if (Info.size != fp->Info.size && Info.mode
               &&  !(bbcp_Config.Options & bbcp_NOFSZCHK))
                  {const char *What = (Info.size < fp->Info.size
                                    ?  "Not all" : "Too much");
                   retc = 29;
                   bbcp_Fmsg("RecvFile",What,"data was transfered for",Path);
                   DEBUG("src size=" <<fp->Info.size <<" snk size=" <<Info.size);
                  }
      } DEBUG("Outfile " <<Path <<" closed");

// Report detailed I/O stats, if so wanted
//
   Elapsed_Timer.Stop();
   if (!retc && bbcp_Config.Options & bbcp_VERBOSE)
      {double ttime;
       Elapsed_Timer.Report(ttime);
       Report(ttime, fp, outFile, cxp);
      }

// All done
//
   Parent_Monitor.Stop();
                delete outFile;
   if (cxp)     delete(cxp);
   if (seqFile) delete(seqFile);
   retc = fp->Finalize(retc);
   close(1); close(2);
   DEBUG("Process " <<getpid() <<" exiting with rc=" <<retc);
   exit(retc);
   return(retc);  // some compilers insist on a return in int functions
}
Beispiel #16
0
int bbcp_File::Passthru(bbcp_BuffPool *iBP, bbcp_BuffPool *oBP, 
                        bbcp_FileChkSum *csP, int nstrms)
{
    bbcp_Buffer *outbuff;
    bbcp_ChkSum *csObj;
    long long Offset = nextoffset;
    int csLen, csVer, numadd, maxbufs, maxadds = nstrms;
    int rc = 0, unordered = !(bbcp_Config.Options & bbcp_ORDER);

// Determine if we will be piggy-backing checksumming here
//
   if (csP && (csObj = csP->csObj))
      {csVer = csP->csVer; csLen = csObj->csSize();}
      else csVer = csLen = 0;

// Record the maximum number of buffers we have here
//
   maxbufs = iBP->BuffCount();
   if (!(numadd = nstrms)) numadd = 1;

// Read all of the data until eof (note that we are single threaded)
//
   while(nstrms)
      {
      // Obtain a full buffer
      //
         if (!(outbuff = iBP->getFullBuff())) break;

     // Check if this is an eof marker
     //
     //  cerr <<nstrms <<" Passt " <<outbuff->blen <<'@' <<outbuff->boff <<endl;
        if (!(outbuff->blen))
           {iBP->putEmptyBuff(outbuff); nstrms--; continue;}

      // Do an unordered write if allowed
      //
         if (unordered) {oBP->putFullBuff(outbuff); continue;}

      // Check if this buffer is in the correct sequence
      //
         if (outbuff->boff != Offset)
            {if (outbuff->boff < 0) {rc = -ESPIPE; break;}
             outbuff->next = nextbuff;
             nextbuff = outbuff;
             bufreorders++;
             if (++curq > maxreorders) 
                {maxreorders = curq;
                 DEBUG("Buff disorder " <<curq <<" rcvd " <<outbuff->boff <<" want " <<Offset);
                }
             if (curq >= maxbufs)
                {if (!(--maxadds)) {rc = -ENOBUFS; break;}
                 DEBUG("Too few buffs; adding " <<numadd <<" more.");
                 bbcp_BPool.Allocate(numadd);
                 maxbufs += numadd;
                }
             continue;
            }

      // Pass through any queued buffers
      //
      do {Offset += outbuff->blen;
          if (csObj)
             {csObj->Update(outbuff->data, outbuff->blen);
              if (csVer && memcmp(outbuff->bHdr.cksm, csObj->csCurr(), csLen))
                 {char buff[32];
                  sprintf(buff, "%lld", outbuff->boff);
                  bbcp_Fmsg("Write",iofn, "xfr checksum error at offset",buff);
                  rc = -EILSEQ; nstrms = 0; break;
                 }
             }
          oBP->putFullBuff(outbuff);
         } while(nextbuff && (outbuff = getBuffer(Offset)));
      }

// Check if we should print an error here
//
   if (rc && rc != -EILSEQ) bbcp_Emsg("Write",rc, "unable to write", iofn);

// Queue an empty buffer indicating eof or abort the stream
//
   if (!rc && (outbuff = iBP->getEmptyBuff()))
      {outbuff->blen = 0; outbuff->boff = Offset;
       oBP->putFullBuff(outbuff);
      } else {
       if (!rc) rc = -ENOBUFS;
       oBP->Abort(); iBP->Abort();
      }

// All done
//
   return rc;
}
Beispiel #17
0
int bbcp_File::Read_All(bbcp_BuffPool &inPool, int Vn)
{
    bbcp_FileChkSum *csP = 0;
    bbcp_BuffPool *outPool;
    bbcp_Buffer   *bP;
    pthread_t tid;
    int rc = 0;

// Get the size of the file
//
   if ((bytesLeft = FSp->getSize(IOB->FD())) < 0)
      {bbcp_Emsg("Read", static_cast<int>(-bytesLeft), "stat", iofn);
       inPool.Abort(); return 200;
      }

// Adjust bytes left based on where we will be reading from
//
   bytesLeft -= nextoffset;
   if (bytesLeft < 0)
      {bbcp_Emsg("Read", ESPIPE, "stat", iofn);
       inPool.Abort(); return 200;
      }

// If this is a real-time copy operation, start the rtcopy object
//
   if (rtCopy && !bbcp_RTCopy.Start(FSp, iofn, IOB->FD()))
      {inPool.Abort(); return 200;
      }

// Set up checksumming. We would prefer to do this in the calling thread but
// this is easier. One day we will generalize buffer piping.
//
   if (bbcp_Config.csOpts & bbcp_csVerIn)
      {csP = new bbcp_FileChkSum(&inPool, this, bbcp_Config.csType,
                                 bbcp_Config.csOpts & bbcp_csVerIO);
       if ((rc = bbcp_Thread_Start(bbcp_FileCSX, (void *)csP, &tid)) < 0)
           {bbcp_Emsg("File", rc, "starting file checksum thread.");
            delete csP;
            if (rtCopy) bbcp_RTCopy.Stop();
            inPool.Abort(); return 201;
           }
       outPool =  &csP->csPool;
      } else outPool = &inPool;

// Establish logging options
//
   if (bbcp_Config.Options & bbcp_LOGRD) IOB->Log("DISK", 0);

// Determine what kind of reading we will do here and do it
//
// cerr <<"BLOCKSIZE " <<blockSize <<endl;
   if (blockSize ) rc = Read_Direct(&inPool, outPool);
      else rc=(Vn > 1 ? Read_Vector(&inPool, outPool, Vn)
                      : Read_Normal(&inPool, outPool));

// Delete the real-time copy object if we have one to kill possible thread
//
   if (rtCopy) bbcp_RTCopy.Stop();

// Check if we ended because with an error
//
   IOB->Close();
   if (rc && rc != -ENOBUFS) bbcp_Emsg("Read", -rc, "reading", iofn);

// Prepare an empty buffer to shutdown the buffer pipeline. The offet indicates
// how much data should have been sent and received. A negative offset implies
// that we encountered an error.
//
   if (!(bP = inPool.getEmptyBuff())) return 255;
   bP->blen = 0;
   bP->boff = (rc ? -1 : nextoffset);

// Verify check sum if so wanted. Note that is link-level check summing is in
//
   if (csP)
      {csP->csPool.putFullBuff(bP);
       bbcp_Thread_Wait(tid);
       if (!rc && *bbcp_Config.csString)
          {char *csTxt, *csVal = csP->csObj->Final(&csTxt);
           if (memcmp(csVal, bbcp_Config.csValue, csP->csObj->csSize()))
              {bbcp_Fmsg("Read", iofn, "source checksum", bbcp_Config.csString,
                         "does not match", csTxt);
               rc = EILSEQ;
              } else {DEBUG(csP->csObj->Type() <<": " <<csTxt <<' ' <<iofn);}
          }
       delete csP;
      } else inPool.putFullBuff(bP);

// All done
//
   DEBUG("EOF offset=" <<nextoffset <<" rc=" <<rc <<" fn=" <<iofn);
   return rc;
}
Beispiel #18
0
int bbcp_Protocol::Request(bbcp_Node *Node)
{
   long long totsz=0;
   int  retc, numfiles, texists;
   int  outDir = (bbcp_Config.Options & bbcp_OUTDIR) != 0;
   bbcp_FileSpec *fp;
   char buff[1024];

// Establish all connections
//
   if (Node->Start(this, !(bbcp_Config.Options & bbcp_CON2SRC))
   ||  Node->getBuffers(1)) return 2;
   Local = Node;

// Establish the target directory
//
   if ((texists = !bbcp_Config.snkSpec->Stat(0))
   &&  bbcp_Config.snkSpec->Info.Otype == 'd')
       tdir = bbcp_Config.snkSpec->pathname;
      else {int plen;
            if (plen = bbcp_Config.snkSpec->filename -
                       bbcp_Config.snkSpec->pathname)
               strncpy(buff, bbcp_Config.snkSpec->pathname, plen-1);
               else {buff[0] = '.'; plen = 2;}
            tdir = buff; buff[plen-1] = '\0';
           }

// Generate a target directory ID. This will also uncover a missing directory.
//
   fs_obj = bbcp_Config.snkSpec->FSys();
   if (texists &&  bbcp_Config.snkSpec->Info.Otype == 'd')
      tdir_id = bbcp_Config.snkSpec->Info.fileid;
      else {bbcp_FileInfo Tinfo;
            if (!fs_obj || (!(retc = fs_obj->Stat(tdir, &Tinfo))
            && Tinfo.Otype != 'd') && outDir) retc = ENOTDIR;
            if (retc) {bbcp_Fmsg("Request","Target directory",
                                 bbcp_Config.snkSpec->pathname,"not found");
                       return Request_exit(2);
                      }
            tdir_id = Tinfo.fileid;
           }

// The first step is to perform an flist to get the list of files
//
   if ((numfiles = Request_flist(totsz)) <= 0) 
      return Request_exit((numfiles  < 0 ? 22 : 0));

// If we have a number files, the target had better be a directory
//
   if (numfiles > 1 || outDir)
      {if (!texists)
          {bbcp_Fmsg("Request", "Target directory",
                     bbcp_Config.snkSpec->pathname, "not found.");
           return Request_exit(2);
          }
       if (bbcp_Config.snkSpec->Info.Otype != 'd')
          {bbcp_Fmsg("Request", "Target", bbcp_Config.snkSpec->pathname,
                     "is not a directory.");
           return Request_exit(20);
          }
       bbcp_Config.Options |= bbcp_OUTDIR;
      }

// Make sure we have enough space in the filesystem
//
   DEBUG("Preparing to copy " <<numfiles <<" file(s); bytes=" <<totsz);
   if (!(bbcp_Config.Options & bbcp_NOSPCHK) && !fs_obj->Enough(totsz, numfiles))
      {bbcp_Fmsg("Sink", "Insufficient space to copy all the files from",
                               Remote->NodeName());
       return Request_exit(28);
      }

// Create all of the required directories
//
   retc = 0;
   fp = bbcp_Config.srcPath;
   while(fp && !(retc = fp->Create_Path()))  fp = fp->next;
   if (retc) return Request_exit(retc);

// Get each source file
//
   fp = bbcp_Config.srcSpec;
   while(fp && !(retc=Request_get(fp)) && !(retc=Local->RecvFile(fp,Remote)))
        {if (bbcp_Config.Options & bbcp_APPEND) totsz -= fp->targetsz;
         fp = fp->next;
        }

// Now determine if we need to reset the stat info on any paths we created
//
   if ((fp = bbcp_Config.srcPath))
      {if (bbcp_Config.Options & bbcp_PCOPY)
          {while(fp && fp->setStat()) fp = fp->next;}
          else if (bbcp_Config.ModeD != bbcp_Config.ModeDC)
                  {while(fp && fp->setMode(bbcp_Config.ModeD)) fp = fp->next;}
      }

// Report back how many files and bytes were received
//
   cout <<"200 End: " <<numfiles <<' ' <<totsz <<endl;

// All done
//
   return Request_exit(retc);
}
Beispiel #19
0
// The following class is here to allow graceful error exits
//
int bbcp_Protocol::Process_login(bbcp_Link *Net)
{
   char buff[256], *tp, *bp, *vp, *wp, *id;
   int retc, blen, respWS;
   bbcp_Login_Stream loginStream(Net);
   bbcp_Node *np = loginStream.np;

// Get the first line of the login stream
//
   if (!(np->GetLine()))
      {if (retc = np->LastError())
          return bbcp_Emsg("Process_Login", retc, "processing login from",
                                 Net->LinkName());
       return bbcp_Fmsg("Process_Login", "Bad login from", Net->LinkName());
     }

// Determine the id we want (the control stream must login first)
//
   id = (Remote ? (char *)"data" : (char *)"ctlr");

// Process the login request: login <id> <password>
//
   if (!(wp = np->GetToken()) || strcmp(wp, "login")
   ||  !(wp = np->GetToken()) || strcmp(wp, id)
   ||  !(wp = np->GetToken()) || strcmp(wp, bbcp_Config.SecToken))
      return bbcp_Fmsg("Process_Login", "Invalid login from", Net->LinkName());

// We are all done if this is not a control stream
//
   if (*id != 'c') {np->Detach(); return 0;}

// Pickup all parameters.
//
   bp = vp = wp = 0;
   while((tp = np->GetToken()))
        {     if (!strcmp(tp, "wsz:"))
                 {if (!(wp = np->GetToken()))
                     return bbcp_Fmsg("Login", "Window size is missing.");
                 }
         else if (!strcmp(tp, "ver:"))
                 {if (!(vp = np->GetToken()))
                     return bbcp_Fmsg("Login", "Version is missing.");
                 }
         else if (!strcmp(tp, "bsz:"))
                 {if (!(bp = np->GetToken()))
                     return bbcp_Fmsg("Login", "Buffer size is missing.");
                 }
        }

// Verify that our version is the same on the other side
//
   if (vp) bbcp_Version.Verify(Net->LinkName(), vp);
      else bbcp_Version.Verify(Net->LinkName(),(char *)"02.01.12.00.0");

// We can now do a window/buffer adjustment
//
   if (!wp) respWS = bbcp_Config.Wsize;
      else if (!(respWS = AdjustWS(wp, bp, 0))) return -1;

// Respond to this login request (control only gets a response)
//
   blen = sprintf(buff, "204 loginok wsz: %d %d\n",respWS,bbcp_Config.RWBsz);
   if ((retc = np->Put(buff, blen)) < 0) return -1;

// All done
//
   Remote = np;
   loginStream.np = 0;
   return 1;
}
Beispiel #20
0
int bbcp_Protocol::Process(bbcp_Node *Node)
{
   bbcp_FileSpec *fp = bbcp_Config.srcSpec;
   pthread_t Tid;
   int rc, NoGo = 0;
   char *cp;

// If there is a r/t lock file, make sure it exists
//
   if ((bbcp_Config.Options & bbcp_RTCSRC) && bbcp_Config.rtLockf
   &&  (bbcp_Config.rtLockd = open(bbcp_Config.rtLockf, O_RDONLY)) < 0)
      {rc = errno, NoGo = 1;
       bbcp_Emsg("Config", rc, "opening lock file", bbcp_Config.rtLockf);
      }

// Make sure all of the source files exist at this location. If there is an
// error, defer exiting until after connecting to prevent a hang-up. We
// make sure that we are not trying to copy a directory.
//
   while(fp)
        {NoGo |= fp->Stat();
         if (fp->Info.Otype == 'd' && !(bbcp_Config.Options & bbcp_RECURSE))
            {bbcp_Fmsg("Source", fp->pathname, "is a directory.");
             NoGo = 1; break;
            }

         fp = fp->next;
        }

// If this is a recursive list, do it in the bacground while we try to connect.
// This avoids time-outs when large number of files are enumerated.
//
   if (!NoGo && bbcp_Config.Options & bbcp_RECURSE)
      if ((rc = bbcp_Thread_Start(bbcp_ProtocolLS, 0, &Tid)) < 0)
         {bbcp_Emsg("Protocol", rc, "starting file enumeration thread.");
          NoGo = 1;
         }

// Establish all connections
//
   if (Node->Start(this, (bbcp_Config.Options & bbcp_CON2SRC))
   ||  Node->getBuffers(0)) return 2;
   Local = Node;

// At this point, if we're running with the -r recursive option, our list of
// file specs (bbcp_Config.srcSpec) is being extended recursively to include
// all subdirs and their contents. We must wait for the thread to finish.
//
   if (!NoGo && bbcp_Config.Options & bbcp_RECURSE) bbcp_Thread_Wait(Tid);

// If there was a fatal error, we can exit now, the remote side will exit
//
   if (NoGo)
      {char buff[8];
       strcpy(buff, "eol\n");
       Remote->Put(buff, (ssize_t)4);
       Node->Stop();
       return 2;
      }
   rc = 0;

// Process all control connection requests and return
//
   while(!rc && Remote->GetLine())
      {if (!(cp = Remote->GetToken())) continue;
            if (!strcmp(cp, "flist")) rc = Process_flist();
       else if (!strcmp(cp, "get"))   rc = Process_get();
       else if (!strcmp(cp, "exit")) {rc = Process_exit(); break;}
       else {bbcp_Fmsg("Process", "Invalid command, '", cp, "'.");
             rc = 1;
            }
      }

// Dismantle this node and return
//
   Node->Stop();
   if (cp) return rc;
   bbcp_Fmsg("Source", "Unexpected end of control stream from",
                             Remote->NodeName());
   return 32;
}