rust-analyzer-transfer.c 5.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
/* rust-analyzer-transfer.c
 *
 * Copyright 2020 Günther Wagner <info@gunibert.de>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#include "rust-analyzer-transfer.h"
#include <libide-threading.h>
#include <libide-core.h>
#include <libsoup/soup.h>
#include <glib/gstdio.h>
#include <stdio.h>
#include <sys/stat.h>

struct _RustAnalyzerTransfer
{
  IdeTransfer parent_instance;
};

G_DEFINE_TYPE (RustAnalyzerTransfer, rust_analyzer_transfer, IDE_TYPE_TRANSFER)

RustAnalyzerTransfer *
rust_analyzer_transfer_new (void)
{
  return g_object_new (RUST_TYPE_ANALYZER_TRANSFER, NULL);
}

typedef struct {
  gchar buffer[6*1024];
  gsize count;
  guint64 total_bytes;
  gchar *filepath;
  GOutputStream *filestream;
  IdeTransfer *transfer;
  IdeTask *task;
} DownloadData;

static void
_downloaded_chunk (GObject      *source_object,
                   GAsyncResult *result,
                   gpointer      user_data)
{
  g_autofree gchar *statusmsg = NULL;
58
  g_autoptr(GError) error = NULL;
59 60 61
  GInputStream *stream = G_INPUT_STREAM (source_object);
  DownloadData *data = user_data;

62 63 64 65 66 67 68 69
  gsize count = g_input_stream_read_finish (stream, result, &error);
  if (error != NULL)
    {
      ide_task_return_error (data->task, g_steal_pointer (&error));
      return;
    }

  if (count == 0)
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
    {
      g_output_stream_close (data->filestream, NULL, NULL);
      g_input_stream_close (stream, NULL, NULL);
      ide_task_return_boolean (data->task, TRUE);
      g_object_unref (data->task);
      g_chmod (data->filepath, S_IRWXU);
      g_free (data->filepath);
      g_slice_free (DownloadData, data);
      return;
    }

  data->count += count;
  statusmsg = g_strdup_printf ("%.2f MB / %.2f MB", data->count / 1048576., data->total_bytes / 1048576.);
  ide_transfer_set_status (data->transfer, statusmsg);
  ide_transfer_set_progress (data->transfer, (gdouble) data->count / data->total_bytes);

86 87 88 89 90 91
  g_output_stream_write_all (data->filestream, &data->buffer, count, NULL, ide_task_get_cancellable (data->task), &error);
  if (error != NULL)
    {
      ide_task_return_error (data->task, g_steal_pointer (&error));
      return;
    }
92 93 94 95 96 97 98 99 100 101
  g_input_stream_read_async (stream, &data->buffer, sizeof (data->buffer), G_PRIORITY_DEFAULT, ide_task_get_cancellable (data->task), _downloaded_chunk, data);
}

static void
_download_lsp (GObject      *source_object,
               GAsyncResult *result,
               gpointer      user_data)
{
  g_autoptr(IdeTask) task = IDE_TASK (user_data);
  g_autoptr(GFile) file = NULL;
102
  g_autoptr(GError) error = NULL;
103 104
  SoupRequest *request = SOUP_REQUEST (source_object);
  GInputStream *stream = NULL;
105
  DownloadData *data;
106 107 108

  stream = soup_request_send_finish (request, result, NULL);

109
  data = g_slice_new0 (DownloadData);
110 111 112
  data->filepath = g_build_filename (g_get_home_dir (), ".cargo", "bin", "rust-analyzer", NULL);
  file = g_file_new_for_path (data->filepath);
  data->transfer = IDE_TRANSFER (ide_task_get_task_data (task));
113 114 115 116 117 118
  data->filestream = G_OUTPUT_STREAM (g_file_replace (file, NULL, FALSE, G_FILE_CREATE_NONE, ide_task_get_cancellable (data->task), &error));
  if (data->filestream == NULL)
    {
      ide_task_return_error (task, g_steal_pointer (&error));
      return;
    }
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
  data->total_bytes = soup_request_get_content_length (request);
  data->task = g_steal_pointer (&task);

  g_input_stream_read_async (stream, &data->buffer, sizeof (data->buffer), G_PRIORITY_DEFAULT, ide_task_get_cancellable (data->task), _downloaded_chunk, data);
}

static void
rust_analyzer_transfer_execute_async (IdeTransfer         *transfer,
                                      GCancellable        *cancellable,
                                      GAsyncReadyCallback  callback,
                                      gpointer             user_data)
{
  RustAnalyzerTransfer *self = RUST_ANALYZER_TRANSFER (transfer);
  g_autoptr(IdeTask) task = NULL;
  g_autoptr(SoupSession) session = NULL;
  g_autoptr(SoupRequest) request = NULL;
135
  g_autoptr(GError) error = NULL;
136 137 138 139 140 141 142 143 144

  g_assert (!cancellable || G_IS_CANCELLABLE (cancellable));

  task = ide_task_new (self, cancellable, callback, user_data);
  ide_task_set_source_tag (task, rust_analyzer_transfer_execute_async);
  ide_task_set_task_data (task, transfer, g_object_unref);

  session = soup_session_new ();

145 146 147 148 149
  request = soup_session_request (session, "https://github.com/rust-analyzer/rust-analyzer/releases/download/nightly/rust-analyzer-linux", &error);
  if (request == NULL)
    ide_task_return_error (task, g_steal_pointer (&error));
  else
    soup_request_send_async (request, NULL, _download_lsp, g_steal_pointer (&task));
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
}

static gboolean
rust_analyzer_transfer_execute_finish (IdeTransfer   *transfer,
                                       GAsyncResult  *result,
                                       GError       **error)
{
  gboolean ret;

  ret = ide_task_propagate_boolean (IDE_TASK (result), error);

  return ret;
}

static void
rust_analyzer_transfer_class_init (RustAnalyzerTransferClass *klass)
{
  IdeTransferClass *transfer_class = IDE_TRANSFER_CLASS (klass);

  transfer_class->execute_async = rust_analyzer_transfer_execute_async;
  transfer_class->execute_finish = rust_analyzer_transfer_execute_finish;
}

static void
rust_analyzer_transfer_init (RustAnalyzerTransfer *self)
{
  ide_transfer_set_title (IDE_TRANSFER (self), "Installing Rust Analyzer...");
}