Exemple #1
0
static GHashTable *
gftp_gen_dir_hash (gftp_request * request, int *ret)
{
  GHashTable * dirhash;
  gftp_file * fle;
  off_t *newsize;

  dirhash = g_hash_table_new (string_hash_function, string_hash_compare);
  *ret = gftp_list_files (request);
  if (*ret == 0)
    {
      fle = g_malloc0 (sizeof (*fle));
      while (gftp_get_next_file (request, NULL, fle) > 0)
        {
          newsize = g_malloc0 (sizeof (*newsize));
          *newsize = fle->size;
          g_hash_table_insert (dirhash, fle->file, newsize);
          fle->file = NULL;
          gftp_file_destroy (fle, 0);
        }
      gftp_end_transfer (request);
      g_free (fle);
    }
  else
    {
      g_hash_table_destroy (dirhash);
      dirhash = NULL;
    }

  return (dirhash);
}
Exemple #2
0
static int
gftpui_common_cmd_ls (void *uidata, gftp_request * request,
                      void *other_uidata, gftp_request * other_request,
                      const char *command)
{
  char *startcolor, *endcolor, *tempstr;
  gftpui_callback_data * cdata;
  GList * templist;
  gftp_file * fle;

  if (!GFTP_IS_CONNECTED (request))
    {
      request->logging_function (gftp_logging_error, request,
                     _("Error: Not connected to a remote site\n"));
      return (1);
    }

  cdata = g_malloc0 (sizeof (*cdata));
  cdata->request = request;
  cdata->uidata = uidata;
  cdata->source_string = *command != '\0' ? (char *) command : NULL;
  cdata->run_function = gftpui_common_run_ls;
  cdata->dont_refresh = 1;

  gftpui_common_run_callback_function (cdata);

  templist = cdata->files;
  while (templist != NULL)
    {
      fle = templist->data;

      gftpui_lookup_file_colors (fle, &startcolor, &endcolor);
      tempstr = gftp_gen_ls_string (request, fle, startcolor, endcolor);
      request->logging_function (gftp_logging_misc_nolog, request, "%s\n",
                                 tempstr);
      g_free (tempstr);

      templist = templist->next;
      gftp_file_destroy (fle, 1);
    }


  if (cdata->files != NULL)
    g_list_free (cdata->files);
  g_free (cdata);

  return (1);
}
Exemple #3
0
static void
_gftpui_common_cmd_transfer_files (void *fromuidata, gftp_request * fromrequest,
                                   void *touidata, gftp_request * torequest,
                                   const char *cmd, const char *filespec)
{
  gftp_transfer * tdata;
  gftp_file * fle;

  if (!GFTP_IS_CONNECTED (fromrequest) ||
      !GFTP_IS_CONNECTED (torequest))
    {
      fromrequest->logging_function (gftp_logging_error, fromrequest,
                                  _("Error: Not connected to a remote site\n"));
      return;
    }

  if (*filespec == '\0')
    {
      fromrequest->logging_function (gftp_logging_error, fromrequest, 
                                     _("usage: %s <filespec>\n"), cmd);
      return;
    }

  tdata = gftp_tdata_new ();
  tdata->fromreq = fromrequest;
  tdata->toreq = torequest;

  if (gftp_list_files (tdata->fromreq) != 0)
    {
      tdata->fromreq = tdata->toreq = NULL;
      free_tdata (tdata);
      return;
    }

  fle = g_malloc0 (sizeof (*fle));
  while (gftp_get_next_file (tdata->fromreq, filespec, fle) > 0)
    {
      if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0)
        {
          gftp_file_destroy (fle, 0);
          continue;
        }

      tdata->files = g_list_append (tdata->files, fle);
      fle = g_malloc0 (sizeof (*fle));
    }

  g_free (fle);

  gftp_end_transfer (tdata->fromreq);

  if (tdata->files == NULL)
    {
      tdata->fromreq = tdata->toreq = NULL;
      free_tdata (tdata);
      return;
    }

  if (gftp_get_all_subdirs (tdata, NULL) != 0)
    {
      tdata->fromreq = tdata->toreq = NULL;
      free_tdata (tdata);
      return;
    }

  if (tdata->files == NULL)
    {
      tdata->fromreq = tdata->toreq = NULL;
      free_tdata (tdata);
      return;
    }

  gftpui_common_add_file_transfer (tdata->fromreq, tdata->toreq,
                                   fromuidata, touidata, tdata->files);

  g_free (tdata);

  return;
}
Exemple #4
0
int
gftpui_common_run_ls (gftpui_callback_data * cdata)
{
    int got, matched_filespec, have_dotdot, ret;
    char *sortcol_var, *sortasds_var;
    intptr_t sortcol, sortasds;
    gftp_file * fle;

    ret = gftp_list_files (cdata->request);
    if (ret < 0)
        return (ret);

    have_dotdot = 0;
    cdata->request->gotbytes = 0;
    cdata->files = NULL;
    fle = g_malloc0 (sizeof (*fle));
    while ((got = gftp_get_next_file (cdata->request, NULL, fle)) > 0 ||
            got == GFTP_ERETRYABLE)
    {
        if (cdata->source_string == NULL)
            matched_filespec = 1;
        else
            matched_filespec = gftp_match_filespec (cdata->request, fle->file,
                                                    cdata->source_string);

        if (got < 0 || strcmp (fle->file, ".") == 0 || !matched_filespec)
        {
            gftp_file_destroy (fle, 0);
            continue;
        }
        else if (strcmp (fle->file, "..") == 0)
            have_dotdot = 1;

        cdata->request->gotbytes += got;
        cdata->files = g_list_prepend (cdata->files, fle);
        fle = g_malloc0 (sizeof (*fle));
    }
    g_free (fle);

    gftp_end_transfer (cdata->request);
    cdata->request->gotbytes = -1;

    if (!have_dotdot)
    {
        fle = g_malloc0 (sizeof (*fle));
        fle->file = g_strdup ("..");
        fle->user = g_malloc0 (1);
        fle->group = g_malloc0 (1);
        fle->st_mode = S_IFDIR | S_IRUSR | S_IWUSR | S_IXUSR;
        cdata->files = g_list_prepend (cdata->files, fle);
    }

    if (cdata->files != NULL)
    {
        if (cdata->request->protonum == GFTP_LOCAL_NUM)
        {
            sortasds_var = "local_sortasds";
            sortcol_var = "local_sortcol";
        }
        else
        {
            sortasds_var = "remote_sortasds";
            sortcol_var = "remote_sortcol";
        }

        gftp_lookup_global_option (sortcol_var, &sortcol);
        gftp_lookup_global_option (sortasds_var, &sortasds);

        cdata->files = gftp_sort_filelist (cdata->files, sortcol, sortasds);
    }

    return (1);
}
Exemple #5
0
static void
do_view_or_edit_file (gftp_window_data * fromwdata, int is_view)
{
  GList * templist, * filelist, * newfile;
  gftp_window_data * towdata;
  gftp_file * new_fle;
  char *suffix;
  int num;

  if (!check_status (is_view ? _("View") : _("Edit"), fromwdata, 0, 1, 1, 1))
    return;

  towdata = fromwdata == &window1 ? &window2 : &window1;

  templist = GTK_CLIST (fromwdata->listbox)->selection;
  num = 0;
  filelist = fromwdata->files;
  templist = get_next_selection (templist, &filelist, &num);
  curfle = filelist->data;

  if (S_ISDIR (curfle->st_mode))
    {
      if (is_view)
        ftp_log (gftp_logging_error, NULL,
                 _("View: %s is a directory. Cannot view it.\n"), curfle->file);
      else
        ftp_log (gftp_logging_error, NULL,
                 _("Edit: %s is a directory. Cannot edit it.\n"), curfle->file);

      return;
    }

  if (strcmp (gftp_protocols[fromwdata->request->protonum].name, "Local") == 0)
    view_file (curfle->file, 0, is_view, 0, 1, 1, NULL, fromwdata);
  else
    {
      new_fle = copy_fdata (curfle);
      if (new_fle->destfile)
        g_free (new_fle->destfile);

      if ((suffix = strrchr (curfle->file, '.')) != NULL)
        {
          new_fle->destfile = g_strconcat (g_get_tmp_dir (),
                                           "/gftp-view.XXXXXX", suffix, NULL);
          new_fle->fd = mkstemps (new_fle->destfile, strlen (suffix));
	}
      else
        {
	  new_fle->destfile = g_strconcat (g_get_tmp_dir (),
                                           "/gftp-view.XXXXXX", NULL);		
          new_fle->fd = mkstemps (new_fle->destfile, 0);
	}
		
      if (new_fle->fd < 0)
        {
          ftp_log (gftp_logging_error, NULL, 
                   _("Error: Cannot open %s for writing: %s\n"),  
                   new_fle->destfile, g_strerror (errno));
          gftp_file_destroy (new_fle, 1);
          return;
        }

      fchmod (new_fle->fd, S_IRUSR | S_IWUSR);

      if (is_view)
        {
          new_fle->done_view = 1;
          new_fle->done_rm = 1;
        }
      else
        new_fle->done_edit = 1;

      newfile = g_list_append (NULL, new_fle); 
      gftpui_common_add_file_transfer (fromwdata->request, towdata->request,
                                       fromwdata, towdata, newfile);
    }
}
Exemple #6
0
static GList *
gftp_get_dir_listing (gftp_transfer * transfer, int getothdir, int *ret)
{
  GHashTable * dirhash;
  GList * templist;
  gftp_file * fle;
  off_t *newsize;
  char *newname;

  if (getothdir && transfer->toreq != NULL)
    {
      dirhash = gftp_gen_dir_hash (transfer->toreq, ret);
      if (*ret == GFTP_EFATAL)
        return (NULL);
    }
  else 
    dirhash = NULL; 

  *ret = gftp_list_files (transfer->fromreq);
  if (*ret < 0)
    {
      gftp_destroy_dir_hash (dirhash);
      return (NULL);
    }

  fle = g_malloc0 (sizeof (*fle));
  templist = NULL;
  while (gftp_get_next_file (transfer->fromreq, NULL, fle) > 0)
    {
      if (strcmp (fle->file, ".") == 0 || strcmp (fle->file, "..") == 0)
        {
          gftp_file_destroy (fle, 0);
          continue;
        }

      if (dirhash && 
          (newsize = g_hash_table_lookup (dirhash, fle->file)) != NULL)
        {
          fle->exists_other_side = 1;
          fle->startsize = *newsize;
        }
      else
        fle->exists_other_side = 0;

      if (transfer->toreq && fle->destfile == NULL)
        fle->destfile = gftp_build_path (transfer->toreq,
                                         transfer->toreq->directory, 
                                         fle->file, NULL);

      if (transfer->fromreq->directory != NULL &&
          *transfer->fromreq->directory != '\0' &&
          *fle->file != '/')
        {
          newname = gftp_build_path (transfer->fromreq,
                                     transfer->fromreq->directory,
                                     fle->file, NULL);

          g_free (fle->file);
          fle->file = newname;
        }

      templist = g_list_append (templist, fle);

      fle = g_malloc0 (sizeof (*fle));
    }
  gftp_end_transfer (transfer->fromreq);

  gftp_file_destroy (fle, 1);
  gftp_destroy_dir_hash (dirhash);

  return (templist);
}
Exemple #7
0
int
gftp_get_next_file (gftp_request * request, const char *filespec,
                    gftp_file * fle)
{
  char *slashpos, *tmpfile, *utf8;
  size_t destlen;
  int fd, ret;

  g_return_val_if_fail (request != NULL, GFTP_EFATAL);

  if (request->get_next_file == NULL)
    return (GFTP_EFATAL);

  if (request->cached && request->cachefd > 0)
    fd = request->cachefd;
  else
    fd = request->datafd;

  memset (fle, 0, sizeof (*fle));
  do
    {
      gftp_file_destroy (fle, 0);
      ret = request->get_next_file (request, fle, fd);
      if (fle->file != NULL && (slashpos = strrchr (fle->file, '/')) != NULL)
        {
          if (*(slashpos + 1) == '\0')
            {
              gftp_file_destroy (fle, 0);
              continue;
            }

          *slashpos = '\0';
          tmpfile = g_strdup (slashpos + 1);

          if (strcmp (fle->file, request->directory) != 0)
            request->logging_function (gftp_logging_error, request,
                                       _("Warning: Stripping path off of file '%s'. The stripped path (%s) doesn't match the current directory (%s)\n"),
                                       tmpfile, fle->file, request->directory,
                                       g_strerror (errno));
          
          g_free (fle->file);
          fle->file = tmpfile;
        }

      if (ret >= 0 && fle->file != NULL)
        {
          if (g_utf8_validate (fle->file, -1, NULL))
            fle->filename_utf8_encoded = 1;
          else
            {
              utf8 = gftp_filename_to_utf8 (request, fle->file, &destlen);
              if (utf8 != NULL)
                {
                  g_free (fle->file);
                  fle->file = utf8;
                  fle->filename_utf8_encoded = 1;
                }
            }
        }

      if (ret >= 0 && !request->cached && request->cachefd > 0 && 
          request->last_dir_entry != NULL)
        {
          if (gftp_fd_write (request, request->last_dir_entry,
                          request->last_dir_entry_len, request->cachefd) < 0)
            {
              request->logging_function (gftp_logging_error, request,
                                        _("Error: Cannot write to cache: %s\n"),
                                        g_strerror (errno));
              close (request->cachefd);
              request->cachefd = -1;
            }
        }
    } while (ret > 0 && !gftp_match_filespec (request, fle->file, filespec));

  return (ret);
}