Exemplo n.º 1
0
/*
** Write all files from vid to the disk.  Or if vid==0 and id!=0
** write just the specific file where VFILE.ID=id.
*/
void vfile_to_disk(
  int vid,               /* vid to write to disk */
  int id,                /* Write this one file, if not zero */
  int verbose,           /* Output progress information */
  int promptFlag         /* Prompt user to confirm overwrites */
){
  Stmt q;
  Blob content;
  int nRepos = strlen(g.zLocalRoot);

  if( vid>0 && id==0 ){
    db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe, islink"
                   "  FROM vfile"
                   " WHERE vid=%d AND mrid>0",
                   g.zLocalRoot, vid);
  }else{
    assert( vid==0 && id>0 );
    db_prepare(&q, "SELECT id, %Q || pathname, mrid, isexe, islink"
                   "  FROM vfile"
                   " WHERE id=%d AND mrid>0",
                   g.zLocalRoot, id);
  }
  while( db_step(&q)==SQLITE_ROW ){
    int id, rid, isExe, isLink;
    const char *zName;

    id = db_column_int(&q, 0);
    zName = db_column_text(&q, 1);
    rid = db_column_int(&q, 2);
    isExe = db_column_int(&q, 3);
    isLink = db_column_int(&q, 4);
    content_get(rid, &content);
    if( file_is_the_same(&content, zName) ){
      blob_reset(&content);
      if( file_wd_setexe(zName, isExe) ){
        db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
                      file_wd_mtime(zName), id);
      }
      continue;
    }
    if( promptFlag && file_wd_size(zName)>=0 ){
      Blob ans;
      char *zMsg;
      char cReply;
      zMsg = mprintf("overwrite %s (a=always/y/N)? ", zName);
      prompt_user(zMsg, &ans);
      free(zMsg);
      cReply = blob_str(&ans)[0];
      blob_reset(&ans);
      if( cReply=='a' || cReply=='A' ){
        promptFlag = 0;
        cReply = 'y';
      }
      if( cReply=='n' || cReply=='N' ){
        blob_reset(&content);
        continue;
      }
    }
    if( verbose ) vcs_print("%s\n", &zName[nRepos]);
    if( file_wd_isdir(zName) == 1 ){
      /*TODO(dchest): remove directories? */
      vcs_fatal("%s is directory, cannot overwrite\n", zName);
    }    
    if( file_wd_size(zName)>=0 && (isLink || file_wd_islink(zName)) ){
      file_delete(zName);
    }
    if( isLink ){
      symlink_create(blob_str(&content), zName);
    }else{
      blob_write_to_file(&content, zName);
    }
    file_wd_setexe(zName, isExe);
    blob_reset(&content);
    db_multi_exec("UPDATE vfile SET mtime=%lld WHERE id=%d",
                  file_wd_mtime(zName), id);
  }
  db_finalize(&q);
}
Exemplo n.º 2
0
Arquivo: file.c Projeto: digsrc/fossil
/*
** Copy symbolic link from zFrom to zTo.
*/
void symlink_copy(const char *zFrom, const char *zTo){
  Blob content;
  blob_read_link(&content, zFrom);
  symlink_create(blob_str(&content), zTo);
  blob_reset(&content);
}
Exemplo n.º 3
0
/*
** COMMAND: revert
**
** Usage: %fossil revert ?-r REVISION? ?FILE ...?
**
** Revert to the current repository version of FILE, or to
** the version associated with baseline REVISION if the -r flag
** appears.
**
** If FILE was part of a rename operation, both the original file
** and the renamed file are reverted.
**
** Revert all files if no file name is provided.
**
** If a file is reverted accidently, it can be restored using
** the "fossil undo" command.
**
** Options:
**   -r REVISION    revert given FILE(s) back to given REVISION
**
** See also: redo, undo, update
*/
void revert_cmd(void) {
    const char *zFile;
    const char *zRevision;
    Blob record;
    int i;
    int errCode;
    Stmt q;

    undo_capture_command_line();
    zRevision = find_option("revision", "r", 1);
    verify_all_options();

    if( g.argc<2 ) {
        usage("?OPTIONS? [FILE] ...");
    }
    if( zRevision && g.argc<3 ) {
        fossil_fatal("the --revision option does not work for the entire tree");
    }
    db_must_be_within_tree();
    db_begin_transaction();
    undo_begin();
    db_multi_exec("CREATE TEMP TABLE torevert(name UNIQUE);");

    if( g.argc>2 ) {
        for(i=2; i<g.argc; i++) {
            Blob fname;
            zFile = mprintf("%/", g.argv[i]);
            blob_zero(&fname);
            file_tree_name(zFile, &fname, 0, 1);
            db_multi_exec(
                "REPLACE INTO torevert VALUES(%B);"
                "INSERT OR IGNORE INTO torevert"
                " SELECT pathname"
                "   FROM vfile"
                "  WHERE origname=%B;",
                &fname, &fname
            );
            blob_reset(&fname);
        }
    } else {
        int vid;
        vid = db_lget_int("checkout", 0);
        vfile_check_signature(vid, 0);
        db_multi_exec(
            "DELETE FROM vmerge;"
            "INSERT OR IGNORE INTO torevert "
            " SELECT pathname"
            "   FROM vfile "
            "  WHERE chnged OR deleted OR rid=0 OR pathname!=origname;"
        );
    }
    db_multi_exec(
        "INSERT OR IGNORE INTO torevert"
        " SELECT origname"
        "   FROM vfile"
        "  WHERE origname!=pathname AND pathname IN (SELECT name FROM torevert);"
    );
    blob_zero(&record);
    db_prepare(&q, "SELECT name FROM torevert");
    if( zRevision==0 ) {
        int vid = db_lget_int("checkout", 0);
        zRevision = db_text(0, "SELECT uuid FROM blob WHERE rid=%d", vid);
    }
    while( db_step(&q)==SQLITE_ROW ) {
        int isExe = 0;
        int isLink = 0;
        char *zFull;
        zFile = db_column_text(&q, 0);
        zFull = mprintf("%/%/", g.zLocalRoot, zFile);
        errCode = historical_version_of_file(zRevision, zFile, &record,
                                             &isLink, &isExe, 0, 2);
        if( errCode==2 ) {
            if( db_int(0, "SELECT rid FROM vfile WHERE pathname=%Q OR origname=%Q",
                       zFile, zFile)==0 ) {
                fossil_print("UNMANAGE %s\n", zFile);
            } else {
                undo_save(zFile);
                file_delete(zFull);
                fossil_print("DELETE   %s\n", zFile);
            }
            db_multi_exec(
                "UPDATE OR REPLACE vfile"
                "   SET pathname=origname, origname=NULL"
                " WHERE pathname=%Q AND origname!=pathname;"
                "DELETE FROM vfile WHERE pathname=%Q",
                zFile, zFile
            );
        } else {
            sqlite3_int64 mtime;
            undo_save(zFile);
            if( file_wd_size(zFull)>=0 && (isLink || file_wd_islink(0)) ) {
                file_delete(zFull);
            }
            if( isLink ) {
                symlink_create(blob_str(&record), zFull);
            } else {
                blob_write_to_file(&record, zFull);
            }
            file_wd_setexe(zFull, isExe);
            fossil_print("REVERT   %s\n", zFile);
            mtime = file_wd_mtime(zFull);
            db_multi_exec(
                "UPDATE vfile"
                "   SET mtime=%lld, chnged=0, deleted=0, isexe=%d, islink=%d,mrid=rid"
                " WHERE pathname=%Q OR origname=%Q",
                mtime, isExe, isLink, zFile, zFile
            );
        }
        blob_reset(&record);
        free(zFull);
    }
    db_finalize(&q);
    undo_finish();
    db_end_transaction(0);
}