Exemple #1
0
//ftp.chdir([dir])
//===================================
static int lftp_chdir( lua_State* L )
{
  if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) {
    ftp_log("[FTP usr] Login first\r\n" );
    lua_pushinteger(L, -1);
    return 1;
  }
  _getFName(L, 1);

  free(ftpresponse);
  ftpresponse = NULL;
  cmd_done = 0;
  uint32_t tmo = mico_get_time();
  ftpCmdSocket->clientFlag = REQ_ACTION_CHDIR;

  while (cmd_done == 0) {
    if ((mico_get_time() - tmo) > 4000) break;
    mico_thread_msleep(60);
    luaWdgReload();
  }
  if (cmd_done == 0) {
    ftp_log("[FTP usr] Timeout\r\n" );
    lua_pushinteger(L, -2);
    return 1;
  }

  lua_pushinteger(L, 0);
  if (ftpresponse != NULL) {
    lua_pushstring(L, ftpresponse);
    free(ftpresponse);
    ftpresponse = NULL;
  }
  else lua_pushstring(L, "?");
  return 2;
}
Exemple #2
0
static int
_check_viewedit_process_status (gftp_viewedit_data * ve_proc, int ret)
{
  int procret;

  if (WIFEXITED (ret))
    {
      procret = WEXITSTATUS (ret);
      if (procret != 0)
        {
          ftp_log (gftp_logging_error, NULL,
                   _("Error: Child %d returned %d\n"), ve_proc->pid, procret);
          if (ve_proc->view)
            remove_file (ve_proc);

          return (0);
        }
      else
        {
          ftp_log (gftp_logging_misc, NULL,
                   _("Child %d returned successfully\n"), ve_proc->pid);
          return (1);
        }
    }
  else
    {
      ftp_log (gftp_logging_error, NULL,
               _("Error: Child %d did not terminate properly\n"),
               ve_proc->pid);
      return (0);
    }
}
Exemple #3
0
static int
_prompt_to_upload_edited_file (gftp_viewedit_data * ve_proc)
{
  struct stat st;
  char *str;

  if (stat (ve_proc->filename, &st) == -1)
    {
      ftp_log (gftp_logging_error, NULL,
               _("Error: Cannot get information about file %s: %s\n"),
	       ve_proc->filename, g_strerror (errno));
      return (0);
    }
  else if (st.st_mtime == ve_proc->st.st_mtime)
    {
      ftp_log (gftp_logging_misc, NULL, _("File %s was not changed\n"),
	       ve_proc->filename);
      remove_file (ve_proc);
      return (0);
    }
  else
    {
      memcpy (&ve_proc->st, &st, sizeof (ve_proc->st));
      str = g_strdup_printf (_("File %s has changed.\nWould you like to upload it?"),
                             ve_proc->remote_filename);

      MakeYesNoDialog (_("Edit File"), str, do_upload, ve_proc, dont_upload,
                       ve_proc);
      g_free (str);
      return (1);
    }
}
Exemple #4
0
void 
viewlog (gpointer data)
{
  char *tempstr, *txt, *pos;
  gint textlen;
  ssize_t len;
  int fd;
#if GTK_MAJOR_VERSION > 1
  GtkTextBuffer * textbuf;
  GtkTextIter iter, iter2;
#endif

  tempstr = g_strconcat (g_get_tmp_dir (), "/gftp-view.XXXXXXXXXX", NULL);
  if ((fd = mkstemp (tempstr)) < 0)
    {
      ftp_log (gftp_logging_error, NULL, 
               _("Error: Cannot open %s for writing: %s\n"), tempstr, 
               g_strerror (errno));
      g_free (tempstr); 
      return;
    }
  chmod (tempstr, S_IRUSR | S_IWUSR);
  
#if GTK_MAJOR_VERSION == 1
  textlen = gtk_text_get_length (GTK_TEXT (logwdw));
  txt = gtk_editable_get_chars (GTK_EDITABLE (logwdw), 0, -1);
#else
  textbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (logwdw));
  textlen = gtk_text_buffer_get_char_count (textbuf);
  gtk_text_buffer_get_iter_at_offset (textbuf, &iter, 0);
  gtk_text_buffer_get_iter_at_offset (textbuf, &iter2, textlen);
  txt = gtk_text_buffer_get_text (textbuf, &iter, &iter2, 0);

  /* gtk_text_buffer_get_char_count() returns the number of characters,
     not bytes. So get the number of bytes that need to be written out */
  textlen = strlen (txt);
