Beispiel #1
0
/**
 * \brief for function Archivefs(), reguler file
 */
void test_Archivefs_file()
{
  char file_path[] = "./Makefile";
  char tar_file[] = "/tmp/Suckupfs.tar.dir/testfile";
  char des_dir[] = "/tmp/Suckupfs.tar.dir/";
  int tar_status = -1;
  char commands[1024] = "";
  struct stat Status;
  if (stat(file_path, &Status) != 0) return; // file_path is not exist or can not access

  int res = Archivefs(file_path, tar_file, des_dir, Status);
  CU_ASSERT_EQUAL(1, res);
  tar_status = stat(file_path, &Status);
  CU_ASSERT_EQUAL(0, tar_status);
  snprintf(commands, sizeof(commands), "file %s |grep ASCII >/dev/null 2>&1", tar_file);
  int rc = system(commands);
  CU_ASSERT_EQUAL(1, rc != -1 && (WEXITSTATUS(rc) == 0));
  rmdir(des_dir);
}
Beispiel #2
0
int main  (int argc, char *argv[])
{
    int arg;
    char *Parm = NULL;
    char *TempFileDir=NULL;
    int c;
    int InitFlag=0;
    int CmdlineFlag = 0; /** run from command line flag, 1 yes, 0 not */
    int user_pk;
    char *agent_desc = "Network downloader.  Uses wget(1).";

    memset(GlobalTempFile,'\0',MAXCMD);
    memset(GlobalURL,'\0',MAXCMD);
    memset(GlobalParam,'\0',MAXCMD);
    memset(GlobalType,'\0',MAXCMD);
    GlobalUploadKey = -1;
    int upload_pk = 0;           // the upload primary key
    //int Agent_pk;
    char *SVN_REV;
    char *VERSION;
    char agent_rev[MAXCMD];

    /* open the connection to the scheduler and configuration */
    fo_scheduler_connect(&argc, argv, &pgConn);

    /* Process command-line */
    while((c = getopt(argc,argv,"d:Gg:ik:A:R:l:Cc:Vvh")) != -1)
    {
        switch(c)
        {
        case 'd':
            TempFileDir = PathCheck(optarg);
            break;
        case 'g':
        {
            struct group *SG;
            SG = getgrnam(optarg);
            if (SG) ForceGroup = SG->gr_gid;
        }
        break;
        case 'G':
            GlobalImportGold=0;
            break;
        case 'i':
            InitFlag=1;
            break;
        case 'k':
            GlobalUploadKey = atol(optarg);
            if (!GlobalTempFile[0])
                strcpy(GlobalTempFile,"wget.default_download");
            break;
        case 'A':
            sprintf(GlobalParam, "%s -A %s ",GlobalParam, optarg);
            break;
        case 'R':
            sprintf(GlobalParam, "%s -R %s ",GlobalParam, optarg);
            break;
        case 'l':
            sprintf(GlobalParam, "%s -l %s ",GlobalParam, optarg);
            break;
        case 'c':
            break; /* handled by fo_scheduler_connect() */
        case 'C':
            CmdlineFlag = 1;
            break;
        case 'v':
            break;
        case 'V':
            printf("%s", BuildVersion);
            SafeExit(0);
        default:
            Usage(argv[0]);
            SafeExit(-1);
        }
    }
    if (argc - optind > 1)
    {
        Usage(argv[0]);
        SafeExit(-1);
    }

    /* When initializing the DB, don't do anything else */
    if (InitFlag)
    {
        if (pgConn) PQfinish(pgConn);
        SafeExit(0);
    }

    SVN_REV = fo_sysconfig("wget_agent", "SVN_REV");
    VERSION = fo_sysconfig("wget_agent", "VERSION");
    sprintf(agent_rev, "%s.%s", VERSION, SVN_REV);
    /* Get the Agent Key from the DB */
    fo_GetAgentKey(pgConn, basename(argv[0]), GlobalUploadKey, agent_rev, agent_desc);

    /** get proxy */
    GetProxy();

    /* Run from the command-line (for testing) */
    for(arg=optind; arg < argc; arg++)
    {
        memset(GlobalURL,'\0',sizeof(GlobalURL));
        strncpy(GlobalURL,argv[arg],sizeof(GlobalURL));
        /* If the file contains "://" then assume it is a URL.
           Else, assume it is a file. */
        LOG_VERBOSE0("Command-line: %s",GlobalURL);
        if (strstr(GlobalURL,"://"))
        {
            fo_scheduler_heart(1);
            LOG_VERBOSE0("It's a URL");
            if (GetURL(GlobalTempFile,GlobalURL,TempFileDir) != 0)
            {
                LOG_FATAL("Download of %s failed.",GlobalURL);
                SafeExit(21);
            }
            if (GlobalUploadKey != -1) {
                DBLoadGold();
            }
            unlink(GlobalTempFile);
        }
        else /* must be a file */
        {
            LOG_VERBOSE0("It's a file -- GlobalUploadKey = %ld",GlobalUploadKey);
            if (GlobalUploadKey != -1)
            {
                memcpy(GlobalTempFile,GlobalURL,MAXCMD);
                DBLoadGold();
            }
        }
    }

    /* Run from scheduler! */
    if (0 == CmdlineFlag)
    {
        user_pk = fo_scheduler_userID(); /* get user_pk for user who queued the agent */
        while(fo_scheduler_next())
        {
            Parm = fo_scheduler_current(); /* get piece of information, including upload_pk, downloadfile url, and parameters */
            if (Parm && Parm[0])
            {
                fo_scheduler_heart(1);
                /* set globals: uploadpk, downloadfile url, parameters */
                SetEnv(Parm,TempFileDir);
                upload_pk = GlobalUploadKey;

                /* Check Permissions */
                if (GetUploadPerm(pgConn, upload_pk, user_pk) < PERM_WRITE)
                {
                    LOG_ERROR("You have no update permissions on upload %d", upload_pk);
                    continue;
                }

                char TempDir[MAXCMD];
                memset(TempDir,'\0',MAXCMD);
                snprintf(TempDir, MAXCMD-1, "%s/wget", TempFileDir); // /var/local/lib/fossology/agents/wget
                struct stat Status;

                if (GlobalType[0])
                {
                    if (GetVersionControl() == 0)
                    {
                        DBLoadGold();
                        unlink(GlobalTempFile);
                    }
                    else
                    {
                        LOG_FATAL("upload %ld File retrieval failed: uploadpk=%ld tempfile=%s URL=%s Type=%s",
                                  GlobalUploadKey,GlobalUploadKey,GlobalTempFile,GlobalURL, GlobalType);
                        SafeExit(23);
                    }
                }
                else if (strstr(GlobalURL, "*") || stat(GlobalURL, &Status) == 0)
                {
                    if (!Archivefs(GlobalURL, GlobalTempFile, TempFileDir, Status))
                    {
                        LOG_FATAL("Failed to archieve. GlobalURL, GlobalTempFile, TempFileDir are: %s, %s, %s, "
                                  "Mode is: %lo (octal)\n", GlobalURL, GlobalTempFile, TempFileDir, (unsigned long) Status.st_mode);
                        SafeExit(50);
                    }
                    DBLoadGold();
                    unlink(GlobalTempFile);
                }
                else
                {
                    if (GetURL(GlobalTempFile,GlobalURL,TempDir) == 0)
                    {
                        DBLoadGold();
                        unlink(GlobalTempFile);
                    }
                    else
                    {
                        LOG_FATAL("upload %ld File retrieval failed: uploadpk=%ld tempfile=%s URL=%s",
                                  GlobalUploadKey,GlobalUploadKey,GlobalTempFile,GlobalURL);
                        SafeExit(22);
                    }
                }
            }
        }
    } /* if run from scheduler */

    SafeExit(0);
    exit(0);  /* to prevent compiler warning */
} /* main() */