Example #1
0
static void
do_query_info_on_read (GVfsBackend *backend,
		       GVfsJobQueryInfoRead *job,
		       GVfsBackendHandle handle,
		       GFileInfo *info,
		       GFileAttributeMatcher *attribute_matcher)
{
  int fd, res;
  struct stat statbuf;
    
  fd = GPOINTER_TO_INT (handle);

  res = fstat (fd, &statbuf);

  if (res == -1)
    {
      int errsv = errno;

      g_vfs_job_failed (G_VFS_JOB (job), G_IO_ERROR,
			g_io_error_from_errno (errsv),
			"Error querying info in file: %s",
			g_strerror (errsv));
    }
  else
    {
      g_file_info_set_size (info, statbuf.st_size);
      g_file_info_set_attribute_uint32 (info, G_FILE_ATTRIBUTE_UNIX_DEVICE,
					statbuf.st_dev);
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED, statbuf.st_mtime);
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_ACCESS, statbuf.st_atime);
      g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_CHANGED, statbuf.st_ctime);

      g_vfs_job_succeeded (G_VFS_JOB (job));
    }
}
Example #2
0
/**
 * sets the dates to a file
 * @param fileinfo : a GFileInfo pointer obtained from an opened file
 *        (GFile *)
 * @param meta : meta_data_t * structure that contains all meta data for
 *        to set to the corresponding file.
 */
void set_dates_to_gfile(GFileInfo *fileinfo, meta_data_t *meta)
{
    if (fileinfo != NULL && meta != NULL)
        {
            g_file_info_set_attribute_uint64(fileinfo, G_FILE_ATTRIBUTE_TIME_ACCESS, meta->atime);
            g_file_info_set_attribute_uint64(fileinfo, G_FILE_ATTRIBUTE_TIME_CHANGED, meta->ctime);
            g_file_info_set_attribute_uint64(fileinfo, G_FILE_ATTRIBUTE_TIME_MODIFIED, meta->mtime);
        }
}
static void
pdf_load_job_cache_set_attributes (PdfLoadJob *job)
{
  GFileInfo *info;
  GFile *file;

  if (job->download_file != NULL)
    {
      gchar *path;

      path = g_file_get_path (job->download_file);

      /* In case the downloaded file is not the final PDF, then we
       * need to convert it.
       */
      if (g_strcmp0 (path, job->pdf_path) != 0)
        {
          /* make the file private */
          g_chmod (path, 0600);
          job->uri = g_file_get_uri (job->download_file);
          pdf_load_job_from_openoffice (job);
          g_free (path);
          return;
        }

      g_clear_object (&job->download_file);
      g_free (path);
    }

  /* make the file private */
  g_chmod (job->pdf_path, 0600);

  file = g_file_new_for_path (job->pdf_path);
  info = g_file_info_new ();

  g_file_info_set_attribute_uint64 (info, G_FILE_ATTRIBUTE_TIME_MODIFIED,
                                    job->original_file_mtime);
  g_file_set_attributes_async (file, info,
                               G_FILE_QUERY_INFO_NONE,
                               G_PRIORITY_DEFAULT,
                               job->cancellable,
                               cache_set_attributes_ready_cb,
                               job);

  g_object_unref (info);
  g_object_unref (file);
}
Example #4
0
static GConverterResult
g_zlib_decompressor_convert (GConverter *converter,
			     const void *inbuf,
			     gsize       inbuf_size,
			     void       *outbuf,
			     gsize       outbuf_size,
			     GConverterFlags flags,
			     gsize      *bytes_read,
			     gsize      *bytes_written,
			     GError    **error)
{
  GZlibDecompressor *decompressor;
  int res;

  decompressor = G_ZLIB_DECOMPRESSOR (converter);

  decompressor->zstream.next_in = (void *)inbuf;
  decompressor->zstream.avail_in = inbuf_size;

  decompressor->zstream.next_out = outbuf;
  decompressor->zstream.avail_out = outbuf_size;

  res = inflate (&decompressor->zstream, Z_NO_FLUSH);

  if (res == Z_DATA_ERROR || res == Z_NEED_DICT)
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_INVALID_DATA,
			   _("Invalid compressed data"));
      return G_CONVERTER_ERROR;
    }

  if (res == Z_MEM_ERROR)
    {
      g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_FAILED,
			   _("Not enough memory"));
      return G_CONVERTER_ERROR;
    }

    if (res == Z_STREAM_ERROR)
    {
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
		   _("Internal error: %s"), decompressor->zstream.msg);
      return G_CONVERTER_ERROR;
    }

    if (res == Z_BUF_ERROR)
      {
	if (flags & G_CONVERTER_FLUSH)
	  return G_CONVERTER_FLUSHED;

	/* Z_FINISH not set, so this means no progress could be made */
	/* We do have output space, so this should only happen if we
	   have no input but need some */

	g_set_error_literal (error, G_IO_ERROR, G_IO_ERROR_PARTIAL_INPUT,
			     _("Need more input"));
	return G_CONVERTER_ERROR;
      }

  g_assert (res == Z_OK || res == Z_STREAM_END);

  *bytes_read = inbuf_size - decompressor->zstream.avail_in;
  *bytes_written = outbuf_size - decompressor->zstream.avail_out;

