Exemplo n.º 1
0
static int run_amz_file(clamz_downloader *dl,
			const clamz_config *cfg,
			FILE *amzfile,
			const char *fname)
{
  char *inbuf;
  unsigned char *xml;
  size_t sz;
  clamz_playlist *pl;
  int i;
  int status, rv = 0;
  char *logname;
  FILE *logfile;

  sz = 0;
  inbuf = NULL;

  while (!feof(amzfile) && !ferror(amzfile)) {
    if (inbuf)
      inbuf = realloc(inbuf, (sz + 1024) * sizeof(char));
    else
      inbuf = malloc((sz + 1024) * sizeof(char));
    sz += fread(&inbuf[sz], 1, 1024, amzfile);
  }

  if (amzfile != stdin)
    fclose(amzfile);

  if (cfg->printonly && cfg->printasxml) {
    xml = decrypt_amz_file(inbuf, sz, fname);
    if (!xml) {
      free(inbuf);
      return 2;
    }

    printf("%s", xml);
    free(xml);
    free(inbuf);
    return 0;
  }
  else {
    if (!cfg->printonly) {
      if (write_backup_file(inbuf, sz, getbasename(fname))) {
        free(inbuf);
        return 3;
      }
    }

    pl = new_playlist();

    if (read_amz_file(pl, inbuf, sz, fname)) {
      free(inbuf);
      free_playlist(pl);
      return 2;
    }

    if (!cfg->printonly) {
      logname = get_config_file_name("logs", getbasename(fname), ".log");
      if (!logname) {
        free(inbuf);
	return 1;
      }

      logfile = fopen(logname, "w");
      if (!logfile) {
	perror(logname);
	free(logname);
        free(inbuf);
	return 3;
      }

      free(logname);
      set_download_log_file(dl, logfile);
    }
    else {
      logfile = NULL;
    }

    if (cfg->printonly || cfg->verbose)
      print_pl_info(pl, fname);

    for (i = 0; i < pl->num_tracks; i++) {
      if (cfg->printonly || cfg->verbose)
        print_tr_info(pl->tracks[i], i + 1);

      status = download_track(dl, pl->tracks[i]);
      if (!rv)
        rv = status;
      fputc('\n', stderr);
    }

    set_download_log_file(dl, NULL);

    free(inbuf);
    free_playlist(pl);
    if (logfile)
      fclose(logfile);
    return rv;
  }
}
Exemplo n.º 2
0
static gboolean 
run_task (RBMtpThread *thread, RBMtpThreadTask *task)
{
	char *name = task_name (task);
	rb_debug ("running task: %s", name);
	g_free (name);

	switch (task->task) {
	case OPEN_DEVICE:
		open_device (thread, task);
		break;

	case CLOSE_DEVICE:
		return TRUE;

	case SET_DEVICE_NAME:
		if (LIBMTP_Set_Friendlyname (thread->device, task->name)) {
			rb_mtp_thread_report_errors (thread, TRUE);
		}
		break;

	case THREAD_CALLBACK:
		{
			RBMtpThreadCallback cb = (RBMtpThreadCallback)task->callback;
			cb (thread->device, task->user_data);
		}
		break;

	case ADD_TO_ALBUM:
		add_track_to_album_and_update (thread, task);
		break;

	case REMOVE_FROM_ALBUM:
		remove_track_from_album (thread, task);
		break;

	case SET_ALBUM_IMAGE:
		set_album_image (thread, task);
		break;

	case GET_TRACK_LIST:
		get_track_list (thread, task);
		break;

	case DELETE_TRACK:
		if (LIBMTP_Delete_Object (thread->device, task->track_id)) {
			rb_mtp_thread_report_errors (thread, TRUE);
		}
		break;

	case UPLOAD_TRACK:
		upload_track (thread, task);
		break;

	case DOWNLOAD_TRACK:
		download_track (thread, task);
		break;

	default:
		g_assert_not_reached ();
	}

	return FALSE;
}