int do_extract_from_opened_pack_archive_onefile(
    unzFile uf,
    const char* filename,
    int opt_extract_without_path,
    const char* prefix_extracting_name,
    int transform_path_separator, int quiet)
{
    int iCaseSensitivity = 1;
    if (unzLocateFile(uf,filename,iCaseSensitivity)!=UNZ_OK)
    {
        u_printf("file %s not found in the zipfile\n",filename);
        return 2;
    }

    if (is_last_char_path_separator(prefix_extracting_name))
    {
        if (quiet == 0)
            u_printf("Creating extracting directory: %s\n",prefix_extracting_name);
        mkDirPortable(prefix_extracting_name);
    }


    if (do_extract_from_opened_pack_archive_currentfile(uf,&opt_extract_without_path,prefix_extracting_name,transform_path_separator,quiet) == UNZ_OK)
        return 0;
    else
        return 1;
}
int af_create_folder_unlogged(const char*folderName)
{
    //if (is_filename_in_abstract_file_space(folderName)==0)
        return mkDirPortable(folderName);
    /*
    else
    {
        int retValue =  0;
        return retValue;
    }
     */
}
int do_extract_from_opened_pack_archive(
    unzFile uf,
    int opt_extract_without_path,
    const char* prefix_extracting_name,
    int transform_path_separator, int quiet)
{
    uLong i;
    unz_global_info gi;
    int err;
    int retValue = 0;

    err = unzGetGlobalInfo(uf,&gi);
    if (err!=UNZ_OK)
    {
        u_printf("error %d with zipfile in unzGetGlobalInfo \n",err);
        return 1;
    }

    if (gi.number_entry != 0)
    {
        err = unzGoToFirstFile(uf);
        if (err!=UNZ_OK)
        {
            u_printf("error %d with zipfile in unzGetGlobalInfo \n",err);
            return 1;
        }
    }

    if (is_last_char_path_separator(prefix_extracting_name))
    {
        if (quiet == 0)
            u_printf("Creating extracting directory: %s\n",prefix_extracting_name);
        mkDirPortable(prefix_extracting_name);
    }

    for (i=0;i<gi.number_entry;i++)
    {
        if (do_extract_from_opened_pack_archive_currentfile(uf,&opt_extract_without_path,prefix_extracting_name,transform_path_separator,quiet) != UNZ_OK)
            break;

        if ((i+1)<gi.number_entry)
        {
            err = unzGoToNextFile(uf);
            if (err!=UNZ_OK)
            {
                u_printf("error %d with zipfile in unzGoToNextFile\n",err);
                retValue = 1;
            }
        }
    }

    return retValue;
}
static int mkDirRecursiveIfNeeded(const char* dir_name)
{
    int res_mk = mkDirPortable(dir_name);
    if (res_mk == 0)
        return 0;

    int len_dir = (int)strlen(dir_name);
    int last_separator = -1;
    for (int i = 0;i < len_dir;i++) {
        if ((*(dir_name + i) == '\\') || (*(dir_name + i) == '/'))
            last_separator = i;
    }

    if (last_separator == -1)
        return res_mk;

    char* up_dir_name = (char*)malloc(last_separator + 1);
    memcpy(up_dir_name, dir_name, last_separator);
    *(up_dir_name + last_separator) = '\0';
    mkDirRecursiveIfNeeded(up_dir_name);
    free(up_dir_name);

    return mkDirPortable(dir_name);
}
Esempio n. 5
0
int make_directory(char *path){
	return mkDirPortable(path);
}
static int do_extract_from_opened_pack_archive_currentfile(
    unzFile uf,
    const int* popt_extract_without_path,
    const char* prefix_extracting_name,
    int transform_path_separator, int quiet)
{
    char filename_inzip[0x200];
    char* filename_withoutpath;
    char* p;
    int err=UNZ_OK;
    ABSTRACTFILE *fout=NULL;
    void* buf;
    uInt size_buf;

    unz_file_info file_info;
    err = unzGetCurrentFileInfo(uf,&file_info,filename_inzip,sizeof(filename_inzip),NULL,0,NULL,0);

    if (err!=UNZ_OK)
    {
        error("error %d with zipfile in unzGetCurrentFileInfo\n",err);
        return err;
    }

    size_buf = UNPACK_WRITEBUFFERSIZE;
    buf = (void*)malloc(size_buf);
    if (buf==NULL)
    {
        error("Error allocating memory\n");
        return UNZ_INTERNALERROR;
    }

    p = filename_withoutpath = filename_inzip;
    while ((*p) != '\0')
    {
        if (((*p)=='/') || ((*p)=='\\'))
            filename_withoutpath = p+1;
        p++;
    }

    if ((*filename_withoutpath)=='\0')
    {
        if ((*popt_extract_without_path)==0)
        {
            {
                size_t len_prefix = strlen(prefix_extracting_name);
                size_t len_directory_in_zip = strlen(filename_inzip);
                size_t total_len = len_prefix + len_directory_in_zip;
                char* provis_dir_name = NULL;
                if (len_directory_in_zip > 0)
                    provis_dir_name = (char*)malloc(total_len + 2);
                if (provis_dir_name)
                {
                    strcpy(provis_dir_name, prefix_extracting_name);
                    strcpy(provis_dir_name + len_prefix, filename_inzip);
                    transform_fileName_separator(provis_dir_name + len_prefix, transform_path_separator);
                    if (quiet == 0)
                        u_printf("creating directory: %s\n", provis_dir_name);
                    mkDirPortable(provis_dir_name);
                    free(provis_dir_name);
                }
            }
        }
    }
    else
    {
        char* previousPathCreated = NULL;
        const char* write_filename;
        int skip=0;

        if ((*popt_extract_without_path)==0)
            write_filename = filename_inzip;
        else
            write_filename = filename_withoutpath;

        err = unzOpenCurrentFile(uf);
        if (err!=UNZ_OK)
        {
            error("error %d with zipfile in unzOpenCurrentFilePassword\n",err);
        }



        if ((skip==0) && (err==UNZ_OK))
        {
            char* provis_concat_name = NULL;
            if (prefix_extracting_name == NULL)
                prefix_extracting_name = "";

            if (prefix_extracting_name != NULL)
                {
                    size_t len_prefix = strlen(prefix_extracting_name);
                    size_t total_len = len_prefix + strlen(write_filename);
                    provis_concat_name = (char*)malloc(total_len+2);
                    if (provis_concat_name != NULL)
                    {
                        strcpy(provis_concat_name,prefix_extracting_name);
                        strcat(provis_concat_name,write_filename);
                        transform_fileName_separator(provis_concat_name + len_prefix, transform_path_separator);
                    }
                }

            const char* useFileName = (provis_concat_name != NULL) ? provis_concat_name : write_filename;

            if ((*popt_extract_without_path)==0)
            {
                char* newPathOnlyPortion = (char*)malloc(strlen(useFileName)+1);
                if (newPathOnlyPortion != NULL)
                {
                    strcpy(newPathOnlyPortion,useFileName);
                    removeNoPathPortionInFileName(newPathOnlyPortion);

                    int can_skip_creating = 0;
                    if (previousPathCreated != NULL)
                        if (strcmp(previousPathCreated,newPathOnlyPortion) == 0)
                            can_skip_creating = 1;

                    if (can_skip_creating == 0)
                    {
                        if (quiet == 0)
                            u_printf("Creating directory: %s\n",newPathOnlyPortion);
                        mkDirPortable(newPathOnlyPortion);
                    }

                    if (previousPathCreated != NULL)
                        free(previousPathCreated);
                    previousPathCreated = newPathOnlyPortion;
                }
            }

            fout=af_fopen(useFileName,"wb");

            if (quiet == 0)
                u_printf("extracting %s to %s...",filename_inzip,useFileName);

            if (fout==NULL)
            {
                error("error opening %s\n",useFileName);
            }

            if (provis_concat_name != NULL)
                free(provis_concat_name);

            /* some zipfile don't contain directory alone before file */
            if ((fout==NULL) && ((*popt_extract_without_path)==0) &&
                                (filename_withoutpath!=(char*)filename_inzip))
            {
                char c=*(filename_withoutpath-1);
                *(filename_withoutpath-1)='\0';
                mkDirPortable(write_filename);
                *(filename_withoutpath-1)=c;
                fout=af_fopen(write_filename,"wb");
            }



            if (quiet == 0)
                u_printf(" done\n");
        }

        if (fout!=NULL)
        {
            af_setsizereservation(fout, (long)file_info.uncompressed_size);
            do
            {
                err = unzReadCurrentFile(uf,buf,(unsigned)size_buf);
                if (err<0)
                {
                    error("error %d with zipfile in unzReadCurrentFile\n",err);
                    break;
                }
                if (err>0)
                    if (af_fwrite(buf,err,1,fout)!=1)
                    {
                        error("error in writing extracted file\n");
                        err=UNZ_ERRNO;
                        break;
                    }
            }
            while (err>0);
            if (fout)
                    af_fclose(fout);
            /*
            if (err==0)
                change_file_date(write_filename,file_info.dosDate,
                                 file_info.tmu_date);
            */
        }

        if (err==UNZ_OK)
        {
            err = unzCloseCurrentFile (uf);
            if (err!=UNZ_OK)
            {
                error("error %d with zipfile in unzCloseCurrentFile\n",err);
            }
        }
        else
            unzCloseCurrentFile(uf); /* don't lose the error */


        if (previousPathCreated != NULL)
           free(previousPathCreated);
    }

    free(buf);
    return err;
}
Esempio n. 7
0
static int create_path(const char* path) {
	return mkDirPortable(path);
}
int main_DuplicateFile(int argc,char* const argv[]) {
if (argc==1) {
   usage();
   return SUCCESS_RETURN_CODE;
}

const char *input_file = NULL;
const char *output_file = NULL;
int do_delete=0;
int do_recursive_delete=0;
int do_move=0;
int do_make_dir=0;
int do_make_dir_parent=0;
int val,index=-1;
bool only_verify_arguments = false;
UnitexGetOpt options;

while (EOF!=(val=options.parse_long(argc,argv,optstring_DuplicateFile,lopts_DuplicateFile,&index))) {
   switch(val) {
   case 'a': do_make_dir = 1; break;
   case 'p': do_make_dir_parent = 1; break;
   case 'd': do_delete = 1; break;
   case 'r': do_delete = do_recursive_delete = 1; break;
   case 'i': if (options.vars()->optarg[0]=='\0') {
                error("Empty input argument\n");
                return USAGE_ERROR_CODE;
             }
             input_file = options.vars()->optarg;
             break;
   case 'm': if (options.vars()->optarg[0]=='\0') {
                error("Empty move argument\n");
                return USAGE_ERROR_CODE;
             }
             input_file = options.vars()->optarg;
             do_move=1;
             break;
   case 'V': only_verify_arguments = true;
             break;
   case 'h': usage();
             return SUCCESS_RETURN_CODE;
   case ':': index==-1 ? error("Missing argument for option -%c\n",options.vars()->optopt):
                         error("Missing argument for option --%s\n",lopts_DuplicateFile[index].name);
             return USAGE_ERROR_CODE;
   case '?': index==-1 ? error("Invalid option -%c\n",options.vars()->optopt) :
                         error("Invalid option --%s\n",options.vars()->optarg);
             return USAGE_ERROR_CODE;
   case 'k':
   case 'q': /* ignore -k and -q parameter instead to raise an error */
             break;
   }
   index=-1;
}

if (options.vars()->optind!=argc-1) {
   error("Invalid arguments: rerun with --help\n");
   return USAGE_ERROR_CODE;
}

output_file = argv[options.vars()->optind];

if ((input_file==NULL) && (do_delete==0) && (do_make_dir==0) && (do_make_dir_parent ==0)) {
   error("You must specify the input_file file\n");
   return USAGE_ERROR_CODE;
}

if ((input_file!=NULL) && (do_delete==1)) {
   error("You cannot specify input_file when delete\n");
   return USAGE_ERROR_CODE;
}
if (output_file==NULL) {
   error("You must specify the output_file file\n");
   return USAGE_ERROR_CODE;
}

if (only_verify_arguments) {
  // freeing all allocated memory
  return SUCCESS_RETURN_CODE;
}

int result = 0;
if (input_file != NULL) {
    if (do_move == 0) {
        u_printf("copy file %s to %s\n",input_file,output_file);
        /* af_copy return 0 if success, -1 with reading problem, 1 writing problem */
        result=af_copy(input_file,output_file);
    } else {
        u_printf("move file %s to %s\n",input_file,output_file);
        result=af_rename(input_file,output_file);
    }
} else if (do_make_dir != 0) {
    u_printf("make dir %s\n", output_file);
    result = mkDirPortable(output_file);
} else if (do_make_dir_parent != 0) {
    u_printf("make dir %s with parent\n", output_file);
    result = mkDirRecursiveIfNeeded(output_file);
} else {
    if (do_recursive_delete == 0) {
        u_printf("remove file %s\n",output_file);
        result=af_remove(output_file);
    } else {
        u_printf("remove folder %s\n", output_file);
        af_remove_folder(output_file);
        result=0;
    }
}
u_printf((result==0) ? "Done.\n" : "Unsucessfull.\n");
return result;
}
Esempio n. 9
0
int make_cassys_directory(const char *path){
	return mkDirPortable(path);
}