Example #1
0
static int db_getattr(backend_store_interface* i, const char* rel_path, struct stat* stbuf) {
	int ret = 0;

	logger_logf(LOG(i), "rel_path = %s", rel_path);

	memset(stbuf, 0, sizeof(struct stat));

    if(strcmp(rel_path, "/") == 0) {
        stbuf->st_mode = S_IFDIR | 0755;
        stbuf->st_nlink = 2;
        logger_logf(LOG(i), "slash");
    } else if (file_exists(rel_path)) {
    	stbuf->st_mode = get_mode(rel_path);
    	stbuf->st_nlink = 1;
    	stbuf->st_uid = get_uid(rel_path);
    	stbuf->st_gid = get_gid(rel_path);
    	stbuf->st_size = get_size(rel_path);
    	stbuf->st_blksize = 4096;
    	stbuf->st_atim.tv_sec = get_access_time(rel_path);
    	stbuf->st_mtim.tv_sec = get_modification_time(rel_path);
    	stbuf->st_ctim.tv_sec = get_status_change_time(rel_path);
    } else {
    	ret = -ENOENT;
    	logger_logf(LOG(i), "-ENOENT");
    }

	return ret;
}
Example #2
0
void build_ustar_header_from_file(FILE_HEADER* header, char* filename) {
	struct stat buffer;
	errno = 0;

	if(stat(filename, &buffer) == 0 && errno == 0) {
		clean_up_header(header);

		get_prefix_and_name(filename, header->name, header->prefix);
		get_file_mode(header->mode, buffer.st_mode);
		get_id(header->uid, buffer.st_uid);
		get_id(header->gid, buffer.st_gid);
		get_size(header->size, buffer.st_size);
		get_modification_time(header->mtime, buffer.st_mtime);
		get_file_type(header->typeflag, buffer.st_mode);
		strcpy(header->magic, "ustar ");
		strcpy(header->version, " \0");
		get_username(header->uname, buffer.st_uid);
		get_groupname(header->gname, buffer.st_gid);
		get_device_numbers(header->devmajor, header->devminor, buffer.st_dev);
		calculate_checksum(header);
	}
	else
		fprintf(stderr, "%s '%s': %s\n", STAT_FAILURE_ERR, filename,
															strerror(errno));
}
Example #3
0
bool BackupSystem::file_has_changed(version_number_t &dst, FilishFso &new_file){
	dst = invalid_version_number;
	auto path = new_file.get_mapped_path();
	FileSystemObject *old_fso = nullptr;
	for (auto &fso : this->old_objects){
		auto found = fso->find(path);
		if (!found || found->is_directoryish())
			continue;
		old_fso = found;
		break;
	}
	if (!old_fso){
		new_file.compute_hash();
		return true;
	}
	zekvok_assert(old_fso);
	auto old_file = static_cast<FilishFso *>(old_fso);
	auto criterium = this->get_change_criterium(new_file);
	bool ret;
	switch (criterium){
		case ChangeCriterium::ArchiveFlag:
			ret = new_file.get_archive_flag();
			break;
		case ChangeCriterium::Size:
			ret = new_file.get_size() != old_file->get_size();
			break;
		case ChangeCriterium::Date:
			ret = new_file.get_modification_time() != old_file->get_modification_time();
			break;
		case ChangeCriterium::Hash:
			ret = compare_hashes(new_file, *old_file);
			break;
		case ChangeCriterium::HashAuto:
			ret = this->file_has_changed(new_file, *old_file);
			break;
		default:
			throw InvalidSwitchVariableException();
	}
	if (!ret){
		auto &hash = old_file->get_hash();
		if (hash.valid)
			new_file.set_hash(hash.digest);
		auto v = old_file->get_latest_version();
		new_file.set_latest_version(v);
		new_file.set_stream_id(old_file->get_stream_id());
		dst = v;
	}
	return ret;
}
static gboolean process_image(gpointer parent)
{
    gboolean success = TRUE;
    
    image_output imageout = (image_output)g_malloc(sizeof(struct imageout_str));
    char* orig_filename = NULL;
    char* orig_basename = NULL;
    char* orig_file_ext = NULL;
    char* output_file_comp = NULL;
    
    // store original file path and name 
    orig_filename = g_slist_nth (bimp_input_filenames, processed_count)->data;
    orig_basename = g_strdup(comp_get_filename(orig_filename)); 
    
    // store original extension and check error cases 
    orig_file_ext = g_strdup(strrchr(orig_basename, '.'));
    if (orig_file_ext == NULL) {
        /* under Linux, GtkFileChooser lets to pick an image file without extension, but GIMP cannot 
         * save it back if its format remains unchanged. Operation can continue only if a MANIP_CHANGEFORMAT
         * is present */
        if (list_contains_changeformat) {
            orig_file_ext = g_malloc0(sizeof(char));
        }        
        else {
            bimp_show_error_dialog(g_strdup_printf(_("Can't save image \"%s\": input file has no extension.\nYou can solve this error by adding a \"Change format or compression\" step"), orig_basename), bimp_window_main);
            success = FALSE;
            goto process_end;
        }
    }
    else if (g_ascii_strcasecmp(orig_file_ext, ".svg") == 0 && !list_contains_changeformat) {
        bimp_show_error_dialog(g_strdup_printf(_("GIMP can't save %s back to its original SVG format.\nYou can solve this error by adding a \"Change format or compression\" step"), orig_basename), bimp_window_main);
        success = FALSE;
        goto process_end;
    }
    
    g_print("\nWorking on file %d of %d (%s)\n", processed_count + 1, total_images, orig_filename);
    bimp_progress_bar_set(((double)processed_count)/total_images, g_strdup_printf(_("Working on file \"%s\"..."), orig_basename));

    // rename and save process... 
    orig_basename[strlen(orig_basename) - strlen(orig_file_ext)] = '\0'; // remove extension from basename 
    
    // check if a rename pattern is defined 
    if(list_contains_rename) {
        g_print("Applying RENAME...\n");
        apply_rename((rename_settings)(bimp_list_get_manip(MANIP_RENAME))->settings, imageout, orig_basename);
    }
    else {
        imageout->filename = orig_basename;
    }

    // To keep the folder hierarchy 
    if (common_folder_path == NULL)    {
        // Not selected or required, everything goes into the same destination folder
        output_file_comp = g_malloc0(sizeof(char));
    }
    else {
        // keep folders to add to output path
        output_file_comp = 
            g_strndup(&orig_filename[strlen(common_folder_path)+1],
            strlen(orig_filename)-(strlen(common_folder_path)+1)
            -strlen(orig_basename)-strlen(orig_file_ext)); 
    }
    
    if (strlen(output_file_comp) > 0) {
#ifdef _WIN32        
        // Clean output_file_comp
        // Should only be concerned for ':' in Drive letter
        int i;
        for (i = 0; i < strlen(output_file_comp); ++i)
            if ( output_file_comp[i] == ':' )
                output_file_comp[i] = '_';
#endif
        // Create path if needed
        g_mkdir_with_parents(
            g_strconcat(bimp_output_folder, FILE_SEPARATOR_STR, output_file_comp, NULL), 
            0777
        );
    }
    
    // save the final image in output dir with proper format and params 
    format_type final_format = -1;
    format_params params = NULL;
    
    if(list_contains_changeformat) {
        changeformat_settings settings = (changeformat_settings)(bimp_list_get_manip(MANIP_CHANGEFORMAT))->settings;
        final_format = settings->format;
        params = settings->params;

        g_print("Changing FORMAT to %s\n", format_type_string[final_format][0]);
        imageout->filename = g_strconcat(imageout->filename, ".", format_type_string[final_format][0], NULL); // append new file extension 
        imageout->filepath = g_strconcat(bimp_output_folder, FILE_SEPARATOR_STR, output_file_comp, imageout->filename, NULL); // build new path 
    }
    // TO CHECK what apply_userdef does once coded 

    else if (list_contains_savingplugin) {
        // leave filename without extension and proceed calling each saving plugin
        imageout->filename = g_strconcat(imageout->filename, ".dds", NULL);
        imageout->filepath = g_strconcat(bimp_output_folder, FILE_SEPARATOR_STR, output_file_comp, imageout->filename, NULL); // build new path 
        
        GSList *iterator = NULL;
        manipulation man;
        for (iterator = bimp_selected_manipulations; iterator; iterator = iterator->next) {
            man = (manipulation)(iterator->data);
            if (man->type == MANIP_USERDEF && strstr(((userdef_settings)(man->settings))->procedure, "-save") != NULL) {
                /* found a saving plugin, execute it
                // TODO!!!! This won't work yet, we need a way to extract the file extension managed by the selected saving plugin
                 * e.g. "file-dds-save" -> "dds" (don't do it with regexp on plugin's name... too easy...) */
                apply_userdef((userdef_settings)(man->settings), imageout);
            }
        }
    }
    else {
        // if not specified, save in original format 
        imageout->filename = g_strconcat(imageout->filename, orig_file_ext, NULL); // append old file extension     
        imageout->filepath = g_strconcat(bimp_output_folder, FILE_SEPARATOR_STR, output_file_comp, imageout->filename, NULL); // build new path         
        final_format = -1;    
    }
    
    // check if writing possible 
    gboolean will_overwrite = FALSE;
    if (bimp_opt_alertoverwrite != BIMP_OVERWRITE_SKIP_ASK) {
        // file already exists ?
        will_overwrite = g_file_test(imageout->filepath, G_FILE_TEST_IS_REGULAR);        
        if (will_overwrite) {
            // "Don't overwrite" without confirmation
            if (bimp_opt_alertoverwrite == BIMP_DONT_OVERWRITE_SKIP_ASK) {
                g_print("Destination file already exists and won't be overwritten\n");
                goto process_end;
            }
            else {
                // Ask what to do
                int ow_res = overwrite_result(imageout->filepath, parent);
                if (ow_res == 0) {
                    g_print("Destination file already exists and user select to don't overwrite\n");
                    goto process_end;
                }
            }
        }
    }
    
    // apply all the main manipulations 
    bimp_apply_drawable_manipulations(imageout, (gchar*)orig_filename, (gchar*)orig_basename); 
    
    time_t mod_time = -1;
    if (will_overwrite && bimp_opt_keepdates) {
        // I must keep the dates even if the file has been overwritten
        mod_time = get_modification_time(imageout->filepath);
        if (mod_time == -1) g_print("An error occurred when retrieving the modification date of file.\n");
    }
    
    // Save 
    g_print("Saving file %s in %s\n", imageout->filename, imageout->filepath);
    image_save(final_format, imageout, params);
    
    if (will_overwrite && bimp_opt_keepdates && mod_time > -1) {
        // replace with the old dates
        int res = set_modification_time(imageout->filepath, mod_time);
        if (res == -1) g_print("An error occurred when replacing the modification date of file.\n");
    }

    gimp_image_delete(imageout->image_id); // is it useful? 
    
process_end:

    g_free(orig_basename);
    g_free(orig_file_ext);
    g_free(output_file_comp);
    g_free(imageout->filename);
    g_free(imageout->filepath);
    g_free(imageout);

    processed_count++;
    if (success) success_count++; 
    
    // TODO: errors check here 
    if (!bimp_is_busy) {
        bimp_progress_bar_set(0.0, _("Operations stopped"));
        g_print("\nStopped, %d files processed.\n", processed_count);
        return FALSE;
    }
    else {
        if (processed_count == total_images) {
            int errors_count = processed_count - success_count;
            bimp_progress_bar_set(1.0, g_strdup_printf(_("End, all files have been processed with %d errors"), errors_count));
            g_print("\nEnd, %d files have been processed with %d errors.\n", processed_count, errors_count);
            
            bimp_set_busy(FALSE);
            
            return FALSE;
        }
        else {
            return TRUE;
        }
    }
}