Changeset - e166f7bf8ca0
[Not reviewed]
default
0 1 0
Ethan Zonca (ethanzonca) - 16 years ago 2010-02-20 20:47:37
e@ethanzonca.com
Initial libarchive implementation of unpackJob
1 file changed with 42 insertions and 4 deletions:
0 comments (0 inline, 0 general)
src/server/slavefuncs.c
Show inline comments
 
@@ -8,48 +8,51 @@
 
  the Free Software Foundation, either version 3 of the License, or
 
  (at your option) any later version.
 

	
 
  DistRen is distributed in the hope that it will be useful,
 
  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
  GNU Affero General Public License for more details.
 

	
 
  You should have received a copy of the GNU Affero General Public License
 
  along with DistRen.  If not, see <http://www.gnu.org/licenses/>.
 
*/
 

	
 
#include "distrenjob.h"
 
#include "slavefuncs.h"
 

	
 
#include "common/asprintf.h"
 
#include "common/execio.h"
 
#include "common/protocol.h"
 
#include "common/remoteio.h"
 

	
 
#include <curl/curl.h>
 
#include <curl/easy.h>
 
#include <curl/types.h>
 

	
 
#include <archive.h>
 
#include <archive_entry.h>
 

	
 
#include <stdio.h>
 
#include <string.h>
 
#include <unistd.h>
 
#include <stdlib.h>
 
#include <sys/stat.h>
 
#include <fcntl.h>
 

	
 

	
 
/**
 
   Sends the server a single request (see protocol.h)
 
*/
 
int sendSignal(struct remoteio *rem, char signal)
 
{
 
  size_t written;
 
  size_t towrite;
 
  char *ssignal;
 

	
 
  _distren_asprintf(&ssignal, "%c", signal);
 
  towrite = strlen(ssignal);
 
  while( towrite
 
	 && !remoteio_write(rem, ssignal, towrite, &written))
 
    {
 
      fprintf(stderr, "Sending request...\n");
 
      towrite -= written;
 
@@ -368,63 +371,98 @@ int downloadTar(char *url, char *destina
 
int uploadOutput(char *pathtoOutput, char *urltoOutput){
 
  if( !curlpost(pathtoOutput, urltoOutput)) // Uploads output
 
    {
 
      fprintf(stderr,"Upload successful, removing old output...\n");
 
      remove(pathtoOutput); // Delete the file after its uploaded
 
      return 0;
 
    }
 
  else
 
    {
 
      fprintf(stderr,"Upload failed. Check your network connection. Retrying upload...\n");
 
      int tries=1;
 
      while(tries<=10 && curlpost(pathtoOutput, urltoOutput))
 
        {
 
          fprintf(stderr, "Upload failed. Trying again in 10 seconds... (attempt %d of 10)\n", tries);
 
          tries++;
 
          sleep(10);
 
        }
 
      return 1; // Upload failed after multiple tries
 
      // @FUTURE: Keep track of files that we were unable to upload, and upload them later
 
    }
 
}
 

	
 

	
 
int unpackJob(char *pathtoTar, int jobnum){
 
  char *tarcmd;        /* Command to run for tar. Migrate to libtar sometime */
 
  char *outdir;        /* Output Directory for tar */
 
  int ret;
 
  int buffsize = 8192;
 
  char *buff = "";
 
  size_t size;
 
  struct archive_entry *ae = archive_entry_new();
 
  struct archive *a = archive_read_new();
 

	
 
  archive_read_support_compression_all(a);
 
  archive_read_support_format_raw(a);
 
  ret = archive_read_open_filename(a, pathtoTar, 16384);
 
  if (ret != ARCHIVE_OK) {
 
    return 1;
 
  }
 
  ret = archive_read_next_header(a, &ae);
 
  if (ret != ARCHIVE_OK) {
 
    return 1;
 
  }
 
   for (;;) {
 
    size = archive_read_data(a, buff, buffsize);
 
    if (size < 0) {
 
      return 1;
 
    }
 
    if (size == 0)
 
      break;
 
    write(1, buff, size);
 
  }
 

	
 
  archive_read_finish(a);
 
  archive_entry_free(ae);
 

	
 
  return 0;
 

	
 
/*
 
  char *tarcmd;        // Command to run for tar. Migrate to libtar sometime
 
  char *outdir;        // Output Directory for tar
 

	
 
  _distren_asprintf(&outdir, "/tmp/distren/job%d", jobnum);
 
  mkdir("/tmp/distren", 0750); /* @FIXME: Change to tmpdir once it exists */
 
  mkdir("/tmp/distren", 0750); // @FIXME: Change to tmpdir once it exists
 
  mkdir(outdir, 0750);
 

	
 
  _distren_asprintf(&tarcmd, "tar -xvf \"%s\" -C \"%s\"", pathtoTar, outdir); /* @FIXME:Use a lib here! */
 
  _distren_asprintf(&tarcmd, "tar -xvf \"%s\" -C \"%s\"", pathtoTar, outdir); // @FIXME:Use a lib here!
 
  system(tarcmd);
 
  free(tarcmd);
 
  free(pathtoTar);
 
  free(outdir);
 
  return 0;
 
  */
 
}
 

	
 

	
 
void prepareJobPaths(int jobnum,char *datadir, char **urltoTar,char **pathtoTar,char **pathtoJobfile,char **urltoOutput,char **pathtoXml){
 
  // Variable Preparation
 
  char *jobdatapath;
 
   _distren_asprintf(&jobdatapath, "job%d", jobnum);
 
   _distren_asprintf(urltoTar, "http://data.distren.org/job%d/job%d.tar.gz", jobnum); // Prepares URL to download from
 
   _distren_asprintf(pathtoTar, "%s/stor/jobdata/job%d.tar.gz", datadir, jobnum); // Prepares destination to save to
 
   _distren_asprintf(pathtoJobfile, "%s/%s/job.blend", datadir, jobdatapath ); // Prepares the path to the jobfile
 
   _distren_asprintf(urltoOutput, "http://data.distren.org/tmp/", jobdatapath ); // Prepares the URL where output is posted
 
   _distren_asprintf(pathtoXml, "%s/job%d/job%d.xml", datadir, jobnum ); // Prepares the path to the job's XML file
 
   free(jobdatapath);
 
}
 

	
 
int checkUsername(char *username){
 
  if(username == NULL || strcmp(username, "!username") == 0 ) {
 
     fprintf(stderr, "\nPlease ensure that your username is present in distrenslave.conf\n");
 
     return 0;
 
   }
 
   else
 
     if( username != NULL || strcmp(username, "!username") != 0 )
 
       {
 
         // Log in the user
 
         if(login_user(username) == 1){
 
           fprintf(stderr, "You should now be logged into distren.\n");
 
           return 0;
0 comments (0 inline, 0 general)