/**
 * \brief remove repository
 *
 * \return 0 on OK, -1 on failure
 */
int remove_Repository()
{
    char *Pfile = "F1D2319DF20ABC4CEB02CA5A3C2021BD87B26810.87972FC55E2CDD2609ED85051BE50BAF.722";
    if (fo_RepExist("files",Pfile))
    {
        if (fo_RepRemove("files", Pfile) != 0)
        {
            printf("Failed to remove %s\n", Pfile);
            return (-1);
        }
    }
    return (0);
}
Example #2
0
/**
 * \brief Given an upload ID, delete it.
 *
 * \param uploadId the upload id
 * \param userId
 * \param userPerm permission level the user has
 *
 * \return 0: yes, can is deleted;
 *         1: can not be deleted;
 *        -1: failure;
 *        -2: does not exist
 */
int deleteUpload (long uploadId, int userId, int userPerm)
{
  char *S;
  int Row,maxRow;
  char tempTable[256];
  PGresult *result, *pfileResult;
  char SQL[MAXSQL], desc[myBUFSIZ];

  int permission_upload = check_write_permission_upload(uploadId, userId, userPerm);
  if(0 != permission_upload) {
    return permission_upload;
  }

  snprintf(tempTable,sizeof(tempTable),"DelUp_%ld_pfile",uploadId);
  snprintf(SQL,MAXSQL,"DROP TABLE IF EXISTS %s;",tempTable);
  PQexecCheckClear(NULL, SQL, __FILE__, __LINE__);

  snprintf(desc, myBUFSIZ, "Deleting upload %ld",uploadId);
  PQexecCheckClear(desc, "SET statement_timeout = 0;", __FILE__, __LINE__);
  PQexecCheckClear(NULL, "BEGIN;", __FILE__, __LINE__);

  /* Delete everything that impacts the UI */
  if (!Test) {
    /* The UI depends on uploadtree and folders for navigation.
     Delete them now to block timeouts from the UI. */
    PQexecCheckClear(NULL, "COMMIT;", __FILE__, __LINE__);
  }

  /* Begin complicated stuff */
  /* Get the list of pfiles to delete */
  /* These are all pfiles in the upload_fk that only appear once. */
  snprintf(SQL,MAXSQL,"SELECT DISTINCT pfile_pk,pfile_sha1 || '.' || pfile_md5 || '.' || pfile_size AS pfile INTO %s FROM uploadtree INNER JOIN pfile ON upload_fk = %ld AND pfile_fk = pfile_pk;",tempTable,uploadId);
  PQexecCheckClear("Getting list of pfiles to delete", SQL, __FILE__, __LINE__);

  /* Remove pfiles which are reused by other uploads */
  snprintf(SQL, MAXSQL, "DELETE FROM %s WHERE pfile_pk IN (SELECT pfile_pk FROM %s INNER JOIN uploadtree ON pfile_pk = pfile_fk WHERE upload_fk != %ld)", tempTable, tempTable, uploadId);
  PQexecCheckClear(NULL, SQL, __FILE__, __LINE__);

  if (Verbose) {
    snprintf(SQL,MAXSQL,"SELECT COUNT(*) FROM %s;",tempTable);
    result = PQexec(pgConn, SQL);
    if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__)) {
      return -1;
    }
    printf("# Created pfile table %s with %ld entries\n", tempTable, atol(PQgetvalue(result,0,0)));
    PQclear(result);
  }

  /* Now to delete the actual pfiles from the repository before remove the DB. */
  /* Get the file listing -- needed for deleting pfiles from the repository. */
  snprintf(SQL,MAXSQL,"SELECT * FROM %s ORDER BY pfile_pk;",tempTable);
  pfileResult = PQexec(pgConn, SQL);
  if (fo_checkPQresult(pgConn, pfileResult, SQL, __FILE__, __LINE__)) {
    return -1;
  }

  if (Test <= 1) {
    maxRow = PQntuples(pfileResult);
    for(Row=0; Row<maxRow; Row++) {
      S = PQgetvalue(pfileResult,Row,1); /* sha1.md5.len */
      if (fo_RepExist("files",S)) {
        if (Test) {
          printf("TEST: Delete %s %s\n","files",S);
        } else {
          fo_RepRemove("files",S);
        }
      }
      if (fo_RepExist("gold",S)) {
        if (Test) {
          printf("TEST: Delete %s %s\n","gold",S);
        } else {
          fo_RepRemove("gold",S);
        }
      }
      fo_scheduler_heart(1);
    }
  }
  PQclear(pfileResult);

  /*
   This begins the slow part that locks the DB.
   The problem is, we don't want to lock a critical row,
   otherwise the scheduler will lock and/or fail.
  */
  if (!Test) {
    PQexecCheckClear(NULL, "BEGIN;", __FILE__, __LINE__);
  }
  /* Delete the upload from the folder-contents table */
  snprintf(SQL,MAXSQL,"DELETE FROM foldercontents WHERE (foldercontents_mode & 2) != 0 AND child_id = %ld;",uploadId);
  PQexecCheckClear("Deleting foldercontents", SQL, __FILE__, __LINE__);

  /* Deleting the actual upload contents*/
  /* Delete the bucket_container record as it can't be cascade delete with upload table */
  snprintf(SQL,MAXSQL,"DELETE FROM bucket_container USING uploadtree WHERE uploadtree_fk = uploadtree_pk AND upload_fk = %ld;",uploadId);
  PQexecCheckClear("Deleting bucket_container", SQL, __FILE__, __LINE__);

  /* Delete the tag_uploadtree record as it can't be cascade delete with upload table */
  snprintf(SQL,MAXSQL,"DELETE FROM tag_uploadtree USING uploadtree WHERE uploadtree_fk = uploadtree_pk AND upload_fk = %ld;",uploadId);
  PQexecCheckClear("Deleting tag_uploadtree", SQL, __FILE__, __LINE__);

  /* Delete uploadtree_nnn table */
  char uploadtree_tablename[1024];
  snprintf(SQL,MAXSQL,"SELECT uploadtree_tablename FROM upload WHERE upload_pk = %ld;",uploadId);
  result = PQexec(pgConn, SQL);
  if (fo_checkPQresult(pgConn, result, SQL, __FILE__, __LINE__)) {
    return -1;
  }
  if (PQntuples(result)) {
    strcpy(uploadtree_tablename, PQgetvalue(result, 0, 0));
    PQclear(result);
    if (strcasecmp(uploadtree_tablename,"uploadtree_a")) {
      snprintf(SQL,MAXSQL,"DROP TABLE %s;", uploadtree_tablename);
      PQexecCheckClear(NULL, SQL, __FILE__, __LINE__);
    }
  }

  printfInCaseOfVerbosity("Deleting license decisions for upload %ld\n",uploadId);
  /* delete from clearing_decision_event table. */
  snprintf(SQL, MAXSQL, "DELETE FROM clearing_decision_event USING clearing_event WHERE clearing_decision_event.clearing_event_fk = clearing_event.clearing_event_pk AND clearing_event.uploadtree_fk IN (SELECT uploadtree_pk FROM uploadtree INNER JOIN %s ON uploadtree.pfile_fk = %s.pfile_pk WHERE upload_fk = %ld);", tempTable, tempTable, uploadId);
  PQexecCheckClear("Deleting from clearing_decision_event", SQL, __FILE__, __LINE__);

  /* delete from clearing_event table. */
  snprintf(SQL, MAXSQL, "DELETE FROM clearing_event WHERE uploadtree_fk IN (SELECT uploadtree_pk FROM uploadtree INNER JOIN %s ON uploadtree.pfile_fk = %s.pfile_pk WHERE upload_fk = %ld);", tempTable, tempTable, uploadId);
  PQexecCheckClear("Deleting from clearing_event", SQL, __FILE__, __LINE__);

  /* delete from uploadtree table. */
  snprintf(SQL, MAXSQL, "DELETE FROM uploadtree WHERE upload_fk = %ld;", uploadId);
  PQexecCheckClear("Deleting from uploadtree", SQL, __FILE__, __LINE__);

  /* delete from pfile is SLOW due to constraint checking. Do it separately. */
  snprintf(SQL,MAXSQL,"DELETE FROM pfile USING %s WHERE pfile.pfile_pk = %s.pfile_pk;",tempTable,tempTable);
  PQexecCheckClear("Deleting from pfile", SQL, __FILE__, __LINE__);

  snprintf(SQL,MAXSQL,"DROP TABLE %s;",tempTable);
  PQexecCheckClear(NULL, SQL, __FILE__, __LINE__);

  PQexecCheckClear(NULL, "SET statement_timeout = 120000;", __FILE__, __LINE__);

  printfInCaseOfVerbosity("Deleted upload %ld from DB, now doing repository.\n",uploadId);

  if (Test) {
    PQexecCheckClear(NULL, "ROLLBACK;", __FILE__, __LINE__);
  } else {
    PQexecCheckClear(NULL, "COMMIT;", __FILE__, __LINE__);
  }

  printfInCaseOfVerbosity("Deleted upload %ld\n",uploadId);

  return 0; /* success */
} /* deleteUpload() */