Exemplo n.º 1
0
Arquivo: rcli.c Projeto: dseomn/rpstir
static int create2op(
    scm *scmp,
    scmcon *conp,
    char *topdir)
{
    scmkva aone;
    scmkv one;
    scmtab *mtab;
    int sta;

    if (conp == NULL || scmp == NULL || scmp->db == NULL || scmp->db[0] == 0)
    {
        LOG(LOG_ERR, "Internal error in create2op()");
        return (-1);
    }
    if (topdir == NULL || topdir[0] == 0)
    {
        LOG(LOG_ERR, "Must specify a top level repository directory");
        return (-2);
    }
    // step 1: locate the metadata table
    mtab = findtablescm(scmp, "METADATA");
    if (mtab == NULL)
    {
        LOG(LOG_ERR, "Cannot find METADATA table");
        return (-3);
    }
    // step 2: translate "topdir" into an absolute path
    tdir = r2adir(topdir);
    if (tdir == NULL)
    {
        LOG(LOG_ERR, "Invalid directory: %s", topdir);
        return (-4);
    }
    // step 3: init the metadata table
    one.column = "rootdir";
    one.value = tdir;
    aone.vec = &one;
    aone.ntot = 1;
    aone.nused = 1;
    aone.vald = 0;
    sta = insertscm(conp, mtab, &aone);
    if (sta == 0)
        LOG(LOG_NOTICE, "Init metadata table succeeded");
    else
        LOG(LOG_ERR, "Init metadata table failed: %s", geterrorscm(conp));
    return (sta);
}
Exemplo n.º 2
0
err_code
splitdf(
    char *dirprefix,
    char *dirname,
    char *fname,
    char **outdir,
    char **outfile,
    char **outfull)
{
    char *slash;
    char *work;
    char *outd = NULL;
    char *outf = NULL;
    int wsta = ERR_SCM_UNSPECIFIED;

    if (fname == NULL || fname[0] == 0)
    {
        LOG(LOG_ERR, "fname argument must not be NULL or an empty string");
        return (ERR_SCM_INVALARG);
    }
    if (outdir != NULL)
        *outdir = NULL;
    if (outfile != NULL)
        *outfile = NULL;
    if (outfull != NULL)
        *outfull = NULL;
    work = (char *)calloc(PATH_MAX, sizeof(char));
    if (work == NULL)
    {
        LOG(LOG_ERR, "out of memory");
        return (ERR_SCM_NOMEM);
    }
    /*
     * First form a path. in the special case that the prefix and the dirname
     * both null and fname contains no / characters, then use the current
     * directory
     */
    if (dirprefix == NULL && dirname == NULL && strchr(fname, '/') == NULL)
    {
        if (!getcwd(work, PATH_MAX))
            abort();
        wsta = strwillfit(work, PATH_MAX, wsta, "/");
        if (wsta < 0)
        {
            free((void *)work);
            return (wsta);
        }
        wsta = strwillfit(work, PATH_MAX, wsta, fname);
        if (wsta < 0)
        {
            free((void *)work);
            return (wsta);
        }
    }
    else
    {
        if (dirprefix != NULL && dirprefix[0] != 0)
        {
            wsta = strwillfit(work, PATH_MAX, wsta, dirprefix);
            if (wsta < 0)
            {
                free((void *)work);
                return (wsta);
            }
        }
        if (dirname != NULL && dirname[0] != 0)
        {
            if (work[0] != 0)
            {
                wsta = strwillfit(work, PATH_MAX, wsta, "/");
                if (wsta < 0)
                {
                    free((void *)work);
                    return (wsta);
                }
            }
            wsta = strwillfit(work, PATH_MAX, wsta, dirname);
            if (wsta < 0)
            {
                free((void *)work);
                return (wsta);
            }
        }
        if (work[0] != 0)
        {
            wsta = strwillfit(work, PATH_MAX, wsta, "/");
            if (wsta < 0)
            {
                free((void *)work);
                return (wsta);
            }
        }
        wsta = strwillfit(work, PATH_MAX, wsta, fname);
        if (wsta < 0)
        {
            free((void *)work);
            return (wsta);
        }
    }
    slash = strrchr(work, '/');
    if (slash == NULL)
    {
        LOG(LOG_ERR, "no slash found in current working directory: %s", work);
        free((void *)work);
        return (ERR_SCM_NOTADIR);
    }
    if (slash[1] == 0)
    {
        LOG(LOG_ERR, "working directory ends with a slash: %s", work);
        free((void *)work);
        return (ERR_SCM_BADFILE);
    }
    *slash = 0;
    outd = r2adir(work);
    if (outd == NULL)
    {
        LOG(LOG_ERR, "unable to convert relative path to absolute path: %s",
            work);
        free((void *)work);
        return (ERR_SCM_NOTADIR);
    }
    if (outdir != NULL)
        *outdir = outd;
    outf = strdup(slash + 1);
    if (outf == NULL)
    {
        LOG(LOG_ERR, "out of memory");
        return (ERR_SCM_NOMEM);
    }
    if (outfile != NULL)
        *outfile = outf;
    if (outfull != NULL)
    {
        xsnprintf(work, PATH_MAX, "%s/%s", outd, outf);
        *outfull = strdup(work);
        if (*outfull == NULL)
        {
            LOG(LOG_ERR, "out of memory");
            return (ERR_SCM_NOMEM);
        }
    }
    free((void *)work);
    return (0);
}