Exemple #1
0
static void _configfile (CustomData *data, gboolean writemode) {
    gchar *filename = g_strdup_printf("file://%s/.4deckradio", g_get_home_dir());
    g_print ("config file: %s\n", filename);
    GFile *file = g_file_new_for_uri (filename);

    if (TRUE == writemode) {
        /* Save the last folder locations to ~/.4deckradio */
        GString *configstring = g_string_new("");
        for (int i=0; i < NUM_PLAYERS; i++) {
            g_print ("deck %i folder %s\n", i, data[i].last_folder_uri);
            g_string_append_printf (configstring, "%s\n", data[i].last_folder_uri);
        }

        g_file_replace_contents (file,
                configstring->str,
                configstring->len,
                NULL, /* old etag */
                FALSE, /* backup */
                G_FILE_CREATE_PRIVATE,
                NULL, /* new etag */
                NULL, /* cancellable */
                NULL);
        g_string_free (configstring, TRUE);
    } else {
        /* load config file */
        GFileInputStream *inputstream = g_file_read (file,
                NULL,
                NULL);
        if (NULL == inputstream) {
            return;
        }

        GDataInputStream *config = g_data_input_stream_new (G_INPUT_STREAM (inputstream));

        if (NULL == config) {
            return;
        }


        GError *error = NULL;

        for (int i = 0; i < NUM_PLAYERS; i++) {
            gsize length;
            data[i].last_folder_uri =
                g_data_input_stream_read_line_utf8(config, &length, NULL, &error);
            if (NULL != error) {
                g_print ("Error reading config file: %s\n", error->message);
            }
            g_clear_error (&error);
            g_print ("Will use %s for deck %i\n", data[i].last_folder_uri, i);
        }

        g_input_stream_close (G_INPUT_STREAM (config), NULL, NULL);
        g_input_stream_close (G_INPUT_STREAM (inputstream), NULL, NULL);
        g_object_unref (config);
        g_object_unref (inputstream);
    }
    g_free (filename);
    g_object_unref (file);
}
Exemple #2
0
static gpointer
log_thread (gpointer data)
{
  GDataInputStream *data_stream = data;
  gboolean istty;
  gboolean iserror;
  gboolean closing = FALSE;
  gchar *line;
  gsize len;

  g_assert (G_IS_DATA_INPUT_STREAM (data_stream));

  iserror = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (data_stream), "IS_STDERR"));
  istty = isatty (iserror ? STDERR_FILENO : STDOUT_FILENO);