#endif
  pos = txt;

  while (textlen > 0)
    {
      if ((len = write (fd, pos, textlen)) == -1)
        { 
          ftp_log (gftp_logging_error, NULL, 
                   _("Error: Error writing to %s: %s\n"), 
                   tempstr, g_strerror (errno));
          break;
        }
      textlen -= len;
      pos += len;
    }

  fsync (fd);
  lseek (fd, 0, SEEK_SET);
  view_file (tempstr, fd, 1, 1, 0, 1, NULL, NULL);
  close (fd);

  g_free (tempstr);
  g_free (txt);
}
Exemple #5
0
//--------------------------------
static int _openDataSocket( void )
{
  closeDataSocket();
  
  ftpDataSocket = (ftpDataSocket_t*)malloc(sizeof(ftpDataSocket_t));
  if (ftpDataSocket == NULL) {
    ftp_log("[FTP dta] Memory allocation failed\r\n" );
    return -1;
  }

  int skt = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  if (skt < 0) {
    ftp_log("[FTP dta] Open data socket failed\r\n" );
    free(ftpDataSocket);
    ftpDataSocket = NULL;
    return -2;
  }
  ftpDataSocket->socket = skt;
  ftpDataSocket->addr.s_port = 0;
  ftpDataSocket->addr.s_ip = 0;

  if (recvDataBuf == NULL) {
    // create recv data buffer
    recvDataBuf = malloc(max_recv_datalen+4);
    if (recvDataBuf == NULL) {
      free(ftpDataSocket);
      ftpDataSocket = NULL;
      return -3;
    }
    memset(recvDataBuf, 0, max_recv_datalen);
  }
  recvDataLen = 0;
  status &= ~FTP_DATACONNECTED;
  return 0;
}
Exemple #6
0
//ftp.send(file [,append])
//==================================
static int lftp_send( lua_State* L )
{
  if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) {
    ftp_log("[FTP usr] Login first\r\n" );
    lua_pushinteger(L, -11);
    return 1;
  }
  if (lua_gettop(L) >= 2) {
    send_type = (uint8_t)luaL_checkinteger(L, 2);
    if (send_type != SEND_APPEND) send_type = SEND_OVERWRITTE;
  }
  else send_type = SEND_OVERWRITTE;
  
  if (_getFName(L, 1) < 0) {
    ftp_log("[FTP fil] File name missing\r\n" );
    lua_pushinteger(L, -12);
    return 1;
  }
  if (_openFile("r") < 0) {
    lua_pushinteger(L, -13);
    return 1;
  }

  spiffs_stat s;
  // Get file size
  SPIFFS_fstat(&fs, file_fd, &s);
  file_size = s.size;
  
  data_done = 0;
  ftpCmdSocket->clientFlag = REQ_ACTION_SEND;
  
  if (ftpCmdSocket->sent_cb == LUA_NOREF) {
    // no cb function, wait until file received (max 10 sec)
    uint32_t tmo = mico_get_time();
    while (!data_done) {
      if ((mico_get_time() - tmo) > 10000) break;
      mico_thread_msleep(60);
      luaWdgReload();
    }
    if (!data_done) {
      ftp_log("[FTP usr] Timeout: file not sent\r\n" );
      lua_pushinteger(L, -14);
    }
    else {
      ftp_log("[FTP usr] File sent\r\n" );
      lua_pushinteger(L, file_status);
    }
  }
  else lua_pushinteger(L, 0);
  return 1;
}
Exemple #7
0
//stat = ftp.start()
//===================================
static int lftp_start( lua_State* L )
{
  LinkStatusTypeDef wifi_link;
  int err = micoWlanGetLinkStatus( &wifi_link );

  if ( wifi_link.is_connected == false ) {
    ftp_log("[FTP usr] WiFi NOT CONNECTED!\r\n" );
    lua_pushinteger(L, -1);
    return 1;
  }
  
  if ( (gL == NULL) || (ftpCmdSocket == NULL) ) {
    ftp_log("[FTP usr] Execute ftp.new first!\r\n" );
    lua_pushinteger(L, -2);
    return 1;
  }
  if (ftp_thread_is_started) {
    ftp_log("[FTP usr] Already started!\r\n" );
    lua_pushinteger(L, -3);
    return 1;
  }
  
  mico_system_notify_register( mico_notify_TCP_CLIENT_CONNECTED, (void *)_micoNotify_FTPClientConnectedHandler, NULL );

  // all setup, start the ftp thread
  if (!ftp_thread_is_started) {
    if (mico_rtos_create_thread(NULL, MICO_APPLICATION_PRIORITY-1, "Ftp_Thread", _thread_ftp, 1024, NULL) != kNoErr) {
      _ftp_deinit(0);    
      ftp_log("[FTP usr] Create thread failed\r\n" );
      lua_pushinteger(L, -4);
      return 1;
    }
    else ftp_thread_is_started = true;
  } 

  if (ftpCmdSocket->logon_cb != LUA_NOREF) {
    lua_pushinteger(L, 0);
    return 1;
  }
  
  // wait max 10 sec for login
  uint32_t tmo = mico_get_time();
  while ( (ftp_thread_is_started) && !(status & FTP_LOGGED) ) {
    if ((mico_get_time() - tmo) > 10000) break;
    mico_thread_msleep(100);
    luaWdgReload();
  }
  if (!(status & FTP_LOGGED)) lua_pushinteger(L, -4);
  else lua_pushinteger(L, 0);
  return 1;
}
Exemple #8
0
static void
remove_file (gftp_viewedit_data * ve_proc)
{
  if (ve_proc->remote_filename == NULL)
    return;

  if (unlink (ve_proc->filename) == 0)
    ftp_log (gftp_logging_misc, NULL, _("Successfully removed %s\n"),
             ve_proc->filename);
  else
    ftp_log (gftp_logging_error, NULL,
             _("Error: Could not remove file %s: %s\n"), ve_proc->filename,
             g_strerror (errno));
}
Exemple #9
0
//ftp.recv(file [,tostr])
//===================================
static int lftp_recv( lua_State* L )
{
  if ( (gL == NULL) || (ftpCmdSocket == NULL) || (!(status & FTP_LOGGED)) ) {
    ftp_log("[FTP usr] Login first\r\n" );
    lua_pushinteger(L, -11);
    return 1;
  }

  if (_getFName(L, 1) < 0) {
    ftp_log("[FTP fil] File name missing\r\n" );
    lua_pushinteger(L, -12);
    return 1;
  }
  if (_openFile("w") < 0) {
    lua_pushinteger(L, -13);
    return 1;
  }
  
  recv_type = RECV_TOFILE;
  if (lua_gettop(L) >= 2) {
    int tos = luaL_checkinteger(L, 2);
    if (tos == 1) recv_type = RECV_TOSTRING;
  }
  data_done = 0;
  ftpCmdSocket->clientFlag = REQ_ACTION_RECV;
  
  if (ftpCmdSocket->received_cb == LUA_NOREF) {
    // no cb function, wait until file received (max 10 sec)
    uint32_t tmo = mico_get_time();
    while (!data_done) {
      if ((mico_get_time() - tmo) > 10000) break;
      mico_thread_msleep(60);
      luaWdgReload();
    }
    if (!data_done) {
      ftp_log("[FTP usr] Timeout: file not received\r\n" );
      lua_pushinteger(L, -14);
    }
    else {
      ftp_log("[FTP usr] File received\r\n" );
      lua_pushinteger(L, file_status);
      if (recv_type == RECV_TOSTRING) {
        lua_pushstring(L, recvDataBuf);
        return 2;
      }
    }
  }
  else lua_pushinteger(L, 0);
  return 1;
}
Exemple #10
0
static void
dosave_directory_listing (GtkWidget * widget, gftp_save_dir_struct * str)
{
  const char *filename;
  gftp_file * tempfle;
  GList * templist;
  char *tempstr;
  FILE * fd;
 

  filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (str->filew));
  if ((fd = fopen (filename, "w")) == NULL)
    {
      ftp_log (gftp_logging_error, NULL, 
               _("Error: Cannot open %s for writing: %s\n"), filename, 
               g_strerror (errno));
      return;
    }

  for (templist = str->wdata->files; 
       templist != NULL;
       templist = templist->next)
    {
      tempfle = templist->data;

      if (!tempfle->shown)
        continue;

      tempstr = gftp_gen_ls_string (NULL, tempfle, NULL, NULL);
      fprintf (fd, "%s\n", tempstr);
      g_free (tempstr);
    }

  fclose (fd);
}
Exemple #11
0
//------------------------------------
static void _ftp_deinit(uint8_t unreg)
{
  closeCmdSocket(unreg);
  free(ftpCmdSocket);
  ftpCmdSocket = NULL;
  closeDataSocket();
  free(ftpDataSocket);
  ftpDataSocket = NULL;
  
  free( pDomain4Dns);
  pDomain4Dns=NULL;
  free( ftpuser);
  ftpuser=NULL;
  free( ftppass);
  ftppass=NULL;
  free( ftpfile);
  ftpfile=NULL;
  free( recvBuf);
  recvBuf=NULL;
  free( recvDataBuf);
  recvDataBuf=NULL;
  free(ftpresponse);
  ftpresponse = NULL;
  if ((gL != NULL) & (unreg == 0)) ftp_log("[FTP end] FTP SESSION CLOSED.\r\n");
  status = FTP_NOT_CONNECTED;
}
Exemple #12
0
void
remove_file_transfer (gpointer data)
{
  gftpui_common_curtrans_data * transdata;
  GtkCTreeNode * node;
  gftp_file * curfle;

  if (GTK_CLIST (dlwdw)->selection == NULL)
    {
      ftp_log (gftp_logging_error, NULL,
              _("There are no file transfers selected\n"));
      return;
    }

  node = GTK_CLIST (dlwdw)->selection->data;
  transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node);

  if (transdata->curfle == NULL || transdata->curfle->data == NULL)
    return;

  curfle = transdata->curfle->data;
  gftpui_common_skip_file_transfer (transdata->transfer, curfle);

  gtk_ctree_node_set_text (GTK_CTREE (dlwdw), curfle->user_data, 1,
                           _("Skipped"));
}
Exemple #13
0
void
move_transfer_down (gpointer data)
{
  GList * firstentry, * secentry, * lastentry;
  gftpui_common_curtrans_data * transdata;
  GtkCTreeNode * node;

  if (GTK_CLIST (dlwdw)->selection == NULL)
    {
      ftp_log (gftp_logging_error, NULL,
	      _("There are no file transfers selected\n"));
      return;
    }
  node = GTK_CLIST (dlwdw)->selection->data;
  transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node);

  if (transdata->curfle == NULL)
    return;

  g_static_mutex_lock (&transdata->transfer->structmutex);
  if (transdata->curfle->next != NULL && (!transdata->transfer->started ||
      (transdata->transfer->curfle != transdata->curfle && 
       transdata->transfer->curfle != transdata->curfle->next)))
    {
      if (transdata->curfle->prev == NULL)
        {
          firstentry = transdata->curfle->next;
          lastentry = transdata->curfle->next->next;
          transdata->transfer->files = firstentry;
          transdata->transfer->files->prev = NULL;
          transdata->transfer->files->next = transdata->curfle;
          transdata->curfle->prev = transdata->transfer->files;
          transdata->curfle->next = lastentry;
          if (lastentry != NULL)
            lastentry->prev = transdata->curfle;
        }
      else
        {
          firstentry = transdata->curfle->prev;
          secentry = transdata->curfle->next;
          lastentry = transdata->curfle->next->next;
          firstentry->next = secentry;
          secentry->prev = firstentry;
          secentry->next = transdata->curfle;
          transdata->curfle->prev = secentry;
          transdata->curfle->next = lastentry;
          if (lastentry != NULL)
            lastentry->prev = transdata->curfle;
        }

      gtk_ctree_move (GTK_CTREE (dlwdw), 
                      ((gftp_file *) transdata->curfle->data)->user_data,
                      transdata->transfer->user_data, 
                      transdata->curfle->next != NULL ?
                          ((gftp_file *) transdata->curfle->next->data)->user_data: NULL);
    }
  g_static_mutex_unlock (&transdata->transfer->structmutex);
}
Exemple #14
0
//-------------------------------------------------------
static void _micoNotify_FTPClientConnectedHandler(int fd)
{
  if (ftpCmdSocket != NULL) {
    if (ftpCmdSocket->socket == fd) {
      // ** command socket connected
      status |= FTP_CONNECTED;
      ftp_log("[FTP cmd] Socket connected\r\n");
      return;
    }
  }
  if (ftpDataSocket != NULL) {
    if (ftpDataSocket->socket == fd) {
      // ** data socket connected
      status |= FTP_DATACONNECTED;
      ftp_log("[FTP dta] Socket connected\r\n");
      return;
    }
  }
}
Exemple #15
0
//-----------------------------------------------
static int _getFName(lua_State* L, uint8_t index)
{
  free(ftpfile);
  ftpfile = NULL;
  if (lua_gettop(L) < index) return -3;
  
  size_t len = 0;
  const char *fname = luaL_checklstring( L, index, &len );
  if (len > SPIFFS_OBJ_NAME_LEN || fname == NULL) {
    ftp_log("[FTP fil] Bad name or length > %d\r\n", SPIFFS_OBJ_NAME_LEN);
    return -1;
  }
  ftpfile=(char*)malloc(len+1);
  if (ftpfile==NULL) {
    ftp_log("[FTP fil] Memory allocation failed\r\n" );
    return -2;
  }
  strcpy(ftpfile,fname);
  return 0;
}
Exemple #16
0
//------------------------------
static int _openFile(char *mode)
{
  // open the file
  if (ftpfile == NULL) {
    ftp_log("[FTP fil] Ftpfile not assigned\r\n" );
    return -1;
  }
  ftp_log("[FTP fil] Opening local file: %s\r\n", ftpfile );
  if (FILE_NOT_OPENED != file_fd) {
    ftp_log("[FTP fil] Closing file first\r\n" );
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
  }
  file_fd = SPIFFS_open(&fs, (char*)ftpfile, mode2flag(mode), 0);
  if (file_fd <= FILE_NOT_OPENED) {
    ftp_log("[FTP fil] Error opening local file: %d\r\n", file_fd );
    file_fd = FILE_NOT_OPENED;
    return -2;
  }
  return 0;
}
Exemple #17
0
//---------------------
uint8_t _sendFile(void)
{
  if ((ftpfile == NULL) || (file_fd == FILE_NOT_OPENED)) {
    ftp_log("[FTP dta] Send file not opened\r\n");
    file_status = -1;
    return 1;
  }
  if (recvDataBuf == NULL) {
    ftp_log("[FTP dta] Buffer error\r\n");
    file_status = -2;
    return 1;
  }
  if (file_status >= file_size) return 1;

  uint16_t len = 1020;
  int rdlen;
  if (max_recv_datalen < len) len = max_recv_datalen-4;
  rdlen = SPIFFS_read(&fs, (spiffs_file)file_fd, (char*)recvDataBuf, len);
  if (rdlen > 0) {
    if (!_sendData(recvDataBuf, rdlen)) {
      ftp_log("[FTP dta] Error sending data\r\n");
      file_status = -3;
      return 1;
    }
    else {
      file_status += rdlen;
      ftp_log("\r[FTP dta] %.*f %%", 1, (float)(((float)file_status/(float)file_size)*100.0));
      if (file_status >= file_size) return 1;
      else return 0;
    }
  }
  else if (rdlen <= 0) {
    ftp_log("[FTP dta] Error reading from file (%d)\r\n", rdlen);
    file_status = -4;
    return 1;
  }
  else return 1;
}
Exemple #18
0
//ftp.sendstring(file, str [,append])
//========================================
static int lftp_sendstring( lua_State* L )
{
  if ((gL != NULL) && (ftpCmdSocket != NULL)) {
    if ((status & FTP_LOGGED)) {
      if (lua_gettop(L) >= 3) {
        send_type = (uint8_t)luaL_checkinteger(L, 3);
        if (send_type != SEND_APPEND) send_type = SEND_OVERWRITTE;
      }
      else send_type = SEND_OVERWRITTE;
      send_type |= SEND_STRING;

      if (_getFName(L, 1) < 0) {
        ftp_log("[FTP fil] File name missing\r\n" );
        lua_pushinteger(L, -13);
        return 1;
      }
      
      size_t len;
      sendDataBuf = (char*)luaL_checklstring( L, 2, &len );
      if (sendDataBuf == NULL) {
        ftp_log("[FTP fil] Bad string\r\n");
        lua_pushinteger(L, -14);
      }
      else {
        file_size = len;
        data_done = 0;
        ftpCmdSocket->clientFlag = REQ_ACTION_SEND;
      
        uint32_t tmo = mico_get_time();
        while (!data_done) {
          if ((mico_get_time() - tmo) > 10000) break;
          mico_thread_msleep(60);
          luaWdgReload();
        }
        if (!data_done) {
          ftp_log("[FTP usr] Timeout: string not sent\r\n" );
          lua_pushinteger(L, -15);
        }
        else {
          ftp_log("[FTP usr] String sent\r\n" );
          lua_pushinteger(L, file_status);
        }
        sendDataBuf = NULL;
      }
    }
    else {
      ftp_log("[FTP usr] Not logged\r\n" );
      lua_pushinteger(L, -12);
    }
  }
  else {
    ftp_log("[FTP usr] Login first\r\n" );
    lua_pushinteger(L, -11);
  }
  return 1;
}
Exemple #19
0
//-----------------------
uint8_t _sendString(void)
{
  if (sendDataBuf == NULL) {
    ftp_log("[FTP dta] Buffer error\r\n");
    file_status = -2;
    return 1;
  }
  if (file_status >= file_size) return 1;

  int len = 1020;
  if ((file_size - file_status) < len) len = file_size - file_status;
  
  if (!_sendData(sendDataBuf+file_status, len)) {
    ftp_log("[FTP dta] Error sending data\r\n");
    file_status = -3;
    return 1;
  }
  else {
    file_status += len;
    ftp_log("\r[FTP dta] %.*f %%", 1, (float)(((float)file_status/(float)file_size)*100.0));
    if (file_status >= file_size) return 1;
    else return 0;
  }
}
Exemple #20
0
//-------------------------------
static void closeDataSocket(void)
{
  if (ftpDataSocket == NULL) return;

  //close client socket
  close(ftpDataSocket->socket);
  
  //free memory
  free(ftpDataSocket);
  ftpDataSocket = NULL;
  
  status &= ~(FTP_DATACONNECTED | FTP_RECEIVING | FTP_LISTING | FTP_SENDING);
  
  ftp_log("[FTP dta] Socket closed\r\n");
}
Exemple #21
0
//------------------------------------
static void _setResponse(uint16_t cmd)
{
  for (uint16_t i = (recvLen-1); i>0; i--) {
    if ((*(recvBuf+i) == '\n') || (*(recvBuf+i) == '\r')) {
      *(recvBuf+i) = '\0';
    }
    else break;
  }
  ftp_log("[FTP cmd] [%d][%s]\r\n", cmd, recvBuf);
  free(ftpresponse);
  ftpresponse = NULL;
  ftpresponse = (char*)malloc(strlen(recvBuf)+1);
  if (ftpresponse != NULL) {
    strcpy(ftpresponse, recvBuf);
  }
  cmd_done = 1;
}
Exemple #22
0
void
stop_transfer (gpointer data)
{
  gftpui_common_curtrans_data * transdata;
  GtkCTreeNode * node;

  if (GTK_CLIST (dlwdw)->selection == NULL)
    {
      ftp_log (gftp_logging_error, NULL,
	      _("There are no file transfers selected\n"));
      return;
    }

  node = GTK_CLIST (dlwdw)->selection->data;
  transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node);
  gftpui_common_cancel_file_transfer (transdata->transfer);
}
Exemple #23
0
void
edit_dialog (gpointer data)
{
  gftp_window_data * fromwdata = data;
  char *edit_program;

  gftp_lookup_request_option (fromwdata->request, "edit_program", 
                              &edit_program);

  if (*edit_program == '\0')
    {
      ftp_log (gftp_logging_error, NULL,
	       _("Edit: You must specify an editor in the options dialog\n"));
      return;
    }

  do_view_or_edit_file (data, 0);
}
Exemple #24
0
//---------------------------------------
static void closeCmdSocket(uint8_t unreg)
{
  if (ftpCmdSocket == NULL) return;

  //unref cb functions
  if (gL != NULL) {
    if (unreg) {
      if (ftpCmdSocket->disconnect_cb != LUA_NOREF)
        luaL_unref(gL, LUA_REGISTRYINDEX, ftpCmdSocket->disconnect_cb);
    }
    ftpCmdSocket->disconnect_cb = LUA_NOREF;
    
    if (ftpCmdSocket->logon_cb != LUA_NOREF)
      luaL_unref(gL, LUA_REGISTRYINDEX, ftpCmdSocket->logon_cb);
    ftpCmdSocket->logon_cb = LUA_NOREF;
    
    if (ftpCmdSocket->list_cb != LUA_NOREF)
      luaL_unref(gL, LUA_REGISTRYINDEX, ftpCmdSocket->list_cb);
    ftpCmdSocket->list_cb = LUA_NOREF;
    
    if (ftpCmdSocket->received_cb != LUA_NOREF)
      luaL_unref(gL, LUA_REGISTRYINDEX, ftpCmdSocket->received_cb);
    ftpCmdSocket->received_cb = LUA_NOREF;
    
    if (ftpCmdSocket->sent_cb != LUA_NOREF)
      luaL_unref(gL, LUA_REGISTRYINDEX, ftpCmdSocket->sent_cb);
    ftpCmdSocket->sent_cb = LUA_NOREF;
  }
  
  ftpCmdSocket->clientFlag = NO_ACTION;

  //close client socket
  close(ftpCmdSocket->socket);
  
  //free memory
  free(ftpCmdSocket);
  ftpCmdSocket = NULL;
  
  status = FTP_NOT_CONNECTED;
  
  ftp_log("[FTP cmd] Socket closed\r\n");
}
Exemple #25
0
void
start_transfer (gpointer data)
{
  gftpui_common_curtrans_data * transdata;
  GtkCTreeNode * node;

  if (GTK_CLIST (dlwdw)->selection == NULL)
    {
      ftp_log (gftp_logging_error, NULL,
	       _("There are no file transfers selected\n"));
      return;
    }
  node = GTK_CLIST (dlwdw)->selection->data;
  transdata = gtk_ctree_node_get_row_data (GTK_CTREE (dlwdw), node);

  g_static_mutex_lock (&transdata->transfer->structmutex);
  if (!transdata->transfer->started)
    create_transfer (transdata->transfer);
  g_static_mutex_unlock (&transdata->transfer->structmutex);
}
Exemple #26
0
static void
dochange_filespec (gftp_window_data * wdata, gftp_dialog_data * ddata)
{
  const char *edttext;

  wdata->show_selected = 0;

  edttext = gtk_entry_get_text (GTK_ENTRY (ddata->edit));
  if (*edttext == '\0')
    {
      ftp_log (gftp_logging_error, NULL,
               _("Change Filespec: Operation canceled...you must enter a string\n"));
      return;
    }

  if (wdata->filespec)
    g_free (wdata->filespec);

  wdata->filespec = g_strdup (edttext);
  update_window_listbox (wdata);
}
Exemple #27
0
//----------------------------------
static void _saveData(uint8_t close)
{
  if (!(status & FTP_RECEIVING)) {
    ftp_log("[FTP dta] Not receiving!\r\n");
    file_status = -4;
    return;
  }
  if ((ftpfile == NULL) || (file_fd == FILE_NOT_OPENED)) {
    ftp_log("[FTP dta] Receive file not opened\r\n");
    file_status = -2;
    return;
  }

  if (close == 0) {  
    if (SPIFFS_write(&fs, (spiffs_file)file_fd, (char*)recvDataBuf, recvDataLen) < 0)
    { //failed
      SPIFFS_fflush(&fs,file_fd);
      SPIFFS_close(&fs,file_fd);
      file_fd = FILE_NOT_OPENED;
      ftp_log("\r\n[FTP dta] Write to local file failed\r\n");
      file_status = -3;
    }
    else {
      ftp_log("\r[FTP dta] received: %d", recvDataLen);
      file_status += recvDataLen;
    }
  }
  else { // close file;
    SPIFFS_fflush(&fs,file_fd);
    SPIFFS_close(&fs,file_fd);
    file_fd = FILE_NOT_OPENED;
    free(ftpfile);
    ftpfile = NULL;
    if (close == 2) {
      ftp_log("\r\n[FTP dta] Data ERROR, file closed\r\n");
      file_status = -1;
    }
    else {
      ftp_log("\r\n[FTP dta] Data file closed\r\n");
    }
  }
}
Exemple #28
0
static void
dosavelog (GtkWidget * widget, GtkFileSelection * fs)
{
  const char *filename;
  char *txt, *pos;
  gint textlen;
  ssize_t len;
  FILE *fd;
  int ok;
#if GTK_MAJOR_VERSION > 1
  GtkTextBuffer * textbuf;
  GtkTextIter iter, iter2;
#endif

  filename = gtk_file_selection_get_filename (GTK_FILE_SELECTION (fs));
  if ((fd = fopen (filename, "w")) == NULL)
    {
      ftp_log (gftp_logging_error, NULL, 
               _("Error: Cannot open %s for writing: %s\n"), filename, 
               g_strerror (errno));
      return;
    }

#if GTK_MAJOR_VERSION == 1
  textlen = gtk_text_get_length (GTK_TEXT (logwdw));
  txt = gtk_editable_get_chars (GTK_EDITABLE (logwdw), 0, -1);
#else
  textbuf = gtk_text_view_get_buffer (GTK_TEXT_VIEW (logwdw));
  textlen = gtk_text_buffer_get_char_count (textbuf);
  gtk_text_buffer_get_iter_at_offset (textbuf, &iter, 0);
  gtk_text_buffer_get_iter_at_offset (textbuf, &iter2, textlen);
  txt = gtk_text_buffer_get_text (textbuf, &iter, &iter2, 0);

  /* gtk_text_buffer_get_char_count() returns the number of characters,
     not bytes. So get the number of bytes that need to be written out */
  textlen = strlen (txt);
#endif

  ok = 1;
  pos = txt;
  do
    {
      if ((len = write (fileno (fd), pos, textlen)) == -1)
        {
          ok = 0;
          ftp_log (gftp_logging_error, NULL, 
                   _("Error: Error writing to %s: %s\n"), 
                   filename, g_strerror (errno));
          break;
        }

      textlen -= len;
      pos += len;
    } while (textlen > 0);

  if (ok)
    ftp_log (gftp_logging_misc, NULL,
             _("Successfully wrote the log file to %s\n"), filename);

  fclose (fd);
  g_free (txt);
}
Exemple #29
0
void
transfer_window_files (gftp_window_data * fromwdata, gftp_window_data * towdata)
{
  gftp_file * tempfle, * newfle;
  GList * templist, * filelist;
  gftp_transfer * transfer;
  int num, ret, disconnect;

  if (!check_status (_("Transfer Files"), fromwdata, 1, 0, 1,
       towdata->request->put_file != NULL && fromwdata->request->get_file != NULL))
    return;

  if (!GFTP_IS_CONNECTED (fromwdata->request) || 
      !GFTP_IS_CONNECTED (towdata->request))
    {
      ftp_log (gftp_logging_error, NULL,
               _("Retrieve Files: Not connected to a remote site\n"));
      return;
    }

  if (check_reconnect (fromwdata) < 0 || check_reconnect (towdata) < 0)
    return;

  transfer = g_malloc0 (sizeof (*transfer));
  transfer->fromreq = gftp_copy_request (fromwdata->request);
  transfer->toreq = gftp_copy_request (towdata->request);
  transfer->fromwdata = fromwdata;
  transfer->towdata = towdata;

  num = 0;
  templist = gftp_gtk_get_list_selection (fromwdata);
  filelist = fromwdata->files;
  while (templist != NULL)
    {
      templist = get_next_selection (templist, &filelist, &num);
      tempfle = filelist->data;
      if (strcmp (tempfle->file, "..") != 0)
        {
          newfle = copy_fdata (tempfle);
          transfer->files = g_list_append (transfer->files, newfle);
        }
    }

  if (transfer->files != NULL)
    {
      gftp_swap_socks (transfer->fromreq, fromwdata->request);
      gftp_swap_socks (transfer->toreq, towdata->request);

      ret = gftp_gtk_get_subdirs (transfer);
      if (ret < 0)
        disconnect = 1;
      else
        disconnect = 0;

      if (!GFTP_IS_CONNECTED (transfer->fromreq))
        {
          gftpui_disconnect (fromwdata);
          disconnect = 1;
        } 

      if (!GFTP_IS_CONNECTED (transfer->toreq))
        {
          gftpui_disconnect (towdata);
          disconnect = 1;
        } 

      if (disconnect)
        {
          free_tdata (transfer);
          return;
        }

      gftp_swap_socks (fromwdata->request, transfer->fromreq);
      gftp_swap_socks (towdata->request, transfer->toreq);
    }

  if (transfer->files != NULL)
    {
      gftpui_common_add_file_transfer (transfer->fromreq, transfer->toreq, 
                                       transfer->fromwdata, transfer->towdata, 
                                       transfer->files);
      g_free (transfer);
    }
  else
    free_tdata (transfer);
}
Exemple #30
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);
    }
}