Beispiel #1
0
static int
executable(clib_package_t *pkg) {
  int rc;
  char *url = NULL;
  char *file = NULL;
  char *tarball = NULL;
  char *command = NULL;
  char *dir = NULL;
  char *deps = NULL;
  char *tmp = NULL;

  tmp = gettempdir();
  if (NULL == tmp) {
    logger_error("error", "gettempdir() out of memory");
    return -1;
  }

  E_FORMAT(&url
    , "https://github.com/%s/%s/archive/%s.tar.gz"
    , pkg->author
    , pkg->name
    , pkg->version);
  E_FORMAT(&file, "%s-%s.tar.gz", pkg->name, pkg->version);
  E_FORMAT(&tarball, "%s/%s", tmp, file);
  rc = http_get_file(url, tarball);
  E_FORMAT(&command, "cd %s && tar -xf %s", tmp, file);

  // cheap untar
  rc = system(command);
  if (0 != rc) goto cleanup;

  E_FORMAT(&dir, "%s/%s-%s", tmp, pkg->name, pkg->version);

  if (pkg->dependencies) {
    E_FORMAT(&deps, "%s/deps", dir);
    rc = clib_package_install_dependencies(pkg, deps, opts.verbose);
    if (-1 == rc) goto cleanup;
  }

  free(command);
  command = NULL;

  E_FORMAT(&command, "cd %s && %s", dir, pkg->install);
  rc = system(command);

cleanup:
  free(tmp);
  free(dir);
  free(command);
  free(tarball);
  free(file);
  free(url);
  return rc;
}
Beispiel #2
0
int main() {
  response_t *res = http_get("http://google.com");
  assert(res);
  assert(res->data);
  assert(res->ok);
  assert(200 == res->status);

  assert(0 == http_get_file("http://google.com", "./google.html"));
  assert(0 == fs_exists("./google.html"));
  return 0;
}
Beispiel #3
0
void get_updates()
{
	char servername[80];
	char filepath_on_server[80];
	char local_filepath[80];
	FILE *fp;
	strcpy(servername, "no-exit.org");
	strcpy(filepath_on_server, "/el/files/testfile");
	strcpy(local_filepath, "testfile");
	fp = fopen(local_filepath, "w");
	http_get_file(servername, filepath_on_server, fp);
	fclose(fp);
}
Beispiel #4
0
// the actual background downloader
int http_get_file_thread_handler(void *specs){
	struct http_get_struct *spec= (struct http_get_struct *) specs;
	SDL_Event event;

	init_thread_log("get_file");

	// load the file
	spec->status= http_get_file(spec->server, spec->path, spec->fp);
	fclose(spec->fp);
	spec->fp= NULL;
	
	// check to see if the file is correct
	if (spec->md5 && *spec->md5)
	{
		// get the MD5 for the file
		if (file_temp_check(download_temp_file, spec->md5) != 0)
		{
			LOG_ERROR("Download of %s does not match the MD5 sum in the update file!", spec->path);
			spec->status= 404;
			// remove the temp-file
			file_remove_config(download_temp_file);
			// and make sure we can't restart
			allow_restart= 0;
			restart_required= 0;
		}
	}

	LOG_DEBUG("Finished downloading %s\n", spec->path);

	// signal we are done
	event.type= SDL_USEREVENT;
	event.user.code= spec->event;
	event.user.data1= spec;
	SDL_Delay(500);			// FIXME: This should _not_ be necessary, but appears to be so recently under Windows
	SDL_PushEvent(&event);
	// NOTE: it is up to the EVENT handler to close the handle & free the spec pointer in data1

	return(0);
}
Beispiel #5
0
int http_download_file(char* url,char* save_path)
{
    int ret;
    int sockfd;
    int file_size;
    
    char file_name[MAX_FILENAME_LENGTH+1];
    char* file_buf;
    char save_file_path[MAX_FILENAME_LENGTH+1];
    char path[PATH_LENGTH+1];

    ret = http_get_ip_port(url,server_ip,server_port);
    if(ret == -1)
        return -1;
    else
        sprintf(server_host,"%s:%s",server_ip,server_port);


    D(printf("server host:\t%s\n",server_host));
    
    
    ret = http_get_file_name(url,file_name);
    if ( ret == -1 )
        return -1;
    
    ret = http_get_file_path(url,path);
    if ( ret == -1 )
        return -1;

    D(printf("request path:\t%s\n",path));


    sockfd = socket_connect(server_ip, server_port);
    
    file_size = http_get_file_size(sockfd,path);
    if ( file_size == -1 )
        return -1; 

    D(printf("http get file size:%d\n",file_size));
    
    file_buf = (char*) malloc (file_size*sizeof(char)*2);
    if(file_buf == NULL )
    {
        perror("ERROR:malloc file buffer failed");
        return -1;
    }
    else
        memset(file_buf,0,file_size*2);
    
    ret = http_get_file(sockfd,path,file_size,file_buf);
    
    close(sockfd);

    if ( ret < 0 )
    {
        free(file_buf);
        return -1;    
    }

    else
    {
        sprintf(save_file_path,"%s",file_name);
        save_file(file_buf,ret,save_file_path);
        free(file_buf);
    }
    
             
}
Beispiel #6
0
int
clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
  if (!pkg || !dir) return -1;

  char *pkg_dir = path_join(dir, pkg->name);
  if (NULL == pkg_dir) {
    return -1;
  }

  if (-1 == mkdirp(pkg_dir, 0777)) {
    free(pkg_dir);
    return -1;
  }

  if (NULL == pkg->url) {
    pkg->url = clib_package_url(pkg->author, pkg->repo_name, pkg->version);
  }
  if (NULL == pkg->url) {
    free(pkg_dir);
    return -1;
  }

  if (NULL != pkg->src) {
    // write package.json

    char *package_json = path_join(pkg_dir, "package.json");
    if (NULL == package_json) {
      free(pkg_dir);
      return -1;
    }

    fs_write(package_json, pkg->json);
    free(package_json);

    // write each source

    list_node_t *node;
    list_iterator_t *it = list_iterator_new(pkg->src, LIST_HEAD);
    while ((node = list_iterator_next(it))) {
      char *filename = node->val;

      // download source file

      char *file_url = clib_package_file_url(pkg->url, filename);
      char *file_path = path_join(pkg_dir, basename(filename));

      if (NULL == file_url || NULL == file_path) {
        if (file_url) free(file_url);
        free(pkg_dir);
        return -1;
      }

      if (verbose) {
        clib_package_log("fetch", file_url);
        clib_package_log("save", file_path);
      }

      int rc = http_get_file(file_url, file_path);
      free(file_url);
      free(file_path);
      if (-1 == rc) {
        if (verbose) clib_package_error("error", "unable to fetch %s", file_url);
        free(pkg_dir);
        return -1;
      }
    }

    list_iterator_destroy(it);
  }

  return clib_package_install_dependencies(pkg, dir, verbose);
}
Beispiel #7
0
int
clib_package_install(clib_package_t *pkg, const char *dir, int verbose) {
  char *pkg_dir = NULL;
  char *package_json = NULL;
  list_iterator_t *iterator = NULL;
  int rc = -1;

  if (!pkg || !dir) goto cleanup;
  if (!(pkg_dir = path_join(dir, pkg->name))) goto cleanup;

  // create directory for pkg
  if (-1 == mkdirp(pkg_dir, 0777)) goto cleanup;

  if (NULL == pkg->url) {
    pkg->url = clib_package_url(pkg->author
      , pkg->repo_name
      , pkg->version);
    if (NULL == pkg->url) goto cleanup;
  }

  // if no sources are listed, just install
  if (NULL == pkg->src) goto install;

  // write package.json
  if (!(package_json = path_join(pkg_dir, "package.json"))) goto cleanup;
  if (-1 == fs_write(package_json, pkg->json)) {
    logger_error("error", "Failed to write %s", package_json);
    goto cleanup;
  }

  iterator = list_iterator_new(pkg->src, LIST_HEAD);
  list_node_t *source;
  while ((source = list_iterator_next(iterator))) {
    char *filename = NULL;
    char *file_url = NULL;
    char *file_path = NULL;
    int error = 0;

    filename = source->val;

    // get file URL to fetch
    if (!(file_url = clib_package_file_url(pkg->url, filename))) {
      error = 1;
      goto loop_cleanup;
    }

    // get file path to save
    if (!(file_path = path_join(pkg_dir, basename(filename)))) {
      error = 1;
      goto loop_cleanup;
    }

    // TODO the whole URL is overkill and floods my terminal
    if (verbose) logger_info("fetch", file_url);

    // fetch source file and save to disk
    if (-1 == http_get_file(file_url, file_path)) {
      logger_error("error", "unable to fetch %s", file_url);
      error = 1;
      goto loop_cleanup;
    }

    if (verbose) logger_info("save", file_path);

  loop_cleanup:
    if (file_url) free(file_url);
    if (file_path) free(file_path);
    if (error) {
      list_iterator_destroy(iterator);
      rc = -1;
      goto cleanup;
    }
  }

install:
  rc = clib_package_install_dependencies(pkg, dir, verbose);

cleanup:
  if (pkg_dir) free(pkg_dir);
  if (package_json) free(package_json);
  if (iterator) list_iterator_destroy(iterator);
  return rc;
}