#if !defined (G_OS_WIN32) || ZLIB_VERNUM >= 0x1240
  if (decompressor->header_data != NULL &&
      decompressor->header_data->gzheader.done == 1)
    {
      HeaderData *data = decompressor->header_data;

      /* So we don't notify again */
      data->gzheader.done = 2;

      data->file_info = g_file_info_new ();
      g_file_info_set_attribute_uint64 (data->file_info,
                                        G_FILE_ATTRIBUTE_TIME_MODIFIED,
                                        data->gzheader.time);
      g_file_info_set_attribute_uint32 (data->file_info,
                                        G_FILE_ATTRIBUTE_TIME_MODIFIED_USEC,
                                        0);

      if (data->filename[0] != '\0')
        g_file_info_set_attribute_byte_string (data->file_info,
                                               G_FILE_ATTRIBUTE_STANDARD_NAME,
                                               data->filename);

      g_object_notify (G_OBJECT (decompressor), "file-info");
    }
#endif /* !G_OS_WIN32 || ZLIB >= 1.2.4 */

  if (res == Z_STREAM_END)
    return G_CONVERTER_FINISHED;
  return G_CONVERTER_CONVERTED;
}
Example #5
0
GFileInfo *
gvfs_file_info_demarshal (char      *data,
			  gsize      size)
{
  guint32 num_attrs, i;
  GInputStream *memstream;
  GDataInputStream *in;
  GFileInfo *info;
  char *attr, *str, **strv;
  GFileAttributeType type;
  GFileAttributeStatus status;
  GObject *obj;
  int objtype;

  memstream = g_memory_input_stream_new_from_data (data, size, NULL);
  in = g_data_input_stream_new (memstream);
  g_object_unref (memstream);

  info = g_file_info_new ();
  num_attrs = g_data_input_stream_read_uint32 (in, NULL, NULL);

  for (i = 0; i < num_attrs; i++)
    {
      attr = read_string (in);
      type = g_data_input_stream_read_byte (in, NULL, NULL);
      status = g_data_input_stream_read_byte (in, NULL, NULL);

      switch (type)
	{
	case G_FILE_ATTRIBUTE_TYPE_STRING:
	  str = read_string (in);
	  g_file_info_set_attribute_string (info, attr, str);
	  g_free (str);
	  break;
	case G_FILE_ATTRIBUTE_TYPE_BYTE_STRING:
	  str = read_string (in);
	  g_file_info_set_attribute_byte_string (info, attr, str);
	  g_free (str);
	  break;
	case G_FILE_ATTRIBUTE_TYPE_STRINGV:
	  strv = read_stringv (in);
	  g_file_info_set_attribute_stringv (info, attr, strv);
	  g_strfreev (strv);
	  break;
	case G_FILE_ATTRIBUTE_TYPE_BOOLEAN:
	  g_file_info_set_attribute_boolean (info, attr,
					     g_data_input_stream_read_byte (in,
									    NULL,
									    NULL));
	  break;
	case G_FILE_ATTRIBUTE_TYPE_UINT32:
	  g_file_info_set_attribute_uint32 (info, attr,
					    g_data_input_stream_read_uint32 (in,
									     NULL,
									     NULL));
	  break;
	case G_FILE_ATTRIBUTE_TYPE_INT32:
	  g_file_info_set_attribute_int32 (info, attr,
					   g_data_input_stream_read_int32 (in,
									   NULL,
									   NULL));
	  break;
	case G_FILE_ATTRIBUTE_TYPE_UINT64:
	  g_file_info_set_attribute_uint64 (info, attr,
					    g_data_input_stream_read_uint64 (in,
									     NULL,
									     NULL));
	  break;
	case G_FILE_ATTRIBUTE_TYPE_INT64:
	  g_file_info_set_attribute_int64 (info, attr,
					   g_data_input_stream_read_int64 (in,
									   NULL,
									   NULL));
	  break;
	case G_FILE_ATTRIBUTE_TYPE_OBJECT:
	  objtype = g_data_input_stream_read_byte (in, NULL, NULL);
	  obj = NULL;

	  if (objtype == 1)
	    {
	      char *icon_str;

	      icon_str = read_string (in);
	      obj = (GObject *)g_icon_new_for_string  (icon_str, NULL);
	      g_free (icon_str);
	    }
	  else
	    {
	      g_warning ("Unsupported GFileInfo object type %d\n", objtype);
	      g_free (attr);
	      goto out;
	    }
	  g_file_info_set_attribute_object (info, attr, obj);
	  if (obj)
	    g_object_unref (obj);
	  break;
	case G_FILE_ATTRIBUTE_TYPE_INVALID:
	  break;
	default:
	  g_warning ("Unsupported GFileInfo attribute type %d\n", type);
	  g_free (attr);
	  goto out;
	  break;
	}
      g_file_info_set_attribute_status (info, attr, status);
      g_free (attr);
    }
  
 out:
  g_object_unref (in);
  return info;
}
static gboolean
fl_parser_fill_file_info (GFileInfo *info, const char **attr)
{
	gint i;
	
	for (i = 0; attr[i]; ++i) {
		const gchar *name;
		const gchar *value;

		name  = attr[i];
		value = attr[++i];
		
		if (strcmp (name, "name") == 0) {
			char *display_name;
			/* Apparently someone decided it was a good idea
			 * to send name="" mem-type="MMC" 
			 */
			if (!value || strcmp (value, "") == 0) {
				return FALSE;
			}

			g_file_info_set_name (info, value);
			display_name = g_filename_display_name (value);
			g_file_info_set_display_name (info, display_name);
			d(g_print ("Name: '%s'\n", display_name));
			g_free (display_name);
		}
		else if (strcmp (name, "size") == 0) {
			g_file_info_set_size (info, strtoll (value, NULL, 10));
			d(g_print ("Size: '%"G_GINT64_FORMAT"'\n", g_file_info_get_size (info)));
		}
		else if (strcmp (name, "modified") == 0) {
			GTimeVal time;

			if (g_time_val_from_iso8601 (value, &time) == FALSE)
				continue;
			g_file_info_set_modification_time (info, &time);
			d(g_print ("Modified: '%s' = '%d'\n", 
				   value, (int)time.tv_sec));
		}
		else if (strcmp (name, "created") == 0) {
			GTimeVal time;

			if (g_time_val_from_iso8601 (value, &time) == FALSE)
				continue;
			g_file_info_set_attribute_uint64 (info,
							  G_FILE_ATTRIBUTE_TIME_CREATED,
							  time.tv_sec);
			g_file_info_set_attribute_uint32 (info,
							  G_FILE_ATTRIBUTE_TIME_CREATED_USEC,
							  time.tv_usec);
			d(g_print ("Created: '%s' = '%d'\n", 
				   value, (int)time.tv_sec));
		}
		else if (strcmp (name, "accessed") == 0) {
			GTimeVal time;

			if (g_time_val_from_iso8601 (value, &time) == FALSE)
				continue;
			g_file_info_set_attribute_uint64 (info,
							  G_FILE_ATTRIBUTE_TIME_ACCESS,
							  time.tv_sec);
			g_file_info_set_attribute_uint32 (info,
							  G_FILE_ATTRIBUTE_TIME_ACCESS_USEC,
							  time.tv_usec);
			d(g_print ("Accessed: '%s' = '%d'\n", 
				   value, (int)time.tv_sec));
		}
		else if (strcmp (name, "user-perm") == 0) {
			/* The permissions don't map well to unix semantics,
			 * since the user is most likely not the same on both
			 * sides. We map the user permissions to "other" on the
			 * local side. D is treated as write, otherwise files
			 * can't be deleted through the module, even if it
			 * should be possible.
			 */
			if (strstr (value, "R")) {
				g_file_info_set_attribute_boolean (info,
								   G_FILE_ATTRIBUTE_ACCESS_CAN_READ,
								   TRUE);
			}
			if (strstr (value, "W") || strstr (value, "D")) {
				g_file_info_set_attribute_boolean (info,
								   G_FILE_ATTRIBUTE_ACCESS_CAN_WRITE,
								   TRUE);
			}
		}
		else if (strcmp (name, "group-perm") == 0) {
			/* Ignore for now */
			d(g_print ("Group permissions: '%s'\n", value));
		}
		else if (strcmp (name, "other-perm") == 0) {
			/* Ignore for now */
			d(g_print ("Other permissions: '%s'\n", value));
		}
		else if (strcmp (name, "owner") == 0) {
			/* Ignore for now */
			d(g_print ("Owner: '%s'\n", value));
		}
		else if (strcmp (name, "group") == 0) {
			/* Ignore for now */
			d(g_print ("Group: '%s'\n", value));
		}
		else if (strcmp (name, "type") == 0) {
			g_file_info_set_content_type (info, value);
			d(g_print ("Mime-Type: '%s'\n", value));
		}
		else if (strcmp (name, "xml:lang") == 0) {
			d(g_print ("Lang: '%s'\n", value));
		}
		else if (strcmp (name, "mem-type") == 0) {
			guint device;

			if (value == NULL || value[0] == '\0')
				continue;

			device = om_mem_type_id_from_string (value);
			g_file_info_set_attribute_uint32 (info,
							 G_FILE_ATTRIBUTE_UNIX_RDEV,
							 device);
			d(g_print ("Mem-Type: '%s' (%d)\n",
				   value, device));
		}
		else {
			d(g_print ("Unknown Attribute: %s = %s\n",
				   name, value));
		}
	}

	if (g_file_info_get_name (info) == NULL) { /* Required attribute */
		/* Set error */
		return FALSE;
	}
	
	return TRUE;
}
Example #7
0
static gboolean
project_import_generate_file (AnjutaPluginDescription *backend, ProjectImportDialog *import_dialog,
                              GFile *project_file)
{
	/* Of course we could do some more intelligent stuff here
	 * and check which plugins are really needed */
	
	GFile* source_file = NULL;
	gchar *backend_id = NULL;
	GError* error = NULL;

	if (!anjuta_plugin_description_get_string (backend, "Project", "Supported-Project-Types", &backend_id))
	{
		if (!strcmp (backend_id, "automake"))
			source_file = g_file_new_for_path (AM_PROJECT_FILE);
		else if (!strcmp (backend_id, "make"))
			source_file = g_file_new_for_path (MKFILE_PROJECT_FILE);
		else if (!strcmp (backend_id, "directory"))
			source_file = g_file_new_for_path (DIRECTORY_PROJECT_FILE);
	}
	g_free (backend_id);
	
	if (source_file != NULL)
	{
		/* Use a default project file */
		if (!g_file_copy (source_file, project_file, 
				G_FILE_COPY_NONE,
				NULL,
				NULL,
				NULL,
				&error))
		{
			if (error->domain == G_IO_ERROR && error->code == G_IO_ERROR_EXISTS)
			{
				gchar *prjfile = g_file_get_parse_name (project_file);
				if (anjuta_util_dialog_boolean_question (GTK_WINDOW (import_dialog), FALSE,
						_("A file named \"%s\" already exists. "
						  "Do you want to replace it?"), prjfile))
				{
					g_error_free (error);
					error = NULL;
					g_file_copy (source_file, project_file,
							G_FILE_COPY_OVERWRITE,
							NULL,
							NULL,
							NULL,
							&error);
				}
				g_free (prjfile);
			}
		}

		if (!error)
		{
			time_t ctime = time(NULL);
			GFileInfo* file_info = g_file_info_new();
			g_file_info_set_attribute_uint64(file_info, 
					"time::modified",
					ctime);
			g_file_info_set_attribute_uint64(file_info, 
					"time::created",
					ctime);
			g_file_info_set_attribute_uint64(file_info, 
					"time::access",
					ctime);
			g_file_set_attributes_from_info (project_file, 
					file_info,
					G_FILE_QUERY_INFO_NONE,
					NULL, NULL);

			g_object_unref (G_OBJECT(file_info));;
		}
	}
	else
	{
		/* For unknown project backend we use the directory project file and
		 * replace the backend plugin with the right one */

		gchar *content;
		gsize length;

		source_file = g_file_new_for_path (DIRECTORY_PROJECT_FILE);
		if (g_file_load_contents (source_file, NULL, &content, &length, NULL, &error))
		{
			GString *buffer;
			const gchar *pos;
			const gchar *plugin;
			const gchar *end_plugin;
			gssize len;

			buffer = g_string_new_len (content, length);
			pos = buffer->str;
			len = buffer->len;
			for (;;)
			{
				plugin = g_strstr_len (pos, len, "<plugin ");
				if (plugin == NULL) break;
				
				end_plugin = g_strstr_len (plugin, len - (plugin - pos), "</plugin>");
				if (end_plugin == NULL) break;
				
				if (g_strstr_len (plugin, end_plugin - plugin, "\"IAnjutaProjectBackend\"") != NULL) break;

				pos = end_plugin + 9;
				len -= (end_plugin + 9 - pos);
			}

			if ((plugin == NULL) || (end_plugin == NULL))
			{
				g_set_error (&error, ianjuta_project_backend_error_quark(),0, "Unable to find backend plugin");
			}
			else
			{
				/* Replace directory backend with right one */
				GString *str;
				GFileOutputStream *stream;
				gchar *name = NULL;
				gchar *plugin_id = NULL;

				anjuta_plugin_description_get_string (backend, "Anjuta Plugin", "Name", &name);
				anjuta_plugin_description_get_string (backend, "Anjuta Plugin", "Location", &plugin_id);

				str = g_string_new (NULL);
				g_string_printf (str, "<plugin name= \"%s\"\n"
				                 "            mandatory=\"yes\">\n"
				                 "         <require group=\"Anjuta Plugin\"\n"
				                 "                  attribute=\"Location\"\n"
				                 "                  value=\"%s\"/>\n"
				                 "         <require group=\"Anjuta Plugin\"\n"
				                 "                  attribute=\"Interfaces\"\n"
				                 "                  value=\"IAnjutaProjectBackend\"/>\n"
				                 "    ", name, plugin_id);
					
				g_string_erase (buffer, plugin - buffer->str, end_plugin - plugin);
				g_string_insert_len (buffer, plugin - buffer->str, str->str, str->len);

				g_string_free (str, TRUE);

				stream = g_file_create (project_file, G_FILE_CREATE_NONE, NULL, &error);
				if (stream == NULL && error->domain == G_IO_ERROR && error->code == G_IO_ERROR_EXISTS)
				{
					gchar *prjfile = g_file_get_parse_name (project_file);
					if (anjuta_util_dialog_boolean_question (GTK_WINDOW (import_dialog), FALSE,
							_("A file named \"%s\" already exists. "
							  "Do you want to replace it?"), prjfile))
					{
						g_error_free (error);
						error = NULL;
						stream = g_file_replace (project_file, NULL, FALSE, G_FILE_CREATE_REPLACE_DESTINATION, NULL, &error);
					}
					g_free (prjfile);
				}
					
				if (stream != NULL)
				{
					gsize written;
					
					g_output_stream_write_all (G_OUTPUT_STREAM (stream), buffer->str, buffer->len, &written, NULL, &error);
					g_output_stream_close (G_OUTPUT_STREAM (stream), NULL, NULL);
				}
			}

			g_string_free (buffer, TRUE);
			g_free (content);
		}
	}
	g_object_unref (source_file);

	if (error)
	{
		gchar *prjfile;

		prjfile = g_file_get_parse_name (project_file);
		
		/* show the dialog since it may be hidden */
		gtk_widget_show (GTK_WIDGET (import_dialog));
		
		anjuta_util_dialog_error (GTK_WINDOW (import_dialog),
				_("A file named \"%s\" cannot be written: %s. "
				  "Check if you have write access to the project directory."),
				  prjfile, error->message);
		g_free (prjfile);
		g_error_free (error);
		
		return FALSE;

	}

	return TRUE;
}