Example #1
0
static void test_progress_bar_cb()
{
  char *old_columns = getenv("COLUMNS");
  progress_bar *pb;

  setenv("COLUMNS", "78", 1);
  pb = progress_bar_new(0);
  /* Silence progress bar output by directing it to a temporary file */
  pb->f = tmpfile();

  progress_bar_cb(pb, 300, 0, 0, 0);
  g_assert_cmpstr(pb->buffer, ==, "                                                                         ");
  progress_bar_cb(pb, 300, 150, 0, 0);
  g_assert_cmpstr(pb->buffer, ==, "####################################                                     ");
  progress_bar_cb(pb, 300, 450, 0, 0);
  g_assert_cmpstr(pb->buffer, ==, "#########################################################################");
  progress_bar_cb(pb, 300, -1, 0, 0);
  g_assert_cmpstr(pb->buffer, ==, "                                                                         ");
  pb->resume_from = 150;
  progress_bar_cb(pb, 150, 0, 0, 0);
  g_assert_cmpstr(pb->buffer, ==, "####################################                                     ");

  fclose(pb->f);
  progress_bar_free(pb);

  if (old_columns) {
    setenv("COLUMNS", old_columns, 1);
  }
}
Example #2
0
static void
progress_bar_finalize (value barv)
{
  struct progress_bar *bar = Bar_val (barv);

  if (bar)
    progress_bar_free (bar);
}
Example #3
0
static void test_progress_bar_new()
{
  char *old_columns = getenv("COLUMNS");
  progress_bar *pb;

  setenv("COLUMNS", "120", 1);
  pb = progress_bar_new(0);
  g_assert_cmpint(pb->width, ==, 74);
  progress_bar_free(pb);

  setenv("COLUMNS", "50", 1);
  pb = progress_bar_new(0);
  g_assert_cmpint(pb->width, ==, 45);
  progress_bar_free(pb);

  setenv("COLUMNS", "abc", 1);
  pb = progress_bar_new(0);
  g_assert_cmpint(pb->width, ==, 74);
  progress_bar_free(pb);

  if (old_columns) {
    setenv("COLUMNS", old_columns, 1);
  }
}
Example #4
0
static int _do_download(channel *c, channel_info *channel_info, rss_item *item,
                        void *user_data, channel_callback cb, int resume,
                        int debug, int show_progress_bar)
{
  int download_failed;
  long resume_from = 0;
  gchar *enclosure_full_filename;
  FILE *enclosure_file;
  struct stat fileinfo;
  progress_bar *pb;

  /* Check that the spool directory exists. */
  if (!g_file_test(c->spool_directory, G_FILE_TEST_IS_DIR)) {
    g_fprintf(stderr, "Spool directory %s not found.\n", c->spool_directory);
    return 1;
  }

  /* Build enclosure filename. */
  enclosure_full_filename = build_enclosure_filename(c->spool_directory,
    c->filename_pattern, channel_info, item);

  if (g_file_test(enclosure_full_filename, G_FILE_TEST_EXISTS)) {
    /* A file with the same filename already exists. If the user has asked us
       to resume downloads, we should append to the file. Otherwise we should
       refuse to continue. If the feed uses the same filename for each
       enclosure, running in append mode will corrupt existing files. There is
       probably no practical way to avoid this, and the issue is documented in
       castget(1) and castgetrc(5). */
    if (resume) {
      /* Set resume offset to the size of the file as it is now (and use
         non-append mode if the size is zero or stat() fails). */
      if (0 == stat(enclosure_full_filename, &fileinfo))
        resume_from = fileinfo.st_size;
      else
        resume_from = 0;
    } else {
      /* File exists but user does not allow us to append so we have to abort. */
      g_fprintf(stderr, "Enclosure file %s already exists.\n", enclosure_full_filename);
      g_free(enclosure_full_filename);
      return 1;
    }
  } else
    /* By letting the offset be 0 we will write in non-append mode. */
    resume_from = 0;

  enclosure_file = fopen(enclosure_full_filename, resume_from ? "ab" : "wb");

  if (!enclosure_file) {
    g_fprintf(stderr, "Error opening enclosure file %s.\n", enclosure_full_filename);
    g_free(enclosure_full_filename);
    return 1;
  }

  if (cb)
    cb(user_data, CCA_ENCLOSURE_DOWNLOAD_START, channel_info, item->enclosure, enclosure_full_filename);

  if (show_progress_bar)
    pb = progress_bar_new(resume_from);
  else
    pb = NULL;

  if (urlget_buffer(item->enclosure->url, enclosure_file, _enclosure_urlget_cb, resume_from, debug, pb)) {
    g_fprintf(stderr, "Error downloading enclosure from %s.\n", item->enclosure->url);

    download_failed = 1;
  } else
    download_failed = 0;

  if (pb)
    progress_bar_free(pb);

  fclose(enclosure_file);

  if (cb)
    cb(user_data, CCA_ENCLOSURE_DOWNLOAD_END, channel_info, item->enclosure, enclosure_full_filename);

  g_free(enclosure_full_filename);

  return download_failed;
}