Login
f-acat.c at [0ab8c75c49]
Login

File f-apps/f-acat.c artifact e1a30ebc4a part of check-in 0ab8c75c49


/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 
/* vim: set ts=2 et sw=2 tw=80: */
/*
   Copyright (c) 2013 D. Richard Hipp
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the Simplified BSD License (also
   known as the "2-Clause License" or "FreeBSD License".)
  
   This program 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.
  
   Author contact information:
     drh@hwaci.com
     http://www.hwaci.com/drh/
  
  *****************************************************************************
   A simple tool for dumping blobs to stdout.
*/

#include "fossil-scm/fossil-cli.h"
#include "fossil-scm/fossil-internal.h"

static void fcli_local_help(){
  printf("Usage:\n\t%s [options] artifact_id\n\n", fcli.appName);

  puts("Outputs content from fossil repositories. Options:\n");
  puts("\n\t--artifact|-a=NAME specifies the artifact UUID "
       "or symbolic name. May optionally be specified as the "
       "first non-flag argument.\n");
  puts("\n\t--output|-o=FILENAME outputs to the given file. "
       "Default is stdout.\n");
  puts("\n\t--raw fetches blobs in raw form, which means that no "
       "undeltification is applied (but they are decompressed, if "
       "needed).\n");

}

int main(int argc, char * const * argv ){
  int rc = 0;
  char * sym = NULL;
  char * ofileName = NULL;
  FILE * ofile = NULL;
  fsl_cx * f;
  fsl_db * db;
  fsl_id_t rid = 0;
  fsl_uuid_str uuid = NULL;
  fsl_buffer blob = fsl_buffer_empty;
  char raw;
  fcli.appHelp = fcli_local_help;
  rc = fcli_setup(argc, argv);
  if(FSL_RC_BREAK==rc) /* --help */ return 0;
  else if(rc) goto end;
  f = fcli.f;
  db = fsl_cx_db_repo(f);
  if(!db){
    rc = fsl_cx_err_set(f, FSL_RC_MISUSE,
                        "This app requires a repository db.");
    goto end;
  }
  raw = fcli_flag("raw",NULL);
  fcli_flag2("o", "output", &ofileName);
  fcli_flag_or_arg("a", "artifact", &sym);
  if(fcli_has_unused_flags(0)) goto end;
  if(!sym){
    fcli_err_set(FSL_RC_MISUSE, "Missing artifact UUID argument.");
    goto end;
  }
  else if(ofileName){
    ofile = fsl_fopen(ofileName, "w");
    if(!ofile){
      rc = fcli_err_set(FSL_RC_IO,
                        "Could not open file for writing: %s", ofileName);
    }
    fsl_free(ofileName);
    ofileName = NULL;
    if(rc) goto end;
  }

  rc = fsl_sym_to_uuid(f, sym, FSL_CATYPE_ANY, &uuid, &rid);
  if(rc) goto end;
  FCLI_V(("Symbol [%s] resolved to [%.*s] (rid %"FSL_ID_T_PFMT")\n",
           sym, 12, uuid, rid));
  rc = raw
    ? fsl_content_blob(f, rid, &blob)
    : fsl_content_get(f, rid, &blob);
  if(!rc){
    fwrite(blob.mem, blob.used, 1, ofile ? ofile : stdout);
  }
  end:
  fsl_fclose(ofile);
  fsl_buffer_clear(&blob);
  fsl_free(sym);
  fsl_free(uuid);
  return fcli_err_report(1) ? EXIT_FAILURE : EXIT_SUCCESS;
}