again:

  while ((line = g_data_input_stream_read_line_utf8 (data_stream, &len, NULL, NULL)))
    {
      /*
       * TODO: I'd like to have time information here too.
       */

      if (iserror)
        {
          if (istty)
            {
              /* TODO: color red */
              g_printerr ("%s\n", line);
            }
          else
            {
              g_printerr ("%s\n", line);
            }
        }
      else
        {
          g_print ("%s\n", line);
        }
      g_free (line);
    }

  if (!closing)
    {
      if (g_atomic_int_get (&build_done))
        closing = TRUE;

      /* one final attempt to flush the logs */
      g_usleep (G_USEC_PER_SEC / 20);
      goto again;
    }

  return NULL;
}
Exemple #3
0
GSList *check_file_load(GSList *ud_list, GFile *file)
{
	g_assert(file);

	char *data = NULL;
	gsize len = 0;
	GError *error = NULL;

	if (!g_file_load_contents(file, NULL, &data, &len, NULL, &error)) {
		check_file_error(file, error);
		g_error_free(error);
		return 0;
	}

	GInputStream *is = g_memory_input_stream_new_from_data(data, len, NULL);
	GDataInputStream *dis = g_data_input_stream_new(is);

	char *line = NULL;
	len = 0;
	error = NULL;

	while ((line = g_data_input_stream_read_line_utf8(dis, &len, NULL, &error))) {
		enum hash_func_e id = HASH_FUNC_INVALID;
		char *filename = NULL;
		char *digest = NULL;

		if ((len >= 8) &&
			check_file_parse_line(line, &id, &filename, &digest))
		{
			if (HASH_FUNC_IS_VALID(id))
				gui_enable_hash_func(id);

			ud_list = check_file_add_uri(ud_list, file, filename, digest);
		}

		g_free(line);
	}

	if (error) {
		check_file_error(file, error);
		g_error_free(error);
	} else
		check_file_enable_hinted_hash_func(file);

	g_object_unref(dis);
	g_object_unref(is);
	g_free(data);

	return ud_list;
}
gboolean
ide_source_snippet_parser_load_from_file (IdeSourceSnippetParser *parser,
                                          GFile                  *file,
                                          GError                **error)
{
  GFileInputStream *file_stream;
  GDataInputStream *data_stream;
  GError *local_error = NULL;
  gchar *line;
  gchar *basename = NULL;

  g_return_val_if_fail (IDE_IS_SOURCE_SNIPPET_PARSER (parser), FALSE);
  g_return_val_if_fail (G_IS_FILE (file), FALSE);

  basename = g_file_get_basename (file);

  if (basename)
    {
      if (strstr (basename, "."))
        *strstr (basename, ".") = '\0';
    }

  file_stream = g_file_read (file, NULL, error);
  if (!file_stream)
    return FALSE;

  data_stream = g_data_input_stream_new (G_INPUT_STREAM (file_stream));
  g_object_unref (file_stream);

again:
  line = g_data_input_stream_read_line_utf8 (data_stream, NULL, NULL, &local_error);
  if (line && local_error)
    {
      g_propagate_error (error, local_error);
      return FALSE;
    }
  else if (line)
    {
      ide_source_snippet_parser_feed_line (parser, basename, line);
      g_free (line);
      goto again;
    }

  ide_source_snippet_parser_finish (parser);
  g_free(basename);

  return TRUE;
}
Exemple #5
0
/* We skip commented lines and not prefixed by prefix if not NULL */
static gchar *
read_gpl_line (GDataInputStream  *stream,
               GError           **error,
               const gchar       *prefix)
{
  gchar *line;

  g_assert (G_IS_INPUT_STREAM (stream));

  while ((line = g_data_input_stream_read_line_utf8 (stream, NULL, NULL, error)))
    {
      g_strchug (line);
      if (*line == '#' || (prefix != NULL && !g_str_has_prefix (line, prefix)))
        g_free (line);
      else
        break;
    }

  return line;
}
Exemple #6
0
static gboolean
run_test (const char *filename,
          int         index)
{
  TestCase *test = test_case_new ();
  GError *error = NULL;

  GFile *file = g_file_new_for_path (filename);

  GDataInputStream *in = NULL;

  GFileInputStream *in_raw = g_file_read (file, NULL, &error);
  g_object_unref (file);
  if (in_raw == NULL)
    goto out;

  in = g_data_input_stream_new (G_INPUT_STREAM (in_raw));
  g_object_unref (in_raw);

  int line_no = 0;
  while (error == NULL)
    {
      char *line = g_data_input_stream_read_line_utf8 (in, NULL, NULL, &error);
      if (line == NULL)
        break;

      line_no++;

      int argc;
      char **argv = NULL;
      if (!g_shell_parse_argv (line, &argc, &argv, &error))
        {
          if (g_error_matches (error, G_SHELL_ERROR, G_SHELL_ERROR_EMPTY_STRING))
            {
              g_clear_error (&error);
              goto next;
            }

          goto next;
        }

      test_case_do (test, argc, argv, &error);

    next:
      if (error)
        g_prefix_error (&error, "%d: ", line_no);

      g_free (line);
      g_strfreev (argv);
    }

  {
    GError *tmp_error = NULL;
    if (!g_input_stream_close (G_INPUT_STREAM (in), NULL, &tmp_error))
      {
        if (error != NULL)
          g_clear_error (&tmp_error);
        else
          g_propagate_error (&error, tmp_error);
      }
  }

 out:
  if (in != NULL)
    g_object_unref (in);

  GError *cleanup_error = NULL;
  test_case_destroy (test, &cleanup_error);

  const char *testspos = strstr (filename, "tests/");
  char *pretty_name;
  if (testspos)
    pretty_name = g_strdup (testspos + strlen("tests/"));
  else
    pretty_name = g_strdup (filename);

  if (error || cleanup_error)
    {
      g_print ("not ok %d %s\n", index, pretty_name);

      if (error)
        g_print ("   %s\n", error->message);

      if (cleanup_error)
        {
          g_print ("   Fatal Error During Cleanup\n");
          g_print ("   %s\n", cleanup_error->message);
          exit (1);
        }
    }
  else
    {
      g_print ("ok %d %s\n", index, pretty_name);
    }

  g_free (pretty_name);

  gboolean success = error == NULL;

  g_clear_error (&error);
  g_clear_error (&cleanup_error);

  return success;
}
Exemple #7
0
void
scrollback_load (session *sess)
{
	GInputStream *stream;
	GDataInputStream *istream;
	gchar *buf, *text;
	gint lines = 0;
	time_t stamp = 0;

	if (sess->text_scrollback == SET_DEFAULT)
	{
		if (!prefs.hex_text_replay)
			return;
	}
	else
	{
		if (sess->text_scrollback != SET_ON)
			return;
	}

	if (!sess->scrollfile)
	{
		if ((buf = scrollback_get_filename (sess)) == NULL)
			return;

		sess->scrollfile = g_file_new_for_path (buf);
		g_free (buf);
	}

	stream = G_INPUT_STREAM(g_file_read (sess->scrollfile, NULL, NULL));
	if (!stream)
		return;

	istream = g_data_input_stream_new (stream);
	/*
	 * This is to avoid any issues moving between windows/unix
	 * but the docs mention an invalid \r without a following \n
	 * can lock up the program... (Our write() always adds \n)
	 */
	g_data_input_stream_set_newline_type (istream, G_DATA_STREAM_NEWLINE_TYPE_ANY);
	g_object_unref (stream);

	while (1)
	{
		GError *err = NULL;
		gsize n_bytes;

		buf = g_data_input_stream_read_line_utf8 (istream, &n_bytes, NULL, &err);

		if (!err && buf)
		{
			/*
			 * Some scrollback lines have three blanks after the timestamp and a newline
			 * Some have only one blank and a newline
			 * Some don't even have a timestamp
			 * Some don't have any text at all
			 */
			if (buf[0] == 'T' && buf[1] == ' ')
			{
				if (sizeof (time_t) == 4)
					stamp = strtoul (buf + 2, NULL, 10);
				else
					stamp = g_ascii_strtoull (buf + 2, NULL, 10); /* in case time_t is 64 bits */

				if (G_UNLIKELY(stamp == 0))
				{
					g_warning ("Invalid timestamp in scrollback file");
					continue;
				}

				text = strchr (buf + 3, ' ');
				if (text && text[1])
				{
					if (prefs.hex_text_stripcolor_replay)
					{
						text = strip_color (text + 1, -1, STRIP_COLOR);
					}

					fe_print_text (sess, text, stamp, TRUE);

					if (prefs.hex_text_stripcolor_replay)
					{
						g_free (text);
					}
				}
				else
				{
					fe_print_text (sess, "  ", stamp, TRUE);
				}
			}
			else
			{
				if (strlen (buf))
					fe_print_text (sess, buf, 0, TRUE);
				else
					fe_print_text (sess, "  ", 0, TRUE);
			}
			lines++;

			g_free (buf);
		}
		else if (err)
		{
			/* If its only an encoding error it may be specific to the line */
			if (g_error_matches (err, G_CONVERT_ERROR, G_CONVERT_ERROR_ILLEGAL_SEQUENCE))
			{
				g_warning ("Invalid utf8 in scrollback file");
				g_clear_error (&err);
				continue;
			}

			/* For general errors just give up */
			g_clear_error (&err);
			break;
		}
		else /* No new line */
		{
			break;
		}
	}

	g_object_unref (istream);

	sess->scrollwritten = lines;

	if (lines)
	{
		text = ctime (&stamp);
		buf = g_strdup_printf ("\n*\t%s %s\n", _("Loaded log from"), text);
		fe_print_text (sess, buf, 0, TRUE);
		g_free (buf);
		/*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf, NULL, NULL, NULL, 0);*/
	}
}
static gboolean
handle_install_authorized_keys (MinCloudAgentApp      *self,
                                GInputStream               *instream,
                                GCancellable               *cancellable,
                                GError                    **error)
{
  gboolean ret = FALSE;
  int fd;
  guint i;
  gs_unref_object GOutputStream *outstream = NULL;
  gs_unref_object GDataInputStream *datain = NULL;
  gs_unref_ptrarray GPtrArray *lines = g_ptr_array_new_with_free_func (g_free);

  datain = g_data_input_stream_new (instream);

  while (TRUE)
    {
      gsize len;
      GError *temp_error = NULL;
      char *line = g_data_input_stream_read_line_utf8 (datain, &len, cancellable, &temp_error);
      if (temp_error != NULL)
        {
          g_propagate_error (error, temp_error);
          g_prefix_error (error, "Reading ssh keys: ");
          goto out;
        }
      if (!line)
        break;
      g_ptr_array_add (lines, line);
    }

  (void) g_input_stream_close ((GInputStream*)datain, NULL, NULL);

  outstream = (GOutputStream*)g_file_append_to (self->authorized_keys_path, 0, cancellable, error);
  if (!outstream)
    {
      g_prefix_error (error, "Appending to '%s': ",
                      gs_file_get_path_cached (self->authorized_keys_path));
      goto out;
    }

  fd = g_file_descriptor_based_get_fd ((GFileDescriptorBased*)outstream);
  if (fchmod (fd, 0600) != 0)
    {
      int errsv = errno;
      g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
                   "Failed to chmod authorized_keys: %s",
                   g_strerror (errsv));
      goto out;
    }

  for (i = 0; i < lines->len; i++)
    {
      const char *line = lines->pdata[i];
      char nl[] = { '\n' };
      gsize bytes_written;
      if (!g_output_stream_write_all (outstream, line, strlen (line),
                                      &bytes_written,
                                      cancellable, error))
        goto out;
      if (!g_output_stream_write_all (outstream, nl, sizeof (nl),
                                      &bytes_written,
                                      cancellable, error))
        goto out;
    }

  if (!g_output_stream_flush (outstream, cancellable, error))
    goto out;

  gs_log_structured_print_id_v (MCA_KEY_INSTALLED_SUCCESS_ID,
                                "Successfully installed ssh key for '%s'",
                                "root");

  ret = TRUE;
 out:
  return ret;
}
Exemple #9
0
LibBalsaAddress *
rfc6350_parse_from_stream(GDataInputStream *stream,
						  gboolean		   *eos,
						  GError		   **error)
{
	gchar *line;
	LibBalsaAddress *result = NULL;

	line = g_data_input_stream_read_line_utf8(stream, NULL, NULL, error);
	if (line == NULL) {
		if (*error == NULL) {
			*eos = TRUE;
		}
	} else if (g_ascii_strcasecmp(line, "BEGIN:VCARD") != 0) {
		g_set_error(error, RFC6350_ERROR_QUARK, RFC6350_ERROR_BEGIN, _("malformed card, BEGIN:VCARD expected"));
		g_free(line);
	} else {
		gboolean parse_done = FALSE;

		result = libbalsa_address_new();
		while (result && (line != NULL) && !parse_done) {
			if (g_ascii_strcasecmp(line, "END:VCARD") == 0) {
				parse_done = TRUE;
				g_free(line);
			} else {
				gchar *nextline;

				/* perform unfolding (RFC 6350, sect. 3.2. "Line Delimiting and Folding") */
				nextline = g_data_input_stream_read_line_utf8(stream, NULL, NULL, error);
				while ((nextline) != NULL && ((nextline[0] == ' ') || (nextline[0] == '\t'))) {
					gchar *unfold;

					unfold = g_strconcat(line, &nextline[1], NULL);
					g_free(line);
					g_free(nextline);
					line = unfold;
					nextline = g_data_input_stream_read_line_utf8(stream, NULL, NULL, error);
				}

				/* evaluate unfolded line, drop address on error */
				if (!rfc6350_eval_line(line, result, error)) {
					g_object_unref(result);
					result = NULL;
				}

				/* process next line */
				g_free(line);
				line = nextline;
			}
		}

		if (!parse_done) {
			g_set_error(error, RFC6350_ERROR_QUARK, RFC6350_ERROR_END, _("malformed card, END:VCARD missing"));
			g_object_unref(result);
			result = NULL;
		}
	}

	/* ignore items without an Email address, fill empty full name if necessary */
	if (result != NULL) {
		if (result->address_list == NULL) {
			g_object_unref(result);
			result = NULL;
		} else if (result->full_name == NULL) {
			result->full_name = g_strdup(_("No-Name"));
		}
	}

	return result;
}
static void
handle_channels_cb (TpSimpleHandler *handler,
  TpAccount *account,
  TpConnection *connection,
  GList *channels,
  GList *request_satisfied,
  gint64 user_aciton_time,
  TpHandleChannelsContext *handler_context,
  gpointer user_data)
{
  TpChannel *channel = channels->data;
  gchar *path = g_build_filename (
    g_get_user_config_dir (),
    "phoenix",
    "auth",
    NULL);
  GFile *file = g_file_new_for_path (path);
  GFileIOStream *stream;
  GDataInputStream *input = NULL;
  char *password = NULL;
  char *line;

  if (g_list_length (channels) != 1)
    {
      GError err = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
        "Can only handle one channel at a time" };
      tp_handle_channels_context_fail (handler_context,
        &err);
      goto out;
    }

  stream = g_file_open_readwrite (file, NULL, NULL);

  if (stream == NULL)
    {
      GError err = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
        "No authenication data stored" };
      tp_handle_channels_context_fail (handler_context,
        &err);
      goto out;
    }

  input = g_data_input_stream_new (
    g_io_stream_get_input_stream (G_IO_STREAM (stream)));
  while ((line = g_data_input_stream_read_line_utf8 (input, NULL, NULL, NULL))
      != NULL)
    {
      gchar **r = g_strsplit (line, " ", 2);
      if (r[0] == NULL || r[1] == NULL)
        continue;

      if (!tp_strdiff (r[0], tp_account_get_path_suffix (account)))
        {
          password = g_strdup (r[1]);
          printf ("Found password: %s\n", password);
          g_strfreev(r);
          break;
        }
      g_strfreev(r);
    }
  g_object_unref (input);

  if (password == NULL)
    {
      GError err = { TP_ERROR, TP_ERROR_INVALID_ARGUMENT,
        "No authenication data stored for this account" };
      tp_handle_channels_context_fail (handler_context,
        &err);
      goto out;
    }

  tp_handle_channels_context_accept (handler_context);

  tp_cli_channel_interface_sasl_authentication_connect_to_sasl_status_changed
    (channel, sasl_status_changed_cb, NULL, NULL, NULL, NULL);
  provide_password (channel, password);

out:
  g_free (path);
  g_free (password);
}