示例#1
0
static int
check_package_conflict(const char *pkgname, void *v)
{
    struct package_conflict *conflict = v;
    package_t pkg;
    plist_t *p;
    FILE *f;
    int rv;

    if (conflict->skip_pkgname != NULL &&
            strcmp(conflict->skip_pkgname, pkgname) == 0)
        return 0;

    rv = 0;

    f = fopen_contents(pkgname, "r");
    read_plist(&pkg, f);
    (void)fclose(f);

    for (p = pkg.head; p; p = p->next) {
        if (p->type != PLIST_PKGCFL)
            continue;

        if (pkg_match(p->name, conflict->pkgname) == 1) {
            *(conflict->conflicting_pkgname) = xstrdup(pkgname);
            *(conflict->conflicting_pattern) = xstrdup(p->name);
            rv = 1 /* nonzero, stop iterating */;
            break;
        }
    }

    free_plist(&pkg);
    return rv;
}
static
void linear_delete(Mindex mdx, Term t)
{
  Plist curr, prev;
  prev = NULL;
  curr = mdx->linear_first;
  while (curr != NULL && curr->v != t) {
    prev = curr;
    curr = curr->next;
  }
  if (curr == NULL) {
    fprint_term(stderr, t);
    fprintf(stderr, "\n");
    fatal_error("mindex_delete (linear), term not found.");
  }
  else {
    if (prev != NULL)
      prev->next = curr->next;
    else
      mdx->linear_first = curr->next;
    if (curr == mdx->linear_last)
      mdx->linear_last = prev;
    free_plist(curr);
  }
}  /* linear_delete */
示例#3
0
文件: main.c 项目: petabi/pkgsrc
static int
add_depends_of(const char *pkgname, void *cookie)
{
    FILE *fp;
    plist_t *p;
    package_t plist;
    char *path;

    path = pkgdb_pkg_file(pkgname, CONTENTS_FNAME);
    if ((fp = fopen(path, "r")) == NULL)
        errx(EXIT_FAILURE, "Cannot read %s of package %s",
             CONTENTS_FNAME, pkgname);
    free(path);
    read_plist(&plist, fp);
    fclose(fp);

    for (p = plist.head; p; p = p->next) {
        if (p->type == PLIST_PKGDEP)
            add_required_by(p->name, pkgname);
    }

    free_plist(&plist);

    return 0;
}
/* PUBLIC */
void zap_plist_of_terms(Plist lst)
{
  Plist p = lst;
  while (p != NULL) {
    Plist p2 = p;
    p = p->next;
    zap_term(p2->v);
    free_plist(p2);
  }
}  /* zap_plist_of_terms */
/* PUBLIC */
void zap_formula_list(Plist lst)
{
  Plist p = lst;
  while (p != NULL) {
    Plist p2 = p;
    p = p->next;
    zap_formula(p2->v);
    free_plist(p2);
  }
}  /* zap_formula_list */
示例#6
0
int main(int argc, char *argv[]){
  plist_info plist;
  int        snapshot;
  char       filename_in[256];
  char       filename_out[256];

  SID_init(&argc,&argv,NULL,NULL);

  /**********************/
  /* Parse command line */
  /**********************/
  if(argc!=2){
    fprintf(stderr,"\n syntax: %s gadget_file\n",argv[0]);
    fprintf(stderr," ------\n\n");
    return(ERROR_SYNTAX);
  }
  else{
    strcpy(filename_in, argv[1]);
    snapshot=atoi(argv[2]);
    strcpy(filename_out,argv[3]);
    strcat(filename_out,".csv");
  }

  SID_log("Converting GADGET file to .csv...",SID_LOG_OPEN|SID_LOG_TIMER,filename_in,filename_out);

  /****************************************/
  /* Read GADGET file into data structure */
  /****************************************/
  init_plist(&plist,NULL,GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY);
  SID_log("Reading GADGET file {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_in);
  read_gadget_binary(filename_in,snapshot,&plist,READ_GADGET_DEFAULT);
  SID_log("Done.",SID_LOG_CLOSE);

  /********************/
  /* Write ascii file */
  /********************/
  SID_log("Writing .csv file {%s}...",SID_LOG_OPEN|SID_LOG_TIMER,filename_out);
  write_gadget_csv(filename_out,&plist);
  SID_log("Done.",SID_LOG_CLOSE);

  /************/
  /* Clean-up */
  /************/
  free_plist(&plist);

  SID_log("Done.",SID_LOG_CLOSE);

  return(ERROR_NONE);
}
示例#7
0
static int
create_from_installed(const char *ipkg, const char *pkg, const char *suf)
{
    FILE *fp;
    Package plist;
    char homedir[MAXPATHLEN], log_dir[FILENAME_MAX];

    snprintf(log_dir, sizeof(log_dir), "%s/%s", LOG_DIR, ipkg);
    if (!fexists(log_dir)) {
	warnx("can't find package '%s' installed!", ipkg);
	return FALSE;
    }
    getcwd(homedir, sizeof(homedir));
    if (chdir(log_dir) == FAIL) {
	warnx("can't change directory to '%s'!", log_dir);
	return FALSE;
    }
    /* Suck in the contents list */
    plist.head = plist.tail = NULL;
    fp = fopen(CONTENTS_FNAME, "r");
    if (!fp) {
	warnx("unable to open %s file", CONTENTS_FNAME);
	return FALSE;
    }
    read_plist(&plist, fp);
    fclose(fp);

    Install = isfile(INSTALL_FNAME) ? (char *)INSTALL_FNAME : NULL;
    PostInstall = isfile(POST_INSTALL_FNAME) ?
	(char *)POST_INSTALL_FNAME : NULL;
    DeInstall = isfile(DEINSTALL_FNAME) ? (char *)DEINSTALL_FNAME : NULL;
    PostDeInstall = isfile(POST_DEINSTALL_FNAME) ?
	(char *)POST_DEINSTALL_FNAME : NULL;
    Require = isfile(REQUIRE_FNAME) ? (char *)REQUIRE_FNAME : NULL;
    Display = isfile(DISPLAY_FNAME) ? (char *)DISPLAY_FNAME : NULL;
    Mtree = isfile(MTREE_FNAME) ?  (char *)MTREE_FNAME : NULL;

    make_dist(homedir, pkg, suf, &plist);

    free_plist(&plist);
    if (chdir(homedir) == FAIL) {
	warnx("can't change directory to '%s'!", homedir);
	return FALSE;
    }
    return TRUE;
}
static
Plist get_sos_limit_pickers(Plist pickers)
{
  if (pickers == NULL)
    return NULL;
  else {
    Plist rest = get_sos_limit_pickers(pickers->next);
    Picker p = pickers->v;
    if (p->use_for_sos_limit) {
      pickers->next = rest;
      return(pickers);
    }
    else {
      free_plist(pickers);
      return rest;
    }
  }
}  /* get_sos_limit_pickers */
示例#9
0
static int
create_from_installed_recursive(const char *pkg, const char *suf)
{
    FILE *fp;
    Package plist;
    PackingList p;
    char tmp[PATH_MAX];
    int rval;

    if (!create_from_installed(InstalledPkg, pkg, suf))
	return FALSE;
    snprintf(tmp, sizeof(tmp), "%s/%s/%s", LOG_DIR, InstalledPkg, CONTENTS_FNAME);
    if (!fexists(tmp)) {
	warnx("can't find package '%s' installed!", InstalledPkg);
	return FALSE;
    }
    /* Suck in the contents list */
    plist.head = plist.tail = NULL;
    fp = fopen(tmp, "r");
    if (!fp) {
	warnx("unable to open %s file", tmp);
	return FALSE;
    }
    read_plist(&plist, fp);
    fclose(fp);
    rval = TRUE;
    for (p = plist.head; p ; p = p->next) {
	if (p->type != PLIST_PKGDEP)
	    continue;
	if (Verbose)
	    printf("Creating package %s\n", p->name);
	if (!create_from_installed(p->name, p->name, suf)) {
	    rval = FALSE;
	    break;
	}
    }
    free_plist(&plist);
    return rval;
}
示例#10
0
int main(int argc, char *argv[]) {
    SID_Init(&argc, &argv, NULL);

    // Parse command line
    select_gadget_volume_params_info select_gadget_volume_params;
    int                              snapshot;
    int                              n_files_out;
    int                              select_mode;
    char                             filename_in_root[SID_MAX_FILENAME_LENGTH];
    char                             filename_out_root[SID_MAX_FILENAME_LENGTH];
    GBPREAL                          cen_select[3];
    GBPREAL                          select_size;
    strcpy(filename_in_root, argv[1]);
    snapshot                           = atoi(argv[2]);
    select_gadget_volume_params.cen[0] = (GBPREAL)atof(argv[3]);
    select_gadget_volume_params.cen[1] = (GBPREAL)atof(argv[4]);
    select_gadget_volume_params.cen[2] = (GBPREAL)atof(argv[5]);
    select_gadget_volume_params.size   = (GBPREAL)atof(argv[6]);
    strcpy(filename_out_root, argv[7]);
    n_files_out = atoi(argv[8]);
    select_mode = atoi(argv[9]);

    // Check that the selection mode is valid and set function pointer
    int (*select_function)(
        gadget_read_info * fp_gadget, void *params, size_t i_particle, size_t i_particle_type, int i_type, GBPREAL *pos, GBPREAL *vel, size_t ID_i);
    if(select_mode == 1)
        select_function = select_gadget_cube;
    else if(select_mode == 2) {
        select_function                   = select_gadget_sphere;
        select_gadget_volume_params.size2 = pow(select_gadget_volume_params.size, 2.);
    } else
        SID_exit_error("Invalid selection mode (%d) given.", SID_ERROR_SYNTAX, select_mode);

    SID_log("Excising volume from Gadget binary file {%s;snapshot=%d}...", SID_LOG_OPEN | SID_LOG_TIMER, filename_in_root, snapshot);

    // Initialize the plist data structure
    plist_info plist;
    select_gadget_volume_params.plist = &plist;
    init_plist(&plist, NULL, GADGET_LENGTH, GADGET_MASS, GADGET_VELOCITY);

    // Read the header and determine the input file-format
    gadget_read_info   fp_gadget;
    int                flag_filefound = init_gadget_read(filename_in_root, snapshot, &fp_gadget);
    int                flag_multifile = fp_gadget.flag_multifile;
    int                flag_file_type = fp_gadget.flag_file_type;
    gadget_header_info header         = fp_gadget.header;
    if(!flag_filefound)
        SID_exit_error("File not found.", SID_ERROR_LOGIC);
    select_gadget_volume_params.box_size = fp_gadget.header.box_size;

    // Count the particles
    size_t n_particles_type_local[N_GADGET_TYPE];
    size_t n_particles_type[N_GADGET_TYPE];
    int    flag_long_IDs;
    process_gadget_file("Counting particles in selection...",
                        filename_in_root,
                        snapshot,
                        select_function,
                        process_gadget_file_fctn_null,
                        &select_gadget_volume_params,
                        n_particles_type_local,
                        n_particles_type,
                        &flag_long_IDs,
                        PROCESS_GADGET_BINARY_DEFAULT);

    // Allocate RAM for the particles
    allocate_gadget_particles(&plist, n_particles_type_local, n_particles_type, flag_long_IDs);

    // Read the particles
    process_gadget_file("Performing read/select/write...",
                        filename_in_root,
                        snapshot,
                        select_function,
                        store_gadget_particles,
                        &select_gadget_volume_params,
                        NULL,
                        NULL,
                        &flag_long_IDs,
                        PROCESS_GADGET_BINARY_DEFAULT);

    // Write the snapshot
    char filename_out[SID_MAX_FILENAME_LENGTH];
    sprintf(filename_out, "%s_%03d", filename_out_root, snapshot);
    write_gadget_binary_new(&plist, filename_out, n_files_out, WRITE_GADGET_BINARY_DEFAULT);

    // Clean-up
    free_plist(&plist);

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
示例#11
0
文件: perform.c 项目: ryo/netbsd-src
int
pkg_perform(const char *pkg)
{
    char   *cp;
    FILE   *pkg_in;
    package_t plist;
    const char *full_pkg, *suffix;
    char *allocated_pkg;
    int retval;

    /* Break the package name into base and desired suffix (if any) */
    if ((cp = strrchr(pkg, '.')) != NULL) {
        allocated_pkg = xmalloc(cp - pkg + 1);
        memcpy(allocated_pkg, pkg, cp - pkg);
        allocated_pkg[cp - pkg] = '\0';
        suffix = cp + 1;
        full_pkg = pkg;
        pkg = allocated_pkg;
    } else {
        allocated_pkg = NULL;
        full_pkg = pkg;
        suffix = "tgz";
    }

    /* Preliminary setup */
    sanity_check();
    if (Verbose && !PlistOnly)
        printf("Creating package %s\n", pkg);
    get_dash_string(&Comment);
    get_dash_string(&Desc);
    if (IS_STDIN(Contents))
        pkg_in = stdin;
    else {
        pkg_in = fopen(Contents, "r");
        if (!pkg_in)
            errx(2, "unable to open contents file '%s' for input", Contents);
    }

    plist.head = plist.tail = NULL;

    /* Stick the dependencies, if any, at the top */
    if (Pkgdeps)
        register_depends(&plist, Pkgdeps, 0);

    /*
     * Put the build dependencies after the dependencies.
     * This works due to the evaluation order in pkg_add.
     */
    if (BuildPkgdeps)
        register_depends(&plist, BuildPkgdeps, 1);

    /* Put the conflicts directly after the dependencies, if any */
    if (Pkgcfl) {
        if (Verbose && !PlistOnly)
            printf("Registering conflicts:");
        while (Pkgcfl) {
            cp = strsep(&Pkgcfl, " \t\n");
            if (*cp) {
                add_plist(&plist, PLIST_PKGCFL, cp);
                if (Verbose && !PlistOnly)
                    printf(" %s", cp);
            }
        }
        if (Verbose && !PlistOnly)
            printf(".\n");
    }

    /* Slurp in the packing list */
    append_plist(&plist, pkg_in);

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

    /* Prefix should override the packing list */
    if (Prefix) {
        delete_plist(&plist, FALSE, PLIST_CWD, NULL);
        add_plist_top(&plist, PLIST_CWD, Prefix);
    }
    /*
         * Run down the list and see if we've named it, if not stick in a name
         * at the top.
         */
    if (find_plist(&plist, PLIST_NAME) == NULL) {
        add_plist_top(&plist, PLIST_NAME, basename_of(pkg));
    }

    /* Make first "real contents" pass over it */
    check_list(&plist, basename_of(pkg));

    /*
         * We're just here for to dump out a revised plist for the FreeBSD ports
         * hack.  It's not a real create in progress.
         */
    if (PlistOnly) {
        write_plist(&plist, stdout, realprefix);
        retval = TRUE;
    } else {
#ifdef BOOTSTRAP
        warnx("Package building is not supported in bootstrap mode");
        retval = FALSE;
#else
        retval = pkg_build(pkg, full_pkg, suffix, &plist);
#endif
    }

    /* Cleanup */
    free(Comment);
    free(Desc);
    free_plist(&plist);

    free(allocated_pkg);

    return retval;
}
示例#12
0
int main(int argc, char *argv[]) {
    SID_Init(&argc, &argv, NULL);

    // Parse command line
    select_gadget_ids_params_info params;
    int                           snapshot;
    int                           halo_index;
    int                           halo_type;
    int                           n_files_out;
    int                           select_mode;
    char                          filename_SSimPL_root[SID_MAX_FILENAME_LENGTH];
    char                          filename_halo_version[SID_MAX_FILENAME_LENGTH];
    char                          filename_in_root[SID_MAX_FILENAME_LENGTH];
    char                          filename_out_root[SID_MAX_FILENAME_LENGTH];
    GBPREAL                       cen_select[3];
    GBPREAL                       select_size;
    strcpy(filename_SSimPL_root, argv[1]);
    strcpy(filename_halo_version, argv[2]);
    snapshot   = atoi(argv[3]);
    halo_index = atoi(argv[4]);
    halo_type  = atoi(argv[5]);
    strcpy(filename_out_root, argv[6]);
    n_files_out = atoi(argv[7]);

    SID_log("Writing halo particles to ascii file {%s;snapshot=%d;halo_index=%d}...",
            SID_LOG_OPEN | SID_LOG_TIMER,
            filename_SSimPL_root,
            snapshot,
            halo_index);

    // Initialize the plist data structure
    plist_info plist;
    params.plist = &plist;
    init_plist(&plist, NULL, GADGET_LENGTH, GADGET_MASS, GADGET_VELOCITY);

    // Read the halo ID list.  Generate sort indicies and copy the
    //    list to a duplicate array.  Make sure this is the one added
    //    to params.  Pass this to write_gadget_ascii below, so that
    //    the particles get written in the same order that they are
    //    in the halo catalog.

    char filename_halos[SID_MAX_FILENAME_LENGTH];
    if(halo_type == 0)
        sprintf(filename_halos, "%s/halos/%s_%03d.catalog_groups", filename_SSimPL_root, filename_halo_version, snapshot);
    else
        sprintf(filename_halos, "%s/halos/%s_%03d.catalog_subgroups", filename_SSimPL_root, filename_halo_version, snapshot);
    FILE * fp_groups = fopen(filename_halos, "r");
    int    n_groups;
    int    offset_size;
    int    halo_length;
    size_t halo_offset;
    SID_fread_verify(&n_groups, sizeof(int), 1, fp_groups);
    SID_fread_verify(&offset_size, sizeof(int), 1, fp_groups);
    fseeko(fp_groups, (off_t)(2 * sizeof(int) + halo_index * sizeof(int)), SEEK_SET);
    SID_fread_verify(&halo_length, sizeof(int), 1, fp_groups);
    fseeko(fp_groups, (off_t)(2 * sizeof(int) + n_groups * sizeof(int) + halo_index * offset_size), SEEK_SET);
    if(offset_size == sizeof(int)) {
        int halo_offset_i;
        SID_fread_verify(&halo_offset_i, offset_size, 1, fp_groups);
        halo_offset = (size_t)halo_offset_i;
    } else
        SID_fread_verify(&halo_offset, offset_size, 1, fp_groups);
    fclose(fp_groups);

    char filename_ids[SID_MAX_FILENAME_LENGTH];
    sprintf(filename_ids, "%s/halos/%s_%03d.catalog_particles", filename_SSimPL_root, filename_halo_version, snapshot);
    FILE * fp_ids = fopen(filename_ids, "r");
    int    id_byte_size;
    size_t n_ids;
    SID_fread_verify(&id_byte_size, sizeof(int), 1, fp_ids);
    SID_log("%d %d-byte IDs to be read (offset=%d)", SID_LOG_COMMENT, halo_length, id_byte_size, halo_offset);
    if(id_byte_size == sizeof(int)) {
        int n_ids_i;
        SID_fread_verify(&n_ids_i, sizeof(int), 1, fp_ids);
        n_ids = (size_t)n_ids_i;
    } else
        SID_fread_verify(&n_ids, sizeof(size_t), 1, fp_ids);
    fseeko(fp_ids, (off_t)(sizeof(int) + id_byte_size + halo_offset * id_byte_size), SEEK_SET);
    params.n_ids             = halo_length;
    params.id_list           = (size_t *)SID_malloc(sizeof(size_t) * halo_length);
    size_t *id_list_unsorted = (size_t *)SID_malloc(sizeof(size_t) * halo_length);
    int     flag_long_ids    = GBP_TRUE;
    if(id_byte_size == sizeof(int)) {
        flag_long_ids  = GBP_FALSE;
        int *id_list_i = (int *)SID_malloc(sizeof(int) * halo_length);
        SID_fread_verify(id_list_i, id_byte_size, halo_length, fp_ids);
        for(int i_p = 0; i_p < halo_length; i_p++)
            id_list_unsorted[i_p] = (size_t)id_list_i[i_p];
        SID_free(SID_FARG id_list_i);
    } else
        SID_fread_verify(id_list_unsorted, id_byte_size, halo_length, fp_ids);
    fclose(fp_ids);
    memcpy(params.id_list, id_list_unsorted, sizeof(size_t) * halo_length);
    merge_sort(params.id_list, halo_length, NULL, SID_SIZE_T, SORT_INPLACE_ONLY, SORT_COMPUTE_INPLACE);

    // Count the particles
    size_t n_particles_type_local[N_GADGET_TYPE];
    size_t n_particles_type[N_GADGET_TYPE];
    int    flag_long_IDs;
    sprintf(filename_in_root, "%s/snapshots/snapshot", filename_SSimPL_root);
    process_gadget_file("Counting particles in selection...",
                        filename_in_root,
                        snapshot,
                        select_gadget_all,
                        process_gadget_file_fctn_null,
                        &params,
                        n_particles_type_local,
                        n_particles_type,
                        &flag_long_IDs,
                        PROCESS_GADGET_BINARY_DEFAULT);

    // Allocate RAM for the particles
    allocate_gadget_particles(&plist, n_particles_type_local, n_particles_type, flag_long_IDs);

    // Read the particles
    process_gadget_file("Performing read...",
                        filename_in_root,
                        snapshot,
                        select_gadget_all,
                        store_gadget_particles,
                        &params,
                        NULL,
                        NULL,
                        &flag_long_IDs,
                        PROCESS_GADGET_BINARY_DEFAULT);

    // Write the snapshot
    if(SID.I_am_Master) {
        char filename_out[SID_MAX_FILENAME_LENGTH];
        sprintf(filename_out, "%s_%03d_%08d.ascii", filename_out_root, snapshot, halo_index);
        FILE *fp = fopen(filename_out, "w");
        fprintf(fp, "#Columns:\n");
        fprintf(fp, "#  1) Gadget particle type\n");
        fprintf(fp, "#  2) x   [Mpc/h]\n");
        fprintf(fp, "#  3) y   [Mpc/h]\n");
        fprintf(fp, "#  4) z   [Mpc/h]\n");
        fprintf(fp, "#  5) v_x [km/s]\n");
        fprintf(fp, "#  6) v_y [km/s]\n");
        fprintf(fp, "#  7) v_z [km/s]\n");
        fprintf(fp, "#  8) id\n");
        int      i_species  = GADGET_TYPE_DARK;
        size_t   n_p        = ((size_t *)ADaPS_fetch(plist.data, "n_%s", plist.species[i_species]))[0];
        GBPREAL *x          = (GBPREAL *)ADaPS_fetch(plist.data, "x_%s", plist.species[i_species]);
        GBPREAL *y          = (GBPREAL *)ADaPS_fetch(plist.data, "y_%s", plist.species[i_species]);
        GBPREAL *z          = (GBPREAL *)ADaPS_fetch(plist.data, "z_%s", plist.species[i_species]);
        GBPREAL *vx         = (GBPREAL *)ADaPS_fetch(plist.data, "vx_%s", plist.species[i_species]);
        GBPREAL *vy         = (GBPREAL *)ADaPS_fetch(plist.data, "vy_%s", plist.species[i_species]);
        GBPREAL *vz         = (GBPREAL *)ADaPS_fetch(plist.data, "vz_%s", plist.species[i_species]);
        size_t * id         = (size_t *)ADaPS_fetch(plist.data, "id_%s", plist.species[i_species]);
        size_t * id_indices = NULL;
        SID_log("Sorting IDs...", SID_LOG_OPEN);
        merge_sort(id, n_p, &id_indices, SID_SIZE_T, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);
        SID_log("Done.", SID_LOG_CLOSE);
        SID_log("Writing particles...", SID_LOG_OPEN);
        pcounter_info pcounter;
        SID_Init_pcounter(&pcounter, halo_length, 10);
        int n_unfound = 0;
        for(int i_p = 0; i_p < halo_length; i_p++) {
            size_t k_p = id_indices[find_index(id, id_list_unsorted[i_p], n_p, id_indices)];
            if(id[k_p] != id_list_unsorted[i_p])
                n_unfound++;
            else
                fprintf(fp,
                        "%1d %11.4e %11.4e %11.4e %11.4e %11.4e %11.4e %7zd\n",
                        i_species,
                        (double)(x[k_p]),
                        (double)(y[k_p]),
                        (double)(z[k_p]),
                        (double)(vx[k_p]),
                        (double)(vy[k_p]),
                        (double)(vz[k_p]),
                        id[k_p]);
            SID_check_pcounter(&pcounter, i_p);
        }
        fclose(fp);
        if(n_unfound != 0)
            SID_log("(%d unfound)...", SID_LOG_CONTINUE, n_unfound);
        SID_log("Done.", SID_LOG_CLOSE);
        SID_free(SID_FARG id_indices);
    }

    // Clean-up
    SID_free(SID_FARG id_list_unsorted);
    SID_free(SID_FARG params.id_list);
    free_plist(&plist);

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
示例#13
0
/*
 * This is seriously ugly code following.  Written very fast!
 * [And subsequently made even worse..  Sigh!  This code was just born
 * to be hacked, I guess.. :) -jkh]
 */
static int
pkg_do(char *pkg)
{
    Package Plist;
    char pkg_fullname[FILENAME_MAX];
    char playpen[FILENAME_MAX];
    char extract_contents[FILENAME_MAX];
    char *extract;
    const char *where_to;
    FILE *cfile;
    int code;
    PackingList p;
    struct stat sb;
    int inPlace, conflictsfound, errcode;
    /* support for separate pre/post install scripts */
    int new_m = 0;
    char pre_script[FILENAME_MAX] = INSTALL_FNAME;
    char post_script[FILENAME_MAX];
    char pre_arg[FILENAME_MAX], post_arg[FILENAME_MAX];
    char *conflict[2];
    char **matched;
    int fd;

    conflictsfound = 0;
    code = 0;
    zapLogDir = 0;
    LogDir[0] = '\0';
    strcpy(playpen, FirstPen);
    inPlace = 0;

    memset(&Plist, '\0', sizeof(Plist));

    /* Are we coming in for a second pass, everything already extracted? */
    if (!pkg) {
	fgets(playpen, FILENAME_MAX, stdin);
	playpen[strlen(playpen) - 1] = '\0'; /* pesky newline! */
	if (chdir(playpen) == FAIL) {
	    warnx("pkg_add in SLAVE mode can't chdir to %s", playpen);
	    return 1;
	}
	read_plist(&Plist, stdin);
	where_to = playpen;
    }
    /* Nope - do it now */
    else {
	/* Is it an ftp://foo.bar.baz/file.t[bg]z specification? */
	if (isURL(pkg)) {
	    if (!(where_to = fileGetURL(NULL, pkg, KeepPackage))) {
		warnx("unable to fetch '%s' by URL", pkg);
		return 1;
	    }
	    strcpy(pkg_fullname, pkg);
	    cfile = fopen(CONTENTS_FNAME, "r");
	    if (!cfile) {
		warnx(
		"unable to open table of contents file '%s' - not a package?",
		CONTENTS_FNAME);
		goto bomb;
	    }
	    read_plist(&Plist, cfile);
	    fclose(cfile);
	}
	else {
	    strcpy(pkg_fullname, pkg);		/*
						 * Copy for sanity's sake,
						 * could remove pkg_fullname
						 */
	    if (strcmp(pkg, "-")) {
		if (stat(pkg_fullname, &sb) == FAIL) {
		    warnx("can't stat package file '%s'", pkg_fullname);
		    goto bomb;
		}
		sprintf(extract_contents, "--fast-read %s", CONTENTS_FNAME);
		extract = extract_contents;
	    }
	    else {
		extract = NULL;
		sb.st_size = 100000;	/* Make up a plausible average size */
	    }
	    if (!(where_to = make_playpen(playpen, sb.st_size * 4)))
		errx(1, "unable to make playpen for %lld bytes", (long long)sb.st_size * 4);
	    /* Since we can call ourselves recursively, keep notes on where we came from */
	    if (!getenv("_TOP"))
		setenv("_TOP", where_to, 1);
	    if (unpack(pkg_fullname, extract)) {
		warnx(
	"unable to extract table of contents file from '%s' - not a package?",
		pkg_fullname);
		goto bomb;
	    }
	    cfile = fopen(CONTENTS_FNAME, "r");
	    if (!cfile) {
		warnx(
	"unable to open table of contents file '%s' - not a package?",
		CONTENTS_FNAME);
		goto bomb;
	    }
	    read_plist(&Plist, cfile);
	    fclose(cfile);

	    /* Extract directly rather than moving?  Oh goodie! */
	    if (find_plist_option(&Plist, "extract-in-place")) {
		if (Verbose)
		    printf("Doing in-place extraction for %s\n", pkg_fullname);
		p = find_plist(&Plist, PLIST_CWD);
		if (p) {
		    if (!isdir(p->name) && !Fake) {
			if (Verbose)
			    printf("Desired prefix of %s does not exist, creating..\n", p->name);
			vsystem("/bin/mkdir -p %s", p->name);
			if (chdir(p->name) == -1) {
			    warn("unable to change directory to '%s'", p->name);
			    goto bomb;
			}
		    }
		    where_to = p->name;
		    inPlace = 1;
		}
		else {
		    warnx(
		"no prefix specified in '%s' - this is a bad package!",
			pkg_fullname);
		    goto bomb;
		}
	    }

	    /*
	     * Apply a crude heuristic to see how much space the package will
	     * take up once it's unpacked.  I've noticed that most packages
	     * compress an average of 75%, so multiply by 4 for good measure.
	     */

	    if (!extract && !inPlace && min_free(playpen) < sb.st_size * 4) {
		warnx("projected size of %lld exceeds available free space.\n"
"Please set your PKG_TMPDIR variable to point to a location with more\n"
		       "free space and try again", (long long)sb.st_size * 4);
		warnx("not extracting %s\ninto %s, sorry!",
			pkg_fullname, where_to);
		goto bomb;
	    }

	    /* If this is a direct extract and we didn't want it, stop now */
	    if (inPlace && Fake)
		goto success;

	    /* Finally unpack the whole mess.  If extract is null we
	       already + did so so don't bother doing it again. */
	    if (extract && unpack(pkg_fullname, NULL)) {
		warnx("unable to extract '%s'!", pkg_fullname);
		goto bomb;
	    }
	}

	/* Check for sanity and dependencies */
	if (sanity_check(pkg))
	    goto bomb;

	/* If we're running in MASTER mode, just output the plist and return */
	if (AddMode == MASTER) {
	    printf("%s\n", where_playpen());
	    write_plist(&Plist, stdout);
	    return 0;
	}
    }

    /*
     * If we have a prefix, delete the first one we see and add this
     * one in place of it.
     */
    if (Prefix) {
	delete_plist(&Plist, FALSE, PLIST_CWD, NULL);
	add_plist_top(&Plist, PLIST_CWD, Prefix);
    }

    setenv(PKG_PREFIX_VNAME, (p = find_plist(&Plist, PLIST_CWD)) ? p->name : ".", 1);
    /* Protect against old packages with bogus @name and origin fields */
    if (Plist.name == NULL)
	Plist.name = "anonymous";
    if (Plist.origin == NULL)
	Plist.origin = "anonymous/anonymous";

    /*
     * See if we're already registered either with the same name (the same
     * version) or some other version with the same origin.
     */
    if ((isinstalledpkg(Plist.name) > 0 ||
         matchbyorigin(Plist.origin, NULL) != NULL) && !Force) {
	warnx("package '%s' or its older version already installed%s",
	      Plist.name, FailOnAlreadyInstalled ? "" : " (ignored)");
	code = FailOnAlreadyInstalled != FALSE;
	goto success;	/* close enough for government work */
    }

    /* Now check the packing list for conflicts */
    if (!IgnoreDeps){
    for (p = Plist.head; p != NULL; p = p->next) {
	if (p->type == PLIST_CONFLICTS) {
	    int i;
	    conflict[0] = strdup(p->name);
	    conflict[1] = NULL;
	    matched = matchinstalled(MATCH_GLOB, conflict, &errcode);
	    free(conflict[0]);
	    if (errcode == 0 && matched != NULL)
		for (i = 0; matched[i] != NULL; i++)
		    if (isinstalledpkg(matched[i]) > 0) {
			warnx("package '%s' conflicts with %s", Plist.name,
				matched[i]);
			conflictsfound = 1;
		    }

	    continue;
	}
    }
    if(conflictsfound) {
	if(!Force) {
	    warnx("please use pkg_delete first to remove conflicting package(s) or -f to force installation");
	    code = 1;
	    goto bomb;
	} else
	    warnx("-f specified; proceeding anyway");
    }

#if ENSURE_THAT_ALL_REQUIREMENTS_ARE_MET
    /*
     * Before attempting to do the slave mode bit, ensure that we've
     * downloaded & processed everything we need.
     * It's possible that we haven't already installed all of our
     * dependencies if the dependency list was misgenerated due to
     * other dynamic dependencies or if a dependency was added to a
     * package without all REQUIRED_BY packages being regenerated.
     */
    for (p = pkg ? Plist.head : NULL; p; p = p->next) {
	const char *ext;
	char *deporigin;

	if (p->type != PLIST_PKGDEP)
	    continue;
	deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;

	if (isinstalledpkg(p->name) <= 0 &&
	    !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
	    char subpkg[FILENAME_MAX], *sep;

	    strlcpy(subpkg, pkg, sizeof subpkg);
	    if ((sep = strrchr(subpkg, '/')) != NULL) {
		*sep = '\0';
		if ((sep = strrchr(subpkg, '/')) != NULL) {
		    *sep = '\0';
		    strlcat(subpkg, "/All/", sizeof subpkg);
		    strlcat(subpkg, p->name, sizeof subpkg);
		    if ((ext = strrchr(pkg, '.')) == NULL) {
			if (getenv("PACKAGESUFFIX"))
			  ext = getenv("PACKAGESUFFIX");
			else
			  ext = ".tbz";
		    }
		    strlcat(subpkg, ext, sizeof subpkg);
		    pkg_do(subpkg);
		}
	    }
	}
    }
#endif

    /* Now check the packing list for dependencies */
    for (p = Plist.head; p ; p = p->next) {
	char *deporigin;

	if (p->type != PLIST_PKGDEP)
	    continue;
	deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name : NULL;
	if (Verbose) {
	    printf("Package '%s' depends on '%s'", Plist.name, p->name);
	    if (deporigin != NULL)
		printf(" with '%s' origin", deporigin);
	    printf(".\n");
	}
	if (isinstalledpkg(p->name) <= 0 &&
	    !(deporigin != NULL && matchbyorigin(deporigin, NULL) != NULL)) {
	    char path[FILENAME_MAX];
	    const char *cp = NULL;

	    if (!Fake) {
		char prefixArg[2 + MAXPATHLEN]; /* "-P" + Prefix */
		if (PrefixRecursive) {
		    strlcpy(prefixArg, "-P", sizeof(prefixArg));
		    strlcat(prefixArg, Prefix, sizeof(prefixArg));
		}
		if (!isURL(pkg) && !getenv("PKG_ADD_BASE")) {
		    const char *ext;

		    ext = strrchr(pkg_fullname, '.');
		    if (ext == NULL) {
			if (getenv("PACKAGESUFFIX")) {
			  ext = getenv("PACKAGESUFFIX");
			} else {
			  ext = ".tbz";
			}
		    }
		    snprintf(path, FILENAME_MAX, "%s/%s%s", getenv("_TOP"), p->name, ext);
		    if (fexists(path))
			cp = path;
		    else
			cp = fileFindByPath(pkg, p->name);
		    if (cp) {
			if (Verbose)
			    printf("Loading it from %s.\n", cp);
			if (vsystem("%s %s %s '%s'", PkgAddCmd, Verbose ? "-v " : "", PrefixRecursive ? prefixArg : "", cp)) {
			    warnx("autoload of dependency '%s' failed%s",
				cp, Force ? " (proceeding anyway)" : "!");
			    if (!Force)
				++code;
			}
		    }
		    else {
			warnx("could not find package %s %s",
			      p->name, Force ? " (proceeding anyway)" : "!");
			if (!Force)
			    ++code;
		    }
		}
		else if ((cp = fileGetURL(pkg, p->name, KeepPackage)) != NULL) {
		    if (Verbose)
			printf("Finished loading %s via a URL\n", p->name);
		    if (!fexists("+CONTENTS")) {
			warnx("autoloaded package %s has no +CONTENTS file?",
				p->name);
			if (!Force)
			    ++code;
		    }
		    else if (vsystem("(pwd; /bin/cat +CONTENTS) | %s %s %s %s -S", PkgAddCmd, Verbose ? "-v" : "", PrefixRecursive ? prefixArg : "", KeepPackage ? "-K" : "")) {
			warnx("pkg_add of dependency '%s' failed%s",
				p->name, Force ? " (proceeding anyway)" : "!");
			if (!Force)
			    ++code;
		    }
		    else if (Verbose)
			printf("\t'%s' loaded successfully.\n", p->name);
		    /* Nuke the temporary playpen */
		    leave_playpen();
		}
	    }
	    else {
		if (Verbose)
		    printf("and was not found%s.\n", Force ? " (proceeding anyway)" : "");
		else
		    printf("Package dependency %s for %s not found%s\n", p->name, pkg,
			   Force ? " (proceeding anyway)" : "!");
		if (!Force)
		    ++code;
	    }
	}
	else if (Verbose)
	    printf(" - already installed.\n");
    }
    } /* if (!IgnoreDeps) */

    if (code != 0)
	goto bomb;

    /* Look for the requirements file */
    if ((fd = open(REQUIRE_FNAME, O_RDWR)) != -1) {
	fstat(fd, &sb);
	fchmod(fd, sb.st_mode | S_IXALL);	/* be sure, chmod a+x */
	close(fd);
	if (Verbose)
	    printf("Running requirements file first for %s..\n", Plist.name);
	if (!Fake && vsystem("./%s %s INSTALL", REQUIRE_FNAME, Plist.name)) {
	    warnx("package %s fails requirements %s", pkg_fullname,
		   Force ? "installing anyway" : "- not installed");
	    if (!Force) {
		code = 1;
		goto success;	/* close enough for government work */
	    }
	}
    }

    /*
     * Test whether to use the old method of passing tokens to installation
     * scripts, and set appropriate variables..
     */

    if (fexists(POST_INSTALL_FNAME)) {
	new_m = 1;
	sprintf(post_script, "%s", POST_INSTALL_FNAME);
	pre_arg[0] = '\0';
	post_arg[0] = '\0';
    } else {
	if (fexists(INSTALL_FNAME)) {
	    sprintf(post_script, "%s", INSTALL_FNAME);
	    sprintf(pre_arg, "PRE-INSTALL");
	    sprintf(post_arg, "POST-INSTALL");
	}
    }

    /* If we're really installing, and have an installation file, run it */
    if (!NoInstall && (fd = open(pre_script, O_RDWR)) != -1) {
	fstat(fd, &sb);
	fchmod(fd, sb.st_mode | S_IXALL);	/* be sure, chmod a+x */
	close(fd);
	if (Verbose)
	    printf("Running pre-install for %s..\n", Plist.name);
	if (!Fake && vsystem("./%s %s %s", pre_script, Plist.name, pre_arg)) {
	    warnx("install script returned error status");
	    unlink(pre_script);
	    code = 1;
	    goto success;		/* nothing to uninstall yet */
	}
    }

    /* Now finally extract the entire show if we're not going direct */
    if (!inPlace && !Fake)
	extract_plist(".", &Plist);

    if (!Fake && fexists(MTREE_FNAME)) {
	if (Verbose)
	    printf("Running mtree for %s..\n", Plist.name);
	p = find_plist(&Plist, PLIST_CWD);
	if (Verbose)
	    printf("mtree -U -f %s -d -e -p %s >%s\n", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL);
	if (!Fake) {
	    if (vsystem("/usr/sbin/mtree -U -f %s -d -e -p %s >%s", MTREE_FNAME, p ? p->name : "/", _PATH_DEVNULL))
		warnx("mtree returned a non-zero status - continuing");
	}
    }

    /* Run the installation script one last time? */
    if (!NoInstall && (fd = open(post_script, O_RDWR)) != -1) {
	fstat(fd, &sb);
	fchmod(fd, sb.st_mode | S_IXALL);	/* be sure, chmod a+x */
	close(fd);
	if (Verbose)
	    printf("Running post-install for %s..\n", Plist.name);
	if (!Fake && vsystem("./%s %s %s", post_script, Plist.name, post_arg)) {
	    warnx("install script returned error status");
	    unlink(post_script);
	    code = 1;
	    goto fail;
	}
    }

    /* Time to record the deed? */
    if (!NoRecord && !Fake) {
	char contents[FILENAME_MAX];
	char **depnames = NULL, **deporigins = NULL, ***depmatches;
	int i, dep_count = 0;
	FILE *contfile;

	if (getuid() != 0)
	    warnx("not running as root - trying to record install anyway");
	sprintf(LogDir, "%s/%s", LOG_DIR, Plist.name);
	zapLogDir = 1;
	if (Verbose)
	    printf("Attempting to record package into %s..\n", LogDir);
	if (make_hierarchy(LogDir, FALSE)) {
	    warnx("can't record package into '%s', you're on your own!",
		   LogDir);
	    bzero(LogDir, FILENAME_MAX);
	    code = 1;
	    goto success;	/* close enough for government work */
	}
	/* Make sure pkg_info can read the entry */
	fd = open(LogDir, O_RDWR);
	fstat(fd, &sb);
	fchmod(fd, sb.st_mode | S_IRALL | S_IXALL);	/* be sure, chmod a+rx */
	close(fd);
	move_file(".", DESC_FNAME, LogDir);
	move_file(".", COMMENT_FNAME, LogDir);
	if (fexists(INSTALL_FNAME))
	    move_file(".", INSTALL_FNAME, LogDir);
	if (fexists(POST_INSTALL_FNAME))
	    move_file(".", POST_INSTALL_FNAME, LogDir);
	if (fexists(DEINSTALL_FNAME))
	    move_file(".", DEINSTALL_FNAME, LogDir);
	if (fexists(POST_DEINSTALL_FNAME))
	    move_file(".", POST_DEINSTALL_FNAME, LogDir);
	if (fexists(REQUIRE_FNAME))
	    move_file(".", REQUIRE_FNAME, LogDir);
	if (fexists(DISPLAY_FNAME))
	    move_file(".", DISPLAY_FNAME, LogDir);
	if (fexists(MTREE_FNAME))
	    move_file(".", MTREE_FNAME, LogDir);
	sprintf(contents, "%s/%s", LogDir, CONTENTS_FNAME);
	contfile = fopen(contents, "w");
	if (!contfile) {
	    warnx("can't open new contents file '%s'! can't register pkg",
		contents);
	    goto success; /* can't log, but still keep pkg */
	}
	write_plist(&Plist, contfile);
	fclose(contfile);
	for (p = Plist.head; p ; p = p->next) {
	    char *deporigin;

	    if (p->type != PLIST_PKGDEP)
		continue;
	    deporigin = (p->next->type == PLIST_DEPORIGIN) ? p->next->name :
							     NULL;
	    if (Verbose) {
		printf("Trying to record dependency on package '%s'", p->name);
		if (deporigin != NULL)
		    printf(" with '%s' origin", deporigin);
		printf(".\n");
	    }

	    if (deporigin) {
		/* Defer to origin lookup */
		depnames = realloc(depnames, (dep_count + 1) * sizeof(*depnames));
		depnames[dep_count] = p->name;
		deporigins = realloc(deporigins, (dep_count + 2) * sizeof(*deporigins));
		deporigins[dep_count] = deporigin;
		deporigins[dep_count + 1] = NULL;
		dep_count++;
	    } else {
	       /* No origin recorded, try to register on literal package name */
	       sprintf(contents, "%s/%s/%s", LOG_DIR, p->name,
		     REQUIRED_BY_FNAME);
	       contfile = fopen(contents, "a");
	       if (!contfile) {
		  warnx("can't open dependency file '%s'!\n"
			"dependency registration is incomplete", contents);
	       } else {
		  fprintf(contfile, "%s\n", Plist.name);
		  if (fclose(contfile) == EOF) {
		     warnx("cannot properly close file %s", contents);
		  }
	       }
	    }
	}
	if (dep_count > 0) {
	    depmatches = matchallbyorigin((const char **)deporigins, NULL);
	    free(deporigins);
	    if (!IgnoreDeps && depmatches) {
		for (i = 0; i < dep_count; i++) {
		    if (depmatches[i]) {
			int j;
			char **tmp = depmatches[i];
			for (j = 0; tmp[j] != NULL; j++) {
			    /* Origin looked up */
			    sprintf(contents, "%s/%s/%s", LOG_DIR, tmp[j],
				REQUIRED_BY_FNAME);
			    if (depnames[i] && strcmp(depnames[i], tmp[j]) != 0)
				warnx("warning: package '%s' requires '%s', but '%s' "
				    "is installed", Plist.name, depnames[i], tmp[j]);
			    contfile = fopen(contents, "a");
			    if (!contfile) {
				warnx("can't open dependency file '%s'!\n"
				    "dependency registration is incomplete", contents);
			    } else {
				fprintf(contfile, "%s\n", Plist.name);
				if (fclose(contfile) == EOF)
				    warnx("cannot properly close file %s", contents);
			    }
			}
		    } else if (depnames[i]) {
			/* No package present with this origin, try literal package name */
			sprintf(contents, "%s/%s/%s", LOG_DIR, depnames[i],
			    REQUIRED_BY_FNAME);
			contfile = fopen(contents, "a");
			if (!contfile) {
			    warnx("can't open dependency file '%s'!\n"
				"dependency registration is incomplete", contents);
			} else {
			    fprintf(contfile, "%s\n", Plist.name);
			    if (fclose(contfile) == EOF) {
				warnx("cannot properly close file %s", contents);
			    }
			}
		    }
		}
	    }
	}
	if (Verbose)
	    printf("Package %s registered in %s\n", Plist.name, LogDir);
    }

    if ((p = find_plist(&Plist, PLIST_DISPLAY)) != NULL) {
	FILE *fp;
	char buf[BUFSIZ];

	snprintf(buf, sizeof buf, "%s/%s", LogDir, p->name);
	fp = fopen(buf, "r");
	if (fp) {
	    putc('\n', stdout);
	    while (fgets(buf, sizeof(buf), fp))
		fputs(buf, stdout);
	    putc('\n', stdout);
	    (void) fclose(fp);
	} else {
    	    if (!Fake) {
		warnx("cannot open %s as display file", buf);  
	    }
	}
    }

    goto success;

 bomb:
    code = 1;
    goto success;

 fail:
    /* Nuke the whole (installed) show, XXX but don't clean directories */
    if (!Fake)
	delete_package(FALSE, FALSE, &Plist);

 success:
    /* delete the packing list contents */
    free_plist(&Plist);
    leave_playpen();
    return code;
}
示例#14
0
int
pkg_perform(char **pkgs)
{
    static const char *home;
    char *pkg = *pkgs;		/* Only one arg to create */
    char *cp;
    FILE *pkg_in, *fp;
    Package plist;
    int len;
    const char *suf;

    /* Preliminary setup */
    if (InstalledPkg == NULL)
	sanity_check();
    if (Verbose && !PlistOnly)
	printf("Creating package %s\n", pkg);

    /* chop suffix off if already specified, remembering if we want to compress  */
    len = strlen(pkg);
    if (len > 4) {
	if (!strcmp(&pkg[len - 4], ".tbz")) {
	    Zipper = BZIP2;
	    pkg[len - 4] = '\0';
	}
	else if (!strcmp(&pkg[len - 4], ".tgz")) {
	    Zipper = GZIP;
	    pkg[len - 4] = '\0';
	}
	else if (!strcmp(&pkg[len - 4], ".txz")) {
	    Zipper = XZ;
	    pkg[len - 4] = '\0';
	}
	else if (!strcmp(&pkg[len - 4], ".tar")) {
	    Zipper = NONE;
	    pkg[len - 4] = '\0';
	}
    }
    if (Zipper == BZIP2) {
	suf = "tbz";
	setenv("BZIP2", "--best", 0);
    } else if (Zipper == GZIP) {
	suf = "tgz";
	setenv("GZIP", "-9", 0);
    } else if (Zipper == XZ) {
	suf = "txz";
    } else
	suf = "tar";

    if (InstalledPkg != NULL) {
	char *pkgglob[] = { InstalledPkg, NULL };
	char **matched, **pkgs;
	int i, error;

	pkgs = pkgglob;
	if (MatchType != MATCH_EXACT) {
		matched = matchinstalled(MatchType, pkgs, &error);
		if (!error && matched != NULL)
			pkgs = matched;
		else if (MatchType != MATCH_GLOB)
	    		errx(1, "no packages match pattern");
	}
	/*
	 * Is there is only one installed package matching the pattern,
	 * we need to respect the optional pkg-filename parameter.  If,
	 * however, the pattern matches several packages, this parameter
	 * makes no sense and is ignored.
	 */
	if (pkgs[1] == NULL) {
	    if (pkg == InstalledPkg)
		pkg = *pkgs;
	    InstalledPkg = *pkgs;
	    if (!Recursive)
		return (create_from_installed(InstalledPkg, pkg, suf));
	    return (create_from_installed_recursive(pkg, suf));
	}
	for (i = 0; pkgs[i] != NULL; i++) {
	    InstalledPkg = pkg = pkgs[i];
	    if (!Recursive)
		create_from_installed(pkg, pkg, suf);
	    else
	        create_from_installed_recursive(pkg, suf);
	}
	return TRUE;
    }

    get_dash_string(&Comment);
    get_dash_string(&Desc);
    if (!strcmp(Contents, "-"))
	pkg_in = stdin;
    else {
	pkg_in = fopen(Contents, "r");
	if (!pkg_in) {
	    cleanup(0);
	    errx(2, "%s: unable to open contents file '%s' for input",
		__func__, Contents);
	}
    }
    plist.head = plist.tail = NULL;

    /* Stick the dependencies, if any, at the top */
    if (Pkgdeps) {
	char **deps, *deporigin;
	int i;
	int ndeps = 0;

	if (Verbose && !PlistOnly)
	    printf("Registering depends:");

	/* Count number of dependencies */
	for (cp = Pkgdeps; cp != NULL && *cp != '\0';
			   cp = strpbrk(++cp, " \t\n")) {
	    ndeps++;
	}

	if (ndeps != 0) {
	    /* Create easy to use NULL-terminated list */
	    deps = alloca(sizeof(*deps) * ndeps + 1);
	    if (deps == NULL) {
		errx(2, "%s: alloca() failed", __func__);
		/* Not reached */
	    }
	    for (i = 0; Pkgdeps;) {
		cp = strsep(&Pkgdeps, " \t\n");
		if (*cp) {
		    deps[i] = cp;
		    i++;
		}
	    }
	    ndeps = i;
	    deps[ndeps] = NULL;

	    sortdeps(deps);
	    for (i = 0; i < ndeps; i++) {
		deporigin = strchr(deps[i], ':');
		if (deporigin != NULL) {
		    *deporigin = '\0';
		    add_plist_top(&plist, PLIST_DEPORIGIN, ++deporigin);
		}
		add_plist_top(&plist, PLIST_PKGDEP, deps[i]);
		if (Verbose && !PlistOnly)
		    printf(" %s", deps[i]);
	    }
	}

	if (Verbose && !PlistOnly)
	    printf(".\n");
    }

    /* Put the conflicts directly after the dependencies, if any */
    if (Conflicts) {
	if (Verbose && !PlistOnly)
	    printf("Registering conflicts:");
	while (Conflicts) {
	   cp = strsep(&Conflicts, " \t\n");
	   if (*cp) {
		add_plist(&plist, PLIST_CONFLICTS, cp);
		if (Verbose && !PlistOnly)
		    printf(" %s", cp);
	   }
	}
	if (Verbose && !PlistOnly)
	    printf(".\n");
    }

    /* If a SrcDir override is set, add it now */
    if (SrcDir) {
	if (Verbose && !PlistOnly)
	    printf("Using SrcDir value of %s\n", SrcDir);
	add_plist(&plist, PLIST_SRC, SrcDir);
    }

    /* Slurp in the packing list */
    read_plist(&plist, pkg_in);

    /* Prefix should add an @cwd to the packing list */
    if (Prefix) {
        char resolved_prefix[PATH_MAX];
        if (realpath(Prefix, resolved_prefix) == NULL)
	    err(EXIT_FAILURE, "couldn't resolve path for prefix: %s", Prefix);
	add_plist_top(&plist, PLIST_CWD, resolved_prefix);
    }

    /* Add the origin if asked, at the top */
    if (Origin)
	add_plist_top(&plist, PLIST_ORIGIN, Origin);

    /*
     * Run down the list and see if we've named it, if not stick in a name
     * at the top.
     */
    if (find_plist(&plist, PLIST_NAME) == NULL)
	add_plist_top(&plist, PLIST_NAME, basename(pkg));

    if (asprintf(&cp, "PKG_FORMAT_REVISION:%d.%d", PLIST_FMT_VER_MAJOR,
		 PLIST_FMT_VER_MINOR) == -1) {
	errx(2, "%s: asprintf() failed", __func__);
    }
    add_plist_top(&plist, PLIST_COMMENT, cp);
    free(cp);

    /*
     * We're just here for to dump out a revised plist for the FreeBSD ports
     * hack.  It's not a real create in progress.
     */
    if (PlistOnly) {
	check_list(home, &plist);
	write_plist(&plist, stdout);
	exit(0);
    }

    /* Make a directory to stomp around in */
    home = make_playpen(PlayPen, 0);
    signal(SIGINT, cleanup);
    signal(SIGHUP, cleanup);

    /* Make first "real contents" pass over it */
    check_list(home, &plist);
    (void) umask(022);	/*
			 * Make sure gen'ed directories, files don't have
			 * group or other write bits.
			 */
    /* copy_plist(home, &plist); */
    /* mark_plist(&plist); */

    /* Now put the release specific items in */
    if (!Prefix) {
	add_plist(&plist, PLIST_CWD, ".");
    }
    write_file(COMMENT_FNAME, Comment);
    add_plist(&plist, PLIST_IGNORE, NULL);
    add_plist(&plist, PLIST_FILE, COMMENT_FNAME);
    add_cksum(&plist, plist.tail, COMMENT_FNAME);
    write_file(DESC_FNAME, Desc);
    add_plist(&plist, PLIST_IGNORE, NULL);
    add_plist(&plist, PLIST_FILE, DESC_FNAME);
    add_cksum(&plist, plist.tail, DESC_FNAME);

    if (Install) {
	copy_file(home, Install, INSTALL_FNAME);
	add_plist(&plist, PLIST_IGNORE, NULL);
	add_plist(&plist, PLIST_FILE, INSTALL_FNAME);
	add_cksum(&plist, plist.tail, INSTALL_FNAME);
    }
    if (PostInstall) {
	copy_file(home, PostInstall, POST_INSTALL_FNAME);
	add_plist(&plist, PLIST_IGNORE, NULL);
	add_plist(&plist, PLIST_FILE, POST_INSTALL_FNAME);
	add_cksum(&plist, plist.tail, POST_INSTALL_FNAME);
    }
    if (DeInstall) {
	copy_file(home, DeInstall, DEINSTALL_FNAME);
	add_plist(&plist, PLIST_IGNORE, NULL);
	add_plist(&plist, PLIST_FILE, DEINSTALL_FNAME);
	add_cksum(&plist, plist.tail, DEINSTALL_FNAME);
    }
    if (PostDeInstall) {
	copy_file(home, PostDeInstall, POST_DEINSTALL_FNAME);
	add_plist(&plist, PLIST_IGNORE, NULL);
	add_plist(&plist, PLIST_FILE, POST_DEINSTALL_FNAME);
	add_cksum(&plist, plist.tail, POST_DEINSTALL_FNAME);
    }
    if (Require) {
	copy_file(home, Require, REQUIRE_FNAME);
	add_plist(&plist, PLIST_IGNORE, NULL);
	add_plist(&plist, PLIST_FILE, REQUIRE_FNAME);
	add_cksum(&plist, plist.tail, REQUIRE_FNAME);
    }
    if (Display) {
	copy_file(home, Display, DISPLAY_FNAME);
	add_plist(&plist, PLIST_IGNORE, NULL);
	add_plist(&plist, PLIST_FILE, DISPLAY_FNAME);
	add_cksum(&plist, plist.tail, DISPLAY_FNAME);
	add_plist(&plist, PLIST_DISPLAY, DISPLAY_FNAME);
    }
    if (Mtree) {
	copy_file(home, Mtree, MTREE_FNAME);
	add_plist(&plist, PLIST_IGNORE, NULL);
	add_plist(&plist, PLIST_FILE, MTREE_FNAME);
	add_cksum(&plist, plist.tail, MTREE_FNAME);
	add_plist(&plist, PLIST_MTREE, MTREE_FNAME);
    }

    /* Finally, write out the packing list */
    fp = fopen(CONTENTS_FNAME, "w");
    if (!fp) {
	cleanup(0);
	errx(2, "%s: can't open file %s for writing",
	    __func__, CONTENTS_FNAME);
    }
    write_plist(&plist, fp);
    if (fclose(fp)) {
	cleanup(0);
	errx(2, "%s: error while closing %s",
	    __func__, CONTENTS_FNAME);
    }

    /* And stick it into a tar ball */
    make_dist(home, pkg, suf, &plist);

    /* Cleanup */
    free(Comment);
    free(Desc);
    free_plist(&plist);
    leave_playpen();
    return TRUE;	/* Success */
}
示例#15
0
int main(int argc, char *argv[]){
  int     n_species;
  int     n_load;
  int     n_used;
  int     flag_used[N_GADGET_TYPE];
  char    species_name[256];
  double  h_Hubble;
  double  n_spec;
  double  redshift;
  int     i_species;
  char    n_string[64];
  int             n[3];
  double          L[3];
  FILE           *fp_1D;
  FILE           *fp_2D;
  cosmo_info     *cosmo;
  field_info     *field[N_GADGET_TYPE];
  field_info     *field_norm[N_GADGET_TYPE];
  plist_info      plist_header;
  plist_info      plist;
  FILE           *fp;
  int     i_temp;
  int     n_temp;
  double *k_temp;
  double *kmin_temp;
  double *kmax_temp;
  double *P_temp;
  size_t *n_mode_temp;
  double *sigma_P_temp;
  double *shot_noise_temp;
  double *dP_temp;
  int     snapshot_number;
  int     i_compute;
  int     distribution_scheme;
  double  k_min_1D;
  double  k_max_1D;
  double  k_min_2D;
  double  k_max_2D;
  int     n_k_1D;
  int     n_k_2D;
  double *k_1D;
  double *P_k_1D;
  double *dP_k_1D;
  int    *n_modes_1D;
  double *P_k_2D;
  double *dP_k_2D;
  int    *n_modes_2D;
  int     n_groups=1;
  double  dk_1D;
  double  dk_2D;
  char   *grid_identifier;

  // Initialization -- MPI etc.
  SID_init(&argc,&argv,NULL,NULL);

  // Parse arguments
  int grid_size;
  char filename_in_root[MAX_FILENAME_LENGTH];
  char filename_out_root[MAX_FILENAME_LENGTH];
  strcpy(filename_in_root,  argv[1]);
  snapshot_number=(int)atoi(argv[2]);
  strcpy(filename_out_root, argv[3]);
  grid_size      =(int)atoi(argv[4]);
  if(!strcmp(argv[5],"ngp") || !strcmp(argv[5],"NGP"))
     distribution_scheme=MAP2GRID_DIST_NGP;
  else if(!strcmp(argv[5],"cic") || !strcmp(argv[5],"CIC"))
     distribution_scheme=MAP2GRID_DIST_CIC;
  else if(!strcmp(argv[5],"tsc") || !strcmp(argv[5],"TSC"))
     distribution_scheme=MAP2GRID_DIST_TSC;
  else if(!strcmp(argv[5],"d12") || !strcmp(argv[5],"D12"))
     distribution_scheme=MAP2GRID_DIST_DWT12;
  else if(!strcmp(argv[5],"d20") || !strcmp(argv[5],"D20"))
     distribution_scheme=MAP2GRID_DIST_DWT20;
  else
     SID_trap_error("Invalid distribution scheme {%s} specified.",ERROR_SYNTAX,argv[5]);

  SID_log("Smoothing Gadget file {%s;snapshot=#%d} to a %dx%dx%d grid with %s kernel...",SID_LOG_OPEN|SID_LOG_TIMER,
          filename_in_root,snapshot_number,grid_size,grid_size,grid_size,argv[5]);

  // Initialization -- fetch header info
  SID_log("Reading Gadget header...",SID_LOG_OPEN);
  gadget_read_info   fp_gadget;
  int                flag_filefound=init_gadget_read(filename_in_root,snapshot_number,&fp_gadget);
  int                flag_multifile=fp_gadget.flag_multifile;
  int                flag_file_type=fp_gadget.flag_file_type;
  gadget_header_info header        =fp_gadget.header;
  double             box_size      =(double)(header.box_size);
  size_t            *n_all         =(size_t *)SID_calloc(sizeof(size_t)*N_GADGET_TYPE);
  size_t             n_total;
  if(flag_filefound){
     if(SID.I_am_Master){
        FILE *fp_in;
        char  filename[MAX_FILENAME_LENGTH];
        int   block_length_open;
        int   block_length_close;
        set_gadget_filename(&fp_gadget,0,filename);
        fp_in=fopen(filename,"r");
        fread_verify(&block_length_open, sizeof(int),1,fp_in);
        fread_verify(&header,            sizeof(gadget_header_info),1,fp_in);
        fread_verify(&block_length_close,sizeof(int),1,fp_in);
        fclose(fp_in);
        if(block_length_open!=block_length_close)
           SID_trap_error("Block lengths don't match (ie. %d!=%d).",ERROR_LOGIC,block_length_open,block_length_close);
     }
     SID_Bcast(&header,sizeof(gadget_header_info),MASTER_RANK,SID.COMM_WORLD);
     redshift=header.redshift;
     h_Hubble=header.h_Hubble;
     box_size=header.box_size;
     if(SID.n_proc>1)
        n_load=1;
     else
        n_load=header.n_files;
     for(i_species=0,n_total=0,n_used=0;i_species<N_GADGET_TYPE;i_species++){
        n_all[i_species]=(size_t)header.n_all_lo_word[i_species]+((size_t)header.n_all_hi_word[i_species])<<32;
        n_total+=n_all[i_species];
        if(n_all[i_species]>0){
           n_used++;
           flag_used[i_species]=TRUE;
        }
        else
           flag_used[i_species]=FALSE;
     }

     // Initialize cosmology
     double box_size        =((double *)ADaPS_fetch(plist.data,"box_size"))[0];
     double h_Hubble        =((double *)ADaPS_fetch(plist.data,"h_Hubble"))[0];
     double redshift        =((double *)ADaPS_fetch(plist.data,"redshift"))[0];
     double expansion_factor=((double *)ADaPS_fetch(plist.data,"expansion_factor"))[0];
     double Omega_M         =((double *)ADaPS_fetch(plist.data,"Omega_M"))[0];
     double Omega_Lambda    =((double *)ADaPS_fetch(plist.data,"Omega_Lambda"))[0];
     double Omega_k         =1.-Omega_Lambda-Omega_M;
     double Omega_b=0.; // not needed, so doesn't matter
     double f_gas  =Omega_b/Omega_M;
     double sigma_8=0.; // not needed, so doesn't matter
     double n_spec =0.; // not needed, so doesn't matter
     char   cosmo_name[16];
     sprintf(cosmo_name,"Gadget file's");
     init_cosmo(&cosmo,
                cosmo_name,
                Omega_Lambda,
                Omega_M,
                Omega_k,
                Omega_b,
                f_gas,
                h_Hubble,
                sigma_8,
                n_spec);
  }
  SID_log("Done.",SID_LOG_CLOSE);

  grid_identifier=(char *)SID_calloc(GRID_IDENTIFIER_SIZE*sizeof(char));

  // Only process if there are >0 particles present
  if(n_used>0){

     // Loop over ithe real-space and 3 redshift-space frames
     int i_write;
     int i_run;
     int n_run;
     int n_grids_total; 
     n_grids_total=4; // For now, hard-wire real-space density and velocity grids only
     n_run=1;         // For now, hard-wire real-space calculation only
     for(i_run=0,i_write=0;i_run<n_run;i_run++){

        // Read catalog
        int  n_grid;
        char i_run_identifier[8];
        switch(i_run){
        case 0:
           SID_log("Processing real-space ...",SID_LOG_OPEN|SID_LOG_TIMER);
           sprintf(i_run_identifier,"r");
           n_grid=4;
           break;
        case 1:
           SID_log("Processing v_x redshift space...",SID_LOG_OPEN|SID_LOG_TIMER);
           sprintf(i_run_identifier,"x");
           n_grid=1;
           break;
        case 2:
           SID_log("Processing v_y redshift space...",SID_LOG_OPEN|SID_LOG_TIMER);
           sprintf(i_run_identifier,"y");
           n_grid=1;
           break;
        case 3:
           SID_log("Processing v_z redsift space...",SID_LOG_OPEN|SID_LOG_TIMER);
           sprintf(i_run_identifier,"z");
           n_grid=1;
           break;
        }

        // For each i_run case, loop over the fields we want to produce
        int i_grid;
        for(i_grid=0;i_grid<n_grid;i_grid++){

           char i_grid_identifier[8];
           switch(i_grid){
           case 0:
              SID_log("Processing density grid ...",SID_LOG_OPEN|SID_LOG_TIMER);
              sprintf(i_grid_identifier,"rho");
              break;
           case 1:
              SID_log("Processing v_x velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER);
              sprintf(i_grid_identifier,"v_x");
              break;
           case 2:
              SID_log("Processing v_y velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER);
              sprintf(i_grid_identifier,"v_y");
              break;
           case 3:
              SID_log("Processing v_z velocity grid...",SID_LOG_OPEN|SID_LOG_TIMER);
              sprintf(i_grid_identifier,"v_z");
              break;
           }

           // Initialize the field that will hold the grid
           int        n[]={grid_size,grid_size,grid_size};
           double     L[]={box_size, box_size, box_size};
           int        i_init;
           for(i_species=0;i_species<N_GADGET_TYPE;i_species++){
              if(flag_used[i_species]){
                 field[i_species]     =(field_info *)SID_malloc(sizeof(field_info));
                 field_norm[i_species]=(field_info *)SID_malloc(sizeof(field_info));
                 init_field(3,n,L,field[i_species]);
                 init_field(3,n,L,field_norm[i_species]);
                 i_init=i_species;
              }
              else{
                 field[i_species]     =NULL;
                 field_norm[i_species]=NULL;
              }
           }

           // Loop over all the files that this rank will read
           int i_load;
           for(i_load=0;i_load<n_load;i_load++){
              if(n_load>1)
                 SID_log("Processing file No. %d of %d...",SID_LOG_OPEN|SID_LOG_TIMER,i_load+1,n_load);

              // Initialization -- read gadget file
              GBPREAL mass_array[N_GADGET_TYPE];
              init_plist(&plist,&((field[i_init])->slab),GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY);
              char filename_root[MAX_FILENAME_LENGTH];
              read_gadget_binary_local(filename_in_root,
                                       snapshot_number,
                                       i_run,
                                       i_load,
                                       n_load,
                                       mass_array,
                                       &(field[i_init]->slab),
                                       cosmo,
                                       &plist);

              // Generate power spectra
              for(i_species=0;i_species<plist.n_species;i_species++){

                 // Determine how many particles of species i_species there are
                 if(n_all[i_species]>0){
                    // Fetch the needed information
                    size_t   n_particles;
                    size_t   n_particles_local;
                    int      flag_alloc_m;
                    GBPREAL *x_particles_local;
                    GBPREAL *y_particles_local;
                    GBPREAL *z_particles_local;
                    GBPREAL *vx_particles_local;
                    GBPREAL *vy_particles_local;
                    GBPREAL *vz_particles_local;
                    GBPREAL *m_particles_local;
                    GBPREAL *v_particles_local;
                    GBPREAL *w_particles_local;
                    n_particles      =((size_t  *)ADaPS_fetch(plist.data,"n_all_%s",plist.species[i_species]))[0];
                    n_particles_local=((size_t  *)ADaPS_fetch(plist.data,"n_%s",    plist.species[i_species]))[0];
                    x_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"x_%s",    plist.species[i_species]);
                    y_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"y_%s",    plist.species[i_species]);
                    z_particles_local= (GBPREAL *)ADaPS_fetch(plist.data,"z_%s",    plist.species[i_species]);
                    vx_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vx_%s",   plist.species[i_species]);
                    vy_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vy_%s",   plist.species[i_species]);
                    vz_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"vz_%s",   plist.species[i_species]);
                    if(ADaPS_exist(plist.data,"M_%s",plist.species[i_species])){
                       flag_alloc_m=FALSE;
                       m_particles_local=(GBPREAL *)ADaPS_fetch(plist.data,"M_%s",plist.species[i_species]);
                    }
                    else{
                       flag_alloc_m=TRUE;
                       m_particles_local=(GBPREAL *)SID_malloc(n_particles_local*sizeof(GBPREAL));
                       int i_particle;
                       for(i_particle=0;i_particle<n_particles_local;i_particle++)
                          m_particles_local[i_particle]=mass_array[i_species];
                    }

                    // Decide the map_to_grid() mode
                    int mode;
                    if(n_load==1)
                       mode=MAP2GRID_MODE_DEFAULT;
                    else if(i_load==0 || n_load==1)
                       mode=MAP2GRID_MODE_DEFAULT|MAP2GRID_MODE_NONORM;
                    else if(i_load==(n_load-1))
                       mode=MAP2GRID_MODE_NOCLEAN;
                    else
                       mode=MAP2GRID_MODE_NOCLEAN|MAP2GRID_MODE_NONORM;

                    // Set the array that will weight the grid
                    field_info *field_i;
                    field_info *field_norm_i;
                    double factor;
                    switch(i_grid){
                    case 0:
                       v_particles_local=m_particles_local;
                       w_particles_local=NULL;
                       field_i          =field[i_species];
                       field_norm_i     =NULL;
                       mode|=MAP2GRID_MODE_APPLYFACTOR;
                       factor=pow((double)grid_size/box_size,3.);
                       break;
                    case 1:
                       v_particles_local=vx_particles_local;
                       w_particles_local=m_particles_local;
                       field_i          =field[i_species];
                       field_norm_i     =field_norm[i_species];
                       factor=1.;
                       break;
                    case 2:
                       v_particles_local=vy_particles_local;
                       w_particles_local=m_particles_local;
                       field_i          =field[i_species];
                       field_norm_i     =field_norm[i_species];
                       factor=1.;
                       break;
                    case 3:
                       v_particles_local=vz_particles_local;
                       w_particles_local=m_particles_local;
                       field_i          =field[i_species];
                       field_norm_i     =field_norm[i_species];
                       factor=1.;
                       break;
                    }

                    // Generate grid
                    map_to_grid(n_particles_local,
                                x_particles_local,
                                y_particles_local,
                                z_particles_local,
                                v_particles_local,
                                w_particles_local,
                                cosmo,
                                redshift,
                                distribution_scheme,
                                factor,
                                field_i,
                                field_norm_i,
                                mode);
                    if(flag_alloc_m)
                       SID_free(SID_FARG m_particles_local);
                 }
              }

              // Clean-up
              free_plist(&plist);
              if(n_load>1)
                 SID_log("Done.",SID_LOG_CLOSE);
           } // loop over i_load
           
           // Write results to disk
           char filename_out_species[MAX_FILENAME_LENGTH];
           init_plist(&plist,NULL,GADGET_LENGTH,GADGET_MASS,GADGET_VELOCITY);
           for(i_species=0;i_species<plist.n_species;i_species++){
              if(flag_used[i_species]){
                 sprintf(grid_identifier,"%s_%s_%s",i_grid_identifier,i_run_identifier,plist.species[i_species]);
                 sprintf(filename_out_species,"%s_%s",filename_out_root,plist.species[i_species]);
                 write_grid(field[i_species],
                            filename_out_species,
                            i_write,
                            n_grids_total,
                            distribution_scheme,
                            grid_identifier,
                            header.box_size);
                 free_field(field[i_species]);
                 free_field(field_norm[i_species]);
                 SID_free(SID_FARG field[i_species]);
                 SID_free(SID_FARG field_norm[i_species]);
                 i_write++;
              }
           }

           // Clean-up
           free_plist(&plist);
           SID_log("Done.",SID_LOG_CLOSE);

        } // loop over i_grid

        SID_log("Done.",SID_LOG_CLOSE);
     } // loop over i_run
  } // if n_used>0 

  // Clean-up
  free_cosmo(&cosmo);
  SID_free(SID_FARG grid_identifier);
  SID_free(SID_FARG n_all);

  SID_log("Done.",SID_LOG_CLOSE);

  SID_exit(ERROR_NONE);
}
示例#16
0
int main(int argc, char *argv[]) {
    int        snapshot;
    char       filename_out[256];
    char       filename_smooth[256];
    char       filename_snapshot[256];
    char *     species_name;
    double     h_Hubble;
    plist_info plist;
    size_t     i_particle;
    int        i_species;
    int        j_species;
    int        i_rank;
    size_t     n_particles;
    GBPREAL *  x_array;
    GBPREAL *  y_array;
    GBPREAL *  z_array;
    GBPREAL *  r_smooth_array;
    GBPREAL *  rho_array;
    GBPREAL *  sigma_v_array;
    FILE *     fp_out;

    SID_Init(&argc, &argv, NULL);

    strcpy(filename_snapshot, argv[1]);
    snapshot = atoi(argv[2]);
    strcpy(filename_smooth, argv[3]);
    strcpy(filename_out, argv[4]);

    SID_log("Creating ascii file {%s} from smmoth files {%s} and snapshot {%s}...",
            SID_LOG_OPEN | SID_LOG_TIMER,
            filename_out,
            filename_smooth,
            filename_snapshot);

    // Read snapshot files
    init_plist(&plist, NULL, GADGET_LENGTH, GADGET_MASS, GADGET_VELOCITY);
    read_gadget_binary(filename_snapshot, snapshot, &plist, READ_GADGET_DEFAULT);
    read_smooth(&plist, filename_smooth, 0, SMOOTH_DEFAULT);
    h_Hubble = ((double *)ADaPS_fetch(plist.data, "h_Hubble"))[0];

    // Loop over each species
    for(i_species = 0, j_species = 0; i_species < N_GADGET_TYPE; i_species++) {
        species_name = plist.species[i_species];
        if(ADaPS_exist(plist.data, "n_all_%s", species_name))
            n_particles = ((size_t *)ADaPS_fetch(plist.data, "n_all_%s", species_name))[0];
        else
            n_particles = 0;
        // If at least one rank has particles for this species ...
        if(n_particles > 0) {
            SID_log("Writting %s particles...", SID_LOG_OPEN, species_name);
            // ... then fetch arrays ...
            n_particles = ((size_t *)ADaPS_fetch(plist.data, "n_%s", species_name))[0];
            x_array     = (GBPREAL *)ADaPS_fetch(plist.data, "x_%s", species_name);
            y_array     = (GBPREAL *)ADaPS_fetch(plist.data, "y_%s", species_name);
            z_array     = (GBPREAL *)ADaPS_fetch(plist.data, "z_%s", species_name);
            if(ADaPS_exist(plist.data, "r_smooth_%s", species_name))
                r_smooth_array = (GBPREAL *)ADaPS_fetch(plist.data, "r_smooth_%s", species_name);
            else
                r_smooth_array = NULL;
            if(ADaPS_exist(plist.data, "rho_%s", species_name))
                rho_array = (GBPREAL *)ADaPS_fetch(plist.data, "rho_%s", species_name);
            else
                rho_array = NULL;
            if(ADaPS_exist(plist.data, "sigma_v_%s", species_name))
                sigma_v_array = (GBPREAL *)ADaPS_fetch(plist.data, "sigma_v_%s", species_name);
            else
                sigma_v_array = NULL;

            // ... and write this species' particles
            for(i_rank = 0; i_rank < SID.n_proc; i_rank++) {
                if(SID.My_rank == i_rank) {
                    if(j_species == 0 && i_rank == 0)
                        fp_out = fopen(filename_out, "w");
                    else
                        fp_out = fopen(filename_out, "a");
                    for(i_particle = 0; i_particle < n_particles; i_particle++) {
                        fprintf(fp_out,
                                "%2d %11.4le %11.4le %11.4le",
                                i_species,
                                (double)x_array[i_particle] * h_Hubble / M_PER_MPC,
                                (double)y_array[i_particle] * h_Hubble / M_PER_MPC,
                                (double)z_array[i_particle] * h_Hubble / M_PER_MPC);
                        if(r_smooth_array != NULL)
                            fprintf(fp_out, " %10.4le", (double)r_smooth_array[i_particle] * h_Hubble / M_PER_MPC);
                        if(rho_array != NULL)
                            fprintf(fp_out, " %10.4le", (double)rho_array[i_particle] / (M_SOL * pow(h_Hubble / M_PER_MPC, 3.)));
                        if(sigma_v_array != NULL)
                            fprintf(fp_out, " %10.4le", (double)sigma_v_array[i_particle] * 1e-3);
                        fprintf(fp_out, "\n");
                    }
                    fclose(fp_out);
                }
                SID_Barrier(SID_COMM_WORLD);
            }
            j_species++;
            SID_log("Done.", SID_LOG_CLOSE);
        }
    }

    // Clean-up
    free_plist(&plist);
    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
示例#17
0
static char *
extract_pkgname(int fd)
{
	package_t plist;
	plist_t *p;
	struct archive *a;
	struct archive_entry *entry;
	char *buf;
	ssize_t len;
	int r;

	a = archive_read_new();
	archive_read_support_compression_all(a);
	archive_read_support_format_all(a);
	if (archive_read_open_fd(a, fd, 1024)) {
		warnx("Cannot open binary package: %s",
		    archive_error_string(a));
		archive_read_finish(a);
		return NULL;
	}

	r = archive_read_next_header(a, &entry);
	if (r != ARCHIVE_OK) {
		warnx("Cannot extract package name: %s",
		    r == ARCHIVE_EOF ? "EOF" : archive_error_string(a));
		archive_read_finish(a);
		return NULL;
	}
	if (strcmp(archive_entry_pathname(entry), "+CONTENTS") != 0) {
		warnx("Invalid binary package, doesn't start with +CONTENTS");
		archive_read_finish(a);
		return NULL;
	}
	if (archive_entry_size(entry) > SSIZE_MAX - 1) {
		warnx("+CONTENTS too large to process");
		archive_read_finish(a);
		return NULL;
	}

	len = archive_entry_size(entry);
	buf = xmalloc(len + 1);

	if (archive_read_data(a, buf, len) != len) {
		warnx("Short read when extracing +CONTENTS");
		free(buf);
		archive_read_finish(a);
		return NULL;
	}
	buf[len] = '\0';

	archive_read_finish(a);

	parse_plist(&plist, buf);
	free(buf);
	p = find_plist(&plist, PLIST_NAME);	
	if (p != NULL) {
		buf = xstrdup(p->name);
	} else {
		warnx("Invalid PLIST: missing @name");
		buf = NULL;
	}
	free_plist(&plist);

	if (lseek(fd, 0, SEEK_SET) != 0) {
		warn("Cannot seek in archive");
		free(buf);
		return NULL;
	}

	return buf;
}
示例#18
0
文件: check.c 项目: Spenser309/CS551
/*
 * Assumes CWD is in /var/db/pkg/<pkg>!
 */
static void 
check1pkg(const char *pkgdir, int *filecnt, int *pkgcnt)
{
	FILE   *f;
	plist_t *p;
	package_t Plist;
	char   *PkgName, *dirp = NULL, *md5file;
	char    file[MaxPathSize];
	char   *content;

	content = pkgdb_pkg_file(pkgdir, CONTENTS_FNAME);
	f = fopen(content, "r");
	if (f == NULL)
		err(EXIT_FAILURE, "can't open %s", content);
	free(content);

	read_plist(&Plist, f);
	p = find_plist(&Plist, PLIST_NAME);
	if (p == NULL)
		errx(EXIT_FAILURE, "Package %s has no @name, aborting.",
		    pkgdir);
	PkgName = p->name;
	for (p = Plist.head; p; p = p->next) {
		switch (p->type) {
		case PLIST_FILE:
			if (dirp == NULL) {
				warnx("dirp not initialized, please send-pr!");
				abort();
			}
			
			(void) snprintf(file, sizeof(file), "%s/%s", dirp, p->name);

			if (isfile(file) || islinktodir(file)) {
				if (p->next && p->next->type == PLIST_COMMENT) {
					if (strncmp(p->next->name, CHECKSUM_HEADER, ChecksumHeaderLen) == 0) {
						if ((md5file = MD5File(file, NULL)) != NULL) {
							/* Mismatch? */
							if (strcmp(md5file, p->next->name + ChecksumHeaderLen) != 0)
								printf("%s fails MD5 checksum\n", file);

							free(md5file);
						}
					} else if (strncmp(p->next->name, SYMLINK_HEADER, SymlinkHeaderLen) == 0) {
						char	buf[MaxPathSize + SymlinkHeaderLen];
						int	cc;

						(void) strlcpy(buf, SYMLINK_HEADER, sizeof(buf));
						if ((cc = readlink(file, &buf[SymlinkHeaderLen],
							  sizeof(buf) - SymlinkHeaderLen - 1)) < 0) {
							warnx("can't readlink `%s'", file);
						} else {
							buf[SymlinkHeaderLen + cc] = 0x0;
							if (strcmp(buf, p->next->name) != 0) {
								printf("symlink (%s) is not same as recorded value, %s: %s\n",
								    file, buf, p->next->name);
							}
						}
					}
				}
				
				(*filecnt)++;
			} else if (isbrokenlink(file)) {
				warnx("%s: Symlink `%s' exists and is in %s but target does not exist!", PkgName, file, CONTENTS_FNAME);
			} else {
				warnx("%s: File `%s' is in %s but not on filesystem!", PkgName, file, CONTENTS_FNAME);
			}
			break;
		case PLIST_CWD:
			if (strcmp(p->name, ".") != 0)
				dirp = p->name;
			else
				dirp = pkgdb_pkg_dir(pkgdir);
			break;
		case PLIST_IGNORE:
			p = p->next;
			break;
		case PLIST_SHOW_ALL:
		case PLIST_SRC:
		case PLIST_CMD:
		case PLIST_CHMOD:
		case PLIST_CHOWN:
		case PLIST_CHGRP:
		case PLIST_COMMENT:
		case PLIST_NAME:
		case PLIST_UNEXEC:
		case PLIST_DISPLAY:
		case PLIST_PKGDEP:
		case PLIST_DIR_RM:
		case PLIST_OPTION:
		case PLIST_PKGCFL:
		case PLIST_BLDDEP:
		case PLIST_PKGDIR:
			break;
		}
	}
	free_plist(&Plist);
	fclose(f);
	(*pkgcnt)++;
}
示例#19
0
文件: main.c 项目: petabi/pkgsrc
/*
 * add1pkg(<pkg>)
 *	adds the files listed in the +CONTENTS of <pkg> into the
 *	pkgdb.byfile.db database file in the current package dbdir.  It
 *	returns the number of files added to the database file.
 */
static int
add_pkg(const char *pkgdir, void *vp)
{
    FILE	       *f;
    plist_t	       *p;
    package_t	Plist;
    char 	       *contents;
    char *PkgName, *dirp;
    char 		file[MaxPathSize];
    struct pkgdb_count *count;

    if (!pkgdb_open(ReadWrite))
        err(EXIT_FAILURE, "cannot open pkgdb");

    count = vp;
    ++count->packages;

    contents = pkgdb_pkg_file(pkgdir, CONTENTS_FNAME);
    if ((f = fopen(contents, "r")) == NULL)
        errx(EXIT_FAILURE, "%s: can't open `%s'", pkgdir, CONTENTS_FNAME);
    free(contents);

    read_plist(&Plist, f);
    if ((p = find_plist(&Plist, PLIST_NAME)) == NULL) {
        errx(EXIT_FAILURE, "Package `%s' has no @name, aborting.", pkgdir);
    }

    PkgName = p->name;
    dirp = NULL;
    for (p = Plist.head; p; p = p->next) {
        switch(p->type) {
        case PLIST_FILE:
            if (dirp == NULL) {
                errx(EXIT_FAILURE, "@cwd not yet found, please send-pr!");
            }
            (void) snprintf(file, sizeof(file), "%s/%s", dirp, p->name);
            if (!(isfile(file) || islinktodir(file))) {
                if (isbrokenlink(file)) {
                    warnx("%s: Symlink `%s' exists and is in %s but target does not exist!",
                          PkgName, file, CONTENTS_FNAME);
                } else {
                    warnx("%s: File `%s' is in %s but not on filesystem!",
                          PkgName, file, CONTENTS_FNAME);
                }
            } else {
                pkgdb_store(file, PkgName);
                ++count->files;
            }
            break;
        case PLIST_PKGDIR:
            add_pkgdir(PkgName, dirp, p->name);
            ++count->directories;
            break;
        case PLIST_CWD:
            if (strcmp(p->name, ".") != 0)
                dirp = p->name;
            else
                dirp = pkgdb_pkg_dir(pkgdir);
            break;
        case PLIST_IGNORE:
            p = p->next;
            break;
        case PLIST_SHOW_ALL:
        case PLIST_SRC:
        case PLIST_CMD:
        case PLIST_CHMOD:
        case PLIST_CHOWN:
        case PLIST_CHGRP:
        case PLIST_COMMENT:
        case PLIST_NAME:
        case PLIST_UNEXEC:
        case PLIST_DISPLAY:
        case PLIST_PKGDEP:
        case PLIST_DIR_RM:
        case PLIST_OPTION:
        case PLIST_PKGCFL:
        case PLIST_BLDDEP:
            break;
        }
    }
    free_plist(&Plist);
    fclose(f);
    pkgdb_close();

    return 0;
}
示例#20
0
int main(int argc, char *argv[]) {
    plist_info            plist;
    char                  filename_root[256];
    char                  filename_log[256];
    char                  filename_number[256];
    char                  filename_in_halos[256];
    char                  filename_out_groups[256];
    char                  filename_out_groups_A[256];
    char                  filename_out_groups_B[256];
    char                  filename_out_groups_C[256];
    char                  filename_out_subgroups[256];
    char                  filename_out_subgroups_A[256];
    char                  filename_out_subgroups_B[256];
    char                  filename_out_hierarchy[256];
    char                  filename_out_hierarchy_A[256];
    char                  filename_out_hierarchy_B[256];
    char                  filename_out_particles[256];
    char                  i_match_txt[5];
    int                   n_groups_AHF;
    int                   n_groups;
    int                   n_subgroups;
    int                   n_subgroups_matched;
    int                   n_subgroups_group;
    size_t                n_particles;
    size_t                n_particles_in_groups;
    size_t                n_particles_in_subgroups;
    size_t                n_particles_AHF_not_used;
    int                   n_particles_temp;
    int *                 n_p_1 = NULL;
    int                   flag_continue;
    int                   flag_long_ids;
    int                   i_match;
    int                   match_id_next;
    int *                 match_id         = NULL;
    int *                 match_id_initial = NULL;
    FILE *                fp               = NULL;
    FILE *                fp_in_halos      = NULL;
    FILE *                fp_out           = NULL;
    int                   n_match;
    int *                 id_2                   = NULL;
    size_t *              particle_ids_AHF       = NULL;
    size_t *              particle_ids_AHF_index = NULL;
    size_t                id_largest;
    int                   id_byte_size;
    size_t *              group_particles = NULL;
    int                   group_id;
    int                   subgroup_id;
    int                   i_group;
    int                   j_group;
    int                   k_group;
    size_t                n_particles_AHF;
    int *                 subgroup_size   = NULL;
    int *                 hierarchy_level = NULL;
    int *                 hierarchy_match = NULL;
    int                   subgroup_size_max;
    int *                 subgroup_size_list       = NULL;
    int *                 subgroup_index_list      = NULL;
    size_t *              subgroup_size_list_index = NULL;
    int *                 group_offsets            = NULL;
    size_t                group_index;
    int *                 group_size        = NULL;
    int *                 group_size_AHF    = NULL;
    int *                 group_offsets_AHF = NULL;
    int                   max_subgroup_size;
    int                   i_subgroup;
    int                   j_subgroup;
    int                   n_subgroups_group_max;
    size_t *              group_size_index = NULL;
    size_t *              match_id_index   = NULL;
    size_t                subgroup_index;
    int                   group_offset;
    int                   subgroup_offset;
    int                   group_count;
    size_t *              group_particles_index = NULL;
    size_t *              subgroup_particles    = NULL;
    int *                 particle_group        = NULL;
    size_t *              particle_group_index  = NULL;
    size_t                i_particle;
    size_t                j_particle;
    size_t                k_particle;
    int                   i_file;
    int                   i_file_start;
    int                   i_file_stop;
    size_t *              match_index = NULL;
    int                   flag_match_subgroups;
    FILE *                fp_log             = NULL;
    FILE *                fp_in              = NULL;
    FILE *                fp_out_particles   = NULL;
    FILE *                fp_out_groups      = NULL;
    FILE *                fp_out_groups_A    = NULL;
    FILE *                fp_out_groups_B    = NULL;
    FILE *                fp_out_groups_C    = NULL;
    FILE *                fp_out_subgroups_A = NULL;
    FILE *                fp_out_subgroups_B = NULL;
    FILE *                fp_out_hierarchy_A = NULL;
    FILE *                fp_out_hierarchy_B = NULL;
    FILE *                fp_test            = NULL;
    int                   substructure_level;
    int                   substructure_level_max;
    halo_properties_info *properties      = NULL;
    void *                particle_buffer = NULL;
    int                   flag_found;

    SID_Init(&argc, &argv, NULL);

    strcpy(filename_root, argv[1]);
    i_file_start = atoi(argv[2]);
    i_file_stop  = atoi(argv[3]);

    SID_log("Converting files #%d->#%d from AHF to subfind format...", SID_LOG_OPEN | SID_LOG_TIMER, i_file_start, i_file_stop);

    sprintf(filename_log, "%s_%dto%d.convert_AHF_log", filename_root, i_file_start, i_file_stop);

    // Loop over all files
    for(i_file = i_file_start; i_file <= i_file_stop; i_file++) {
        SID_log("Processing file #%d...", SID_LOG_OPEN | SID_LOG_TIMER, i_file);

        // Read catalogs
        if(i_file < 10)
            sprintf(filename_number, "00%1d", i_file);
        else if(i_file < 100)
            sprintf(filename_number, "0%2d", i_file);
        else
            sprintf(filename_number, "%3d", i_file);

        // Read AHF group file
        init_plist(&plist, NULL, GADGET_LENGTH, GADGET_MASS, GADGET_VELOCITY);
        read_groups_AHF(filename_root, i_file, READ_GROUPS_ALL, &plist, filename_number);
        n_groups_AHF = ((int *)ADaPS_fetch(plist.data, "n_groups_%s", filename_number))[0];

        n_groups                 = 0;
        n_subgroups              = 0;
        n_subgroups_matched      = 0;
        n_subgroups_group        = 0;
        n_particles              = 0;
        n_particles_in_groups    = 0;
        n_particles_in_subgroups = 0;
        n_particles_AHF_not_used = 0;
        n_subgroups_group_max    = 0;

        if(n_groups_AHF > 0) {
            n_particles_AHF   = (size_t)((size_t *)ADaPS_fetch(plist.data, "n_particles_%s", filename_number))[0];
            group_size_AHF    = (int *)ADaPS_fetch(plist.data, "n_particles_group_%s", filename_number);
            group_offsets_AHF = (int *)ADaPS_fetch(plist.data, "particle_offset_group_%s", filename_number);
            particle_ids_AHF  = (size_t *)ADaPS_fetch(plist.data, "particle_ids_%s", filename_number);

            // Find largest id so we know what size to write the ids with
            for(i_particle = 0, id_largest = 0; i_particle < n_particles_AHF; i_particle++)
                id_largest = GBP_MAX(id_largest, particle_ids_AHF[i_particle]);
            if(id_largest > INT_MAX) {
                flag_long_ids = GBP_TRUE;
                id_byte_size  = sizeof(size_t);
            } else {
                flag_long_ids = GBP_FALSE;
                id_byte_size  = sizeof(int);
            }

            // Match AHF groups against themselves to find substructure
            match_halos(&plist, NULL, i_file, NULL, 0, &plist, NULL, i_file, NULL, 0, "substructure", MATCH_SUBSTRUCTURE, MATCH_SCORE_RANK_INDEX);
            match_id_initial = (int *)ADaPS_fetch(plist.data, "match_substructure");
            hierarchy_match  = match_id_initial; // Fore readability

            // Assign sub-...-sub-structures to parent (ie. top-level) halos
            SID_log("Assigning substructures to groups...", SID_LOG_OPEN);
            group_size      = (int *)SID_malloc(sizeof(int) * n_groups_AHF);
            subgroup_size   = (int *)SID_malloc(sizeof(int) * n_groups_AHF);
            hierarchy_level = (int *)SID_malloc(sizeof(int) * n_groups_AHF);
            particle_group  = (int *)SID_malloc(sizeof(int) * n_particles_AHF);
            for(i_group = 0, i_particle = 0; i_group < n_groups_AHF; i_group++) {
                group_size[i_group]    = 0;
                subgroup_size[i_group] = 0;
                for(j_particle = 0; j_particle < group_size_AHF[i_group]; i_particle++, j_particle++)
                    particle_group[i_particle] = i_group;
            }
            match_id = (int *)SID_malloc(sizeof(int) * n_groups_AHF);
            for(i_group = 0, substructure_level_max = 0; i_group < n_groups_AHF; i_group++) {
                substructure_level = 0;
                match_id_next      = match_id_initial[i_group];
                match_id[i_group]  = match_id_next;
                while(match_id_next >= 0) {
                    substructure_level++;
                    match_id[i_group] = match_id_next; // Tie subgroups to their top-level group
                    match_id_next     = match_id_initial[match_id_next];
                }
                if(match_id[i_group] < 0)
                    match_id[i_group] = i_group; // Unmatched halos should be matched to themselves
                hierarchy_level[i_group] = substructure_level;
                substructure_level_max   = GBP_MAX(substructure_level, substructure_level_max);
            }
            // needed? ADaPS_store(&(plist.data),(void *)(match_id),"match_substructure",ADaPS_DEFAULT);
            SID_log("Done.", SID_LOG_CLOSE);

            // Make sure the deepest substructures are given particle ownership
            SID_log("Assigning particles to subgroups...", SID_LOG_OPEN);
            merge_sort(
                (void *)particle_ids_AHF, (size_t)n_particles_AHF, &particle_ids_AHF_index, SID_SIZE_T, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);
            for(i_particle = 0, n_particles_AHF_not_used = 0; i_particle < n_particles_AHF; i_particle += k_particle) {
                // Count the number of times this particle id is used
                j_particle = i_particle;
                while(particle_ids_AHF[particle_ids_AHF_index[j_particle]] == particle_ids_AHF[particle_ids_AHF_index[i_particle]] &&
                      j_particle < (n_particles_AHF - 2))
                    j_particle++;
                if(particle_ids_AHF[particle_ids_AHF_index[j_particle]] == particle_ids_AHF[particle_ids_AHF_index[i_particle]])
                    j_particle++;
                k_particle = j_particle - i_particle;
                // Find the deepest substructure using this particle id...
                i_group = particle_group[particle_ids_AHF_index[i_particle]];
                for(j_particle = 1; j_particle < k_particle; j_particle++) {
                    j_group = particle_group[particle_ids_AHF_index[i_particle + j_particle]];
                    if(group_size_AHF[j_group] < group_size_AHF[i_group])
                        i_group = j_group;
                }
                // ... and set particle's group to a dummy value if this particle instance is not from the deepest group
                for(j_particle = 0, flag_found = GBP_FALSE; j_particle < k_particle; j_particle++) {
                    if(particle_group[particle_ids_AHF_index[i_particle + j_particle]] != i_group || flag_found) {
                        particle_group[particle_ids_AHF_index[i_particle + j_particle]] = -1;
                        n_particles_AHF_not_used++;
                    } else
                        flag_found = GBP_TRUE;
                }
            }
            SID_free((void **)&particle_ids_AHF_index);
            SID_log("Done.", SID_LOG_CLOSE);

            // Generate subgroup_size array
            for(i_group = 0; i_group < n_groups_AHF; i_group++)
                subgroup_size[i_group] = 0;
            for(i_particle = 0; i_particle < n_particles_AHF; i_particle++) {
                i_group = particle_group[i_particle];
                if(i_group >= 0)
                    subgroup_size[i_group]++;
            }

            // Get rid of groups that are too small
            for(i_particle = 0; i_particle < n_particles_AHF; i_particle++) {
                i_group = particle_group[i_particle];
                if(i_group >= 0) {
                    if(subgroup_size[i_group] < 20) {
                        n_particles_AHF_not_used++;
                        particle_group[i_particle] = -1;
                    }
                }
            }

            // Regenerate subgroup_size array
            for(i_group = 0; i_group < n_groups_AHF; i_group++)
                subgroup_size[i_group] = 0;
            for(i_particle = 0; i_particle < n_particles_AHF; i_particle++) {
                i_group = particle_group[i_particle];
                if(i_group >= 0)
                    subgroup_size[i_group]++;
            }

            // Find the largest subgroup's size
            for(i_group = 0, n_subgroups = 0, subgroup_size_max = 0; i_group < n_groups_AHF; i_group++)
                subgroup_size_max = GBP_MAX(subgroup_size[i_group], subgroup_size_max);

            // Generate group_size array
            for(i_group = 0; i_group < n_groups_AHF; i_group++)
                group_size[match_id[i_group]] += subgroup_size[i_group]; // update group size

            // Sort groups in order of size
            merge_sort((void *)group_size, (size_t)n_groups_AHF, &group_size_index, SID_INT, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);
            merge_sort((void *)match_id, (size_t)n_groups_AHF, &match_id_index, SID_INT, SORT_COMPUTE_INDEX, SORT_COMPUTE_NOT_INPLACE);

            // Count groups, subgroups, etc.
            SID_log("Counting groups & subgroups...", SID_LOG_OPEN);
            for(i_group = 0, n_groups = 0, n_subgroups = 0; i_group < n_groups_AHF; i_group++) {
                group_index = group_size_index[n_groups_AHF - i_group - 1];

                // Find start of subgroup list for this group
                j_group = find_index_int(match_id, group_index, n_groups_AHF, match_id_index);
                while(group_index > match_id[match_id_index[j_group]] && j_group < (n_groups_AHF - 2))
                    j_group++;
                if(group_index > match_id[match_id_index[j_group]])
                    j_group++;

                // Count subgroups
                n_subgroups_group = 0;
                while(match_id[match_id_index[j_group]] == group_index && j_group < (n_groups_AHF - 2)) {
                    if(subgroup_size[match_id_index[j_group]] > 0)
                        n_subgroups_group++;
                    j_group++;
                }
                if(match_id[match_id_index[j_group]] == group_index) {
                    if(subgroup_size[match_id_index[j_group]] > 0)
                        n_subgroups_group++;
                    j_group++;
                }
                n_subgroups += n_subgroups_group;

                // Largest number of subgroups
                n_subgroups_group_max = GBP_MAX(n_subgroups_group_max, n_subgroups_group);

                // Count groups
                if(n_subgroups_group > 0)
                    n_groups++;
            }
            SID_log("Done.", SID_LOG_CLOSE);
        }

        // Find largest subgroup and count the number of particles in groups
        for(i_group = 0, max_subgroup_size = 0, n_particles_in_groups = 0; i_group < n_groups_AHF; i_group++) {
            max_subgroup_size = GBP_MAX(max_subgroup_size, subgroup_size[i_group]);
            if(subgroup_size[i_group] > 0)
                n_particles_in_groups += (size_t)subgroup_size[i_group];
        }

        // Write some statistics
        SID_log("Substructure statistics:", SID_LOG_OPEN);
        SID_log("Number of groups                 =%d", SID_LOG_COMMENT, n_groups);
        SID_log("Number of subgroups              =%d", SID_LOG_COMMENT, n_subgroups);
        SID_log("Max number of subgroups per group=%d", SID_LOG_COMMENT, n_subgroups_group_max);
        SID_log("Largest subgroup                 =%d particles", SID_LOG_COMMENT, subgroup_size_max);
        SID_log("Depth of substructure heirarchy  =%d levels", SID_LOG_COMMENT, substructure_level_max);
        SID_log("Number of AHF particles used     =%lld", SID_LOG_COMMENT, n_particles_in_groups);
        SID_log("Number of AHF particles NOT used =%lld", SID_LOG_COMMENT, n_particles_AHF_not_used);
        SID_log("", SID_LOG_CLOSE | SID_LOG_NOPRINT);

        // Open files
        SID_set_verbosity(SID_SET_VERBOSITY_RELATIVE, 0);
        SID_log("Writing %d groups, %d subgroups and %lld particles to files...",
                SID_LOG_OPEN | SID_LOG_TIMER,
                n_groups,
                n_subgroups,
                n_particles_in_groups);
        sprintf(filename_out_groups, "%s_%s.catalog_groups", filename_root, filename_number);
        sprintf(filename_out_groups_A, "%s_%s.catalog_groups_A", filename_root, filename_number);
        sprintf(filename_out_groups_B, "%s_%s.catalog_groups_B", filename_root, filename_number);
        sprintf(filename_out_groups_C, "%s_%s.catalog_groups_C", filename_root, filename_number);
        sprintf(filename_out_subgroups, "%s_%s.catalog_subgroups", filename_root, filename_number);
        sprintf(filename_out_subgroups_A, "%s_%s.catalog_subgroups_A", filename_root, filename_number);
        sprintf(filename_out_subgroups_B, "%s_%s.catalog_subgroups_B", filename_root, filename_number);
        sprintf(filename_out_hierarchy, "%s_%s.catalog_hierarchy", filename_root, filename_number);
        sprintf(filename_out_hierarchy_A, "%s_%s.catalog_hierarchy_A", filename_root, filename_number);
        sprintf(filename_out_hierarchy_B, "%s_%s.catalog_hierarchy_B", filename_root, filename_number);
        sprintf(filename_out_particles, "%s_%s.catalog_particles", filename_root, filename_number);
        fp_out_groups_A    = fopen(filename_out_groups_A, "w");
        fp_out_groups_B    = fopen(filename_out_groups_B, "w");
        fp_out_groups_C    = fopen(filename_out_groups_C, "w");
        fp_out_subgroups_A = fopen(filename_out_subgroups_A, "w");
        fp_out_subgroups_B = fopen(filename_out_subgroups_B, "w");
        fp_out_hierarchy_A = fopen(filename_out_hierarchy_A, "w");
        fp_out_hierarchy_B = fopen(filename_out_hierarchy_B, "w");
        fp_out_particles   = fopen(filename_out_particles, "w");

        // Write headers
        fwrite(&n_groups, sizeof(int), 1, fp_out_groups_A);
        fwrite(&n_subgroups, sizeof(int), 1, fp_out_subgroups_A);
        fwrite(&n_subgroups, sizeof(int), 1, fp_out_hierarchy_A);
        fwrite(&id_byte_size, sizeof(int), 1, fp_out_particles);
        switch(flag_long_ids) {
            case GBP_TRUE:
                fwrite(&n_particles_in_groups, sizeof(size_t), 1, fp_out_particles);
                break;
            default:
                n_particles_temp = (int)n_particles_in_groups;
                fwrite(&n_particles_temp, sizeof(int), 1, fp_out_particles);
                break;
        }

        // Write files; group and subgroup files in parts (to be concatinated together later)
        subgroup_size_list  = (int *)SID_malloc(sizeof(int) * n_subgroups_group_max);
        subgroup_index_list = (int *)SID_malloc(sizeof(int) * n_subgroups_group_max);
        particle_buffer     = (void *)SID_malloc(id_byte_size * subgroup_size_max);
        subgroup_offset     = 0;
        group_offset        = 0;

        for(i_group = n_groups_AHF - 1; i_group >= n_groups_AHF - n_groups; i_group--) {
            group_index = group_size_index[i_group];

            // Find start of subgroup list for this group
            i_subgroup = find_index_int(match_id, group_index, n_groups_AHF, match_id_index);
            while(group_index > match_id[match_id_index[i_subgroup]] && i_subgroup < (n_groups_AHF - 2))
                i_subgroup++;
            if(group_index > match_id[match_id_index[i_subgroup]])
                i_subgroup++;

            // Create a list of subgroups for this group and sort it by size
            n_subgroups_group = 0;
            subgroup_index    = match_id_index[i_subgroup];
            while(match_id[subgroup_index] == group_index && i_subgroup < (n_groups_AHF - 2)) {
                if(subgroup_size[subgroup_index] > 0) {
                    subgroup_size_list[n_subgroups_group]  = subgroup_size[subgroup_index];
                    subgroup_index_list[n_subgroups_group] = (int)subgroup_index;
                    n_subgroups_group++;
                }
                i_subgroup++;
                subgroup_index = match_id_index[i_subgroup];
            }
            if(match_id[subgroup_index] == group_index) {
                if(subgroup_size[subgroup_index] > 0) {
                    subgroup_size_list[n_subgroups_group]  = subgroup_size[subgroup_index];
                    subgroup_index_list[n_subgroups_group] = (int)subgroup_index;
                    n_subgroups_group++;
                }
                i_subgroup++;
                subgroup_index = match_id_index[i_subgroup];
            }
            merge_sort((void *)subgroup_size_list,
                       (size_t)n_subgroups_group,
                       &subgroup_size_list_index,
                       SID_INT,
                       SORT_COMPUTE_INDEX,
                       SORT_COMPUTE_NOT_INPLACE);

            // Perform writes for subgroups and particle lists
            for(i_subgroup = 0, i_particle = 0; i_subgroup < n_subgroups_group; i_subgroup++) {
                j_subgroup = subgroup_index_list[subgroup_size_list_index[n_subgroups_group - i_subgroup - 1]];
                // ... subgroups ...
                fwrite(&(subgroup_size[j_subgroup]), sizeof(int), 1, fp_out_subgroups_A);
                fwrite(&(subgroup_offset), sizeof(int), 1, fp_out_subgroups_B);
                fwrite(&(hierarchy_match[j_subgroup]), sizeof(int), 1, fp_out_hierarchy_A);
                fwrite(&(hierarchy_level[j_subgroup]), sizeof(int), 1, fp_out_hierarchy_B);
                subgroup_offset += subgroup_size[j_subgroup];
                // ... and particles
                for(j_particle = group_offsets_AHF[j_subgroup], k_particle = 0, i_particle = 0; k_particle < group_size_AHF[j_subgroup];
                    j_particle++, k_particle++) {
                    if(particle_group[j_particle] == j_subgroup) {
                        switch(flag_long_ids) {
                            case GBP_TRUE:
                                ((size_t *)particle_buffer)[i_particle++] = (size_t)(particle_ids_AHF[j_particle]);
                                break;
                            default:
                                ((int *)particle_buffer)[i_particle++] = (int)(particle_ids_AHF[j_particle]);
                                break;
                        }
                    }
                }
                if(i_particle == subgroup_size[j_subgroup])
                    fwrite(particle_buffer, id_byte_size, i_particle, fp_out_particles);
                else
                    SID_exit_error("Subgroup size mismatch!", SID_ERROR_LOGIC);
            }

            SID_free((void **)&subgroup_size_list_index);

            // Perform writes for groups
            fwrite(&(group_size[group_index]), sizeof(int), 1, fp_out_groups_A);
            fwrite(&group_offset, sizeof(int), 1, fp_out_groups_B);
            fwrite(&n_subgroups_group, sizeof(int), 1, fp_out_groups_C);
            group_offset += group_size[group_index];
        }
        SID_free((void **)&subgroup_size_list);
        SID_free((void **)&subgroup_index_list);
        SID_free((void **)&particle_buffer);

        fclose(fp_out_groups_A);
        fclose(fp_out_groups_B);
        fclose(fp_out_groups_C);
        fclose(fp_out_subgroups_A);
        fclose(fp_out_subgroups_B);
        fclose(fp_out_hierarchy_A);
        fclose(fp_out_hierarchy_B);
        fclose(fp_out_particles);

        // Concatinate group and subgroup temp files into final files
        SID_cat_files(filename_out_groups, SID_CAT_CLEAN, 3, filename_out_groups_A, filename_out_groups_B, filename_out_groups_C);

        SID_cat_files(filename_out_subgroups, SID_CAT_CLEAN, 2, filename_out_subgroups_A, filename_out_subgroups_B);

        SID_cat_files(filename_out_hierarchy, SID_CAT_CLEAN, 2, filename_out_hierarchy_A, filename_out_hierarchy_B);

        // Clean-up
        SID_free((void **)&subgroup_size);
        SID_free((void **)&hierarchy_level);
        SID_free((void **)&group_size);
        SID_free((void **)&group_size_index);
        SID_free((void **)&match_id_index);
        free_plist(&plist);
        SID_set_verbosity(SID_SET_VERBOSITY_DEFAULT);
        SID_log("Done.", SID_LOG_CLOSE);

        // Write log file
        SID_log("Writing to log file...", SID_LOG_OPEN);
        // Write a header for the log file
        if(i_file == i_file_start) {
            fp_log = fopen(filename_log, "w");
            fprintf(fp_log, "# (1):  filenumber\n");
            fprintf(fp_log, "# (2):  n_groups_AHF\n");
            fprintf(fp_log, "# (3):  n_particles_AHF\n");
            fprintf(fp_log, "# (4):  n_groups\n");
            fprintf(fp_log, "# (5):  n_subgroups\n");
            fprintf(fp_log, "# (6):  max number of subgroups per group\n");
            fprintf(fp_log, "# (7):  largest subgroup\n");
            fprintf(fp_log, "# (8):  depth of substructure heirarchy\n");
            fprintf(fp_log, "# (9):  number of AHF particles used\n");
            fprintf(fp_log, "# (10): number of AHF particles NOT used\n");
        } else
            fp_log = fopen(filename_log, "a");
        fprintf(fp_log,
                "%4d %9d %12zd %9d %9d %9d %9d %9d %12zd %12zd\n",
                i_file,
                n_groups_AHF,
                n_particles_AHF,
                n_groups,
                n_subgroups,
                n_subgroups_group_max,
                subgroup_size_max,
                substructure_level_max,
                n_particles_in_groups,
                n_particles_AHF_not_used);
        fclose(fp_log);

        SID_log("Done.", SID_LOG_CLOSE);

        SID_log("Done.", SID_LOG_CLOSE);
    }

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}
static
void discrim_bind_delete(Term t, Discrim root, void *object)
{
  Discrim end, d2, d3, parent;
  Plist tp1, tp2;
  Plist dp1, path;

    /* First find the correct leaf.  path is used to help with  */
    /* freeing nodes, because nodes don't have parent pointers. */

  path = NULL;
  end = discrim_bind_end(t, root, &path);
  if (end == NULL) {
    fatal_error("discrim_bind_delete, cannot find end.");
  }

    /* Free the pointer in the leaf-list */

  tp1 = end->u.data;
  tp2 = NULL;
  while(tp1 && tp1->v != object) {
    tp2 = tp1;
    tp1 = tp1->next;
  }
  if (tp1 == NULL) {
    fatal_error("discrim_bind_delete, cannot find term.");
  }

  if (tp2 == NULL)
    end->u.data = tp1->next;
  else
    tp2->next = tp1->next;
  free_plist(tp1);

  if (end->u.data == NULL) {
    /* free tree nodes from bottom up, using path to get parents */
    end->u.kids = NULL;  /* probably not necessary */
    dp1 = path;
    while (end->u.kids == NULL && end != root) {
      parent = (Discrim) dp1->v;
      dp1 = dp1->next;
      d2 = parent->u.kids;
      d3 = NULL;
      while (d2 != end) {
	d3 = d2;
	d2 = d2->next;
      }
      if (d3 == NULL)
	parent->u.kids = d2->next;
      else
	d3->next = d2->next;
      free_discrim(d2);
      end = parent;
    }
  }

  /* free path list */

  while (path) {
    dp1 = path;
    path = path->next;
    free_plist(dp1);
  }

}  /* discrim_bind_delete */
示例#22
0
int main(int argc, char *argv[]) {
    SID_Init(&argc, &argv, NULL);

    // Fetch user inputs
    char   filename_halos_root[256];
    char   filename_catalog_root[256];
    char   filename_PHKs_root[256];
    double box_size;
    double dx;
    int    i_file_lo_in;
    int    i_file_hi_in;
    int    i_file_skip;
    strcpy(filename_halos_root, argv[1]);
    strcpy(filename_catalog_root, argv[2]);
    strcpy(filename_PHKs_root, argv[3]);
    box_size     = atof(argv[4]);
    dx           = atof(argv[5]);
    i_file_lo_in = atoi(argv[6]);
    i_file_hi_in = atoi(argv[7]);
    i_file_skip  = atoi(argv[8]);

    int i_file_lo;
    int i_file_hi;
    if(i_file_lo_in < i_file_hi_in) {
        i_file_lo = i_file_lo_in;
        i_file_hi = i_file_hi_in;
    } else {
        i_file_lo = i_file_hi_in;
        i_file_hi = i_file_lo_in;
    }

    SID_log("Generating group PH keys for files #%d->#%d...", SID_LOG_OPEN | SID_LOG_TIMER, i_file_lo, i_file_hi);
    for(int i_file = i_file_lo; i_file <= i_file_hi; i_file += i_file_skip) {
        SID_log("Processing file #%03d...", SID_LOG_OPEN | SID_LOG_TIMER, i_file);
        SID_set_verbosity(SID_SET_VERBOSITY_RELATIVE, 0);

        // Read group info from the halo catalogs
        plist_info plist;
        int *      PHK_group       = NULL;
        size_t *   PHK_group_index = NULL;
        char *     filename_number = (char *)SID_malloc(sizeof(char) * 10);
        init_plist(&plist, NULL, GADGET_LENGTH, GADGET_MASS, GADGET_VELOCITY);
        sprintf(filename_number, "%03d", i_file);
        ADaPS_store(&(plist.data), (void *)filename_number, "read_catalog", ADaPS_DEFAULT);
        read_groups(filename_halos_root, i_file, READ_GROUPS_ALL | READ_GROUPS_MBP_IDS_ONLY, &plist, filename_number);
        int n_groups_all = ((int *)ADaPS_fetch(plist.data, "n_groups_all_%s", filename_number))[0];
        int n_groups     = ((int *)ADaPS_fetch(plist.data, "n_groups_%s", filename_number))[0];

        // If there's any groups to analyze ...
        int *  n_particles_groups     = NULL;
        size_t n_particles_cumulative = 0;
        int    n_bits                 = 0; // Default value if there are no groups
        if(n_groups > 0) {
            // Fetch the halo sizes
            n_particles_groups = (int *)ADaPS_fetch(plist.data, "n_particles_group_%s", filename_number);

            // Read MBP data from halo catalogs
            SID_log("Reading most-bound-particle positions...", SID_LOG_OPEN);
            halo_properties_info group_properties;
            fp_catalog_info      fp_group_properties;
            double *             x_array = (double *)SID_malloc(sizeof(double) * n_groups);
            double *             y_array = (double *)SID_malloc(sizeof(double) * n_groups);
            double *             z_array = (double *)SID_malloc(sizeof(double) * n_groups);
            fopen_catalog(filename_catalog_root, i_file, READ_CATALOG_GROUPS | READ_CATALOG_PROPERTIES, &fp_group_properties);
            if(fp_group_properties.n_halos_total != n_groups)
                SID_exit_error("Halo counts in group files and catalogs don't match (ie. %d!=%d)", SID_ERROR_LOGIC,
                               fp_group_properties.n_halos_total, n_groups);
            for(int i_group = 0; i_group < n_groups; i_group++) {
                fread_catalog_file(&fp_group_properties, NULL, NULL, &group_properties, NULL, i_group);
                x_array[i_group] = group_properties.position_MBP[0];
                y_array[i_group] = group_properties.position_MBP[1];
                z_array[i_group] = group_properties.position_MBP[2];
                // Enforce periodic BCs
                if(x_array[i_group] < 0.)
                    x_array[i_group] += box_size;
                if(x_array[i_group] >= box_size)
                    x_array[i_group] -= box_size;
                if(y_array[i_group] < 0.)
                    y_array[i_group] += box_size;
                if(y_array[i_group] >= box_size)
                    y_array[i_group] -= box_size;
                if(z_array[i_group] < 0.)
                    z_array[i_group] += box_size;
                if(z_array[i_group] >= box_size)
                    z_array[i_group] -= box_size;
            }
            fclose_catalog(&fp_group_properties);
            SID_log("Done.", SID_LOG_CLOSE);

            // Determine the number of bits to use for the PHKs
            for(n_bits = N_BITS_MIN; (box_size / pow(2., (double)(n_bits + 1))) > dx && n_bits <= 20;)
                n_bits++;

            // Compute PHKs
            SID_log("Computing PHKs (using %d bits per dimension)...", SID_LOG_OPEN, n_bits);
            PHK_group = (int *)SID_malloc(sizeof(int) * n_groups);
            for(int i_group = 0; i_group < n_groups; i_group++) {
                // Compute the key for this group
                PHK_group[i_group] = compute_PHK_from_Cartesian(
                    n_bits, 3, (double)x_array[i_group] / box_size, (double)y_array[i_group] / box_size, (double)z_array[i_group] / box_size);
            }
            SID_free(SID_FARG x_array);
            SID_free(SID_FARG y_array);
            SID_free(SID_FARG z_array);
            SID_log("Done.", SID_LOG_CLOSE);

            // Sort PHKs
            SID_log("Sorting PHKs...", SID_LOG_OPEN);
            merge_sort((void *)PHK_group, n_groups, &PHK_group_index, SID_INT, SORT_COMPUTE_INDEX, GBP_FALSE);
            SID_log("Done.", SID_LOG_CLOSE);

            // Count the number of particles
            for(int i_group = 0; i_group < n_groups; i_group++)
                n_particles_cumulative += n_particles_groups[PHK_group_index[i_group]];
        }

        // Write results
        SID_log("Writing results for %d groups...", SID_LOG_OPEN, n_groups);
        char filename_output_properties[256];
        sprintf(filename_output_properties, "%s_%s.catalog_PHKs", filename_PHKs_root, filename_number);
        FILE *fp_PHKs = fopen(filename_output_properties, "w");
        fwrite(&n_groups, sizeof(int), 1, fp_PHKs);
        fwrite(&n_bits, sizeof(int), 1, fp_PHKs);
        fwrite(&n_particles_cumulative, sizeof(size_t), 1, fp_PHKs);
        n_particles_cumulative = 0;
        for(int i_group = 0; i_group < n_groups; i_group++) {
            int index_temp = (int)PHK_group_index[i_group];
            n_particles_cumulative += n_particles_groups[index_temp];
            fwrite(&(PHK_group[index_temp]), sizeof(int), 1, fp_PHKs);
            fwrite(&index_temp, sizeof(int), 1, fp_PHKs);
            fwrite(&n_particles_cumulative, sizeof(size_t), 1, fp_PHKs);
        }
        fclose(fp_PHKs);
        SID_log("Done.", SID_LOG_CLOSE);

        // Clean-up
        free_plist(&plist);
        if(n_groups > 0) {
            SID_free(SID_FARG PHK_group);
            SID_free(SID_FARG PHK_group_index);
        }

        SID_set_verbosity(SID_SET_VERBOSITY_DEFAULT);
        SID_log("Done.", SID_LOG_CLOSE);
    }

    SID_log("Done.", SID_LOG_CLOSE);
    SID_Finalize();
}