changeset 182:33b394ebba68

2003-6-15 Brian Masney <masneyb@gftp.org> * lib/cache.c lib/gftp.h - added gftp_generate_cache_description(). * lib/cache.c lib/gftp.h src/text/gftp-text.c src/gtk/delete-dialog.c src/gtk/menu-items.c src/gtk/misc-gtk.c src/gtk/mkdir-dialog.c - Added description parameter to gftp_delete_cache_entry(). * lib/protocols.c lib/gftp.h - added gftp_fd_open(). It will call open() and then set the socket option close on exec * lib/cache.c lib/local.c lib/misc.c - use gftp_fd_open() instead of open() * lib/rfc959.c lib/protocols.c - on newly created sockets, make sure the close on exec socket option is set * lib/options.h src/text/gftp-text.c src/gtk/transfer.c - added preserve_permissions option * lib/protocols.c (gftp_parse_url) - allow an @ to be in the username * src/text/gftp-text.c - after transfering a file, honor preserve_permissions if it is set * src/gtk/delete-dialog.c - improvments to clearing the expired cache entries
author masneyb
date Sun, 15 Jun 2003 21:28:02 +0000
parents 0153a867819c
children 9d42809f9fb4
files ChangeLog lib/cache.c lib/gftp.h lib/local.c lib/misc.c lib/options.h lib/protocols.c lib/rfc959.c src/gtk/delete_dialog.c src/gtk/menu-items.c src/gtk/misc-gtk.c src/gtk/mkdir_dialog.c src/gtk/transfer.c src/text/gftp-text.c
diffstat 14 files changed, 186 insertions(+), 80 deletions(-) [+]
line wrap: on
line diff
--- a/ChangeLog	Sun Jun 15 13:22:09 2003 +0000
+++ b/ChangeLog	Sun Jun 15 21:28:02 2003 +0000
@@ -1,3 +1,30 @@
+2003-6-15 Brian Masney <masneyb@gftp.org>
+	* lib/cache.c lib/gftp.h - added gftp_generate_cache_description(). 
+
+	* lib/cache.c lib/gftp.h src/text/gftp-text.c src/gtk/delete-dialog.c 
+	src/gtk/menu-items.c src/gtk/misc-gtk.c src/gtk/mkdir-dialog.c - Added
+	description parameter to gftp_delete_cache_entry().
+
+	* lib/protocols.c lib/gftp.h - added gftp_fd_open(). It will call 
+	open() and then set the socket option close on exec
+
+	* lib/cache.c lib/local.c lib/misc.c - use gftp_fd_open() instead of
+	open()
+
+	* lib/rfc959.c lib/protocols.c - on newly created sockets, make sure
+	the close on exec socket option is set
+
+	* lib/options.h src/text/gftp-text.c src/gtk/transfer.c - added
+	preserve_permissions option
+
+	* lib/protocols.c (gftp_parse_url) - allow an @ to be in the username
+
+	* src/text/gftp-text.c - after transfering a file, honor 
+	preserve_permissions if it is set
+
+	* src/gtk/delete-dialog.c - improvments to clearing the expired cache
+	entries
+
 2003-6-14 Brian Masney <masneyb@gftp.org>
 	* lib/cache.c (gftp_delete_cache_entry) - fix for restoring newlines
 
@@ -968,7 +995,7 @@
 
 	* cvsclean - added this script
 
-	* *.[ch] - added $Id: ChangeLog,v 1.88 2003/06/14 14:13:59 masneyb Exp $ tags
+	* *.[ch] - added $Id: ChangeLog,v 1.89 2003/06/15 21:28:00 masneyb Exp $ tags
 
 	* debian/* - updated files from Debian maintainer
 
--- a/lib/cache.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/lib/cache.c	Sun Jun 15 21:28:02 2003 +0000
@@ -72,7 +72,7 @@
 }
 
 
-void
+static void
 gftp_restore_cache_line (gftp_cache_entry * centry, char *line)
 {
   if (centry->pos1 != NULL)
@@ -83,6 +83,19 @@
 }
 
 
+void
+gftp_generate_cache_description (gftp_request * request, char *description,
+                                 size_t len, int ignore_directory)
+{
+  g_snprintf (description, len, "%s://%s@%s:%d%s",
+              request->url_prefix,
+              request->username == NULL ? "" : request->username,
+              request->hostname == NULL ? "" : request->hostname,
+              request->port, 
+              ignore_directory || request->directory == NULL ? "" : request->directory);
+}
+
+
 int
 gftp_new_cache_entry (gftp_request * request)
 {
@@ -105,18 +118,14 @@
     }
 
   tempstr = g_strdup_printf ("%s/index.db", cachedir);
-  if ((fd = open (tempstr, O_WRONLY | O_APPEND | O_CREAT, 
-                  S_IRUSR | S_IWUSR)) == -1)
+  if ((fd = gftp_fd_open (request, tempstr, O_WRONLY | O_APPEND | O_CREAT, 
+                          S_IRUSR | S_IWUSR)) == -1)
     {
-      if (request != NULL)
-        request->logging_function (gftp_logging_error, request->user_data,
-                                   _("Error: Cannot open local file %s: %s\n"),
-                                   tempstr, g_strerror (errno));
-
       g_free (tempstr);
       g_free (cachedir);
       return (-1);
     }
+
   g_free (tempstr);
 
   tempstr = g_strdup_printf ("%s/cache.XXXXXX", cachedir);
@@ -166,15 +175,11 @@
   gftp_cache_entry centry;
   int indexfd, cachefd;
 
-  g_snprintf (description, sizeof (description), "%s://%s@%s:%d%s",
-              request->url_prefix,
-              request->username == NULL ? "" : request->username,
-              request->hostname == NULL ? "" : request->hostname,
-              request->port, 
-              request->directory == NULL ? "" : request->directory);
+  gftp_generate_cache_description (request, description, sizeof (description),
+                                   0);
 
   indexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
-  if ((indexfd = open (indexfile, O_RDONLY)) == -1)
+  if ((indexfd = gftp_fd_open (request, indexfile, O_RDONLY, 0)) == -1)
     {
       g_free (indexfile);
       return (-1);
@@ -199,15 +204,8 @@
               return (-1);
             }
 
-	  if ((cachefd = open (centry.file, O_RDONLY)) == -1)
-            {
-              if (request != NULL)
-                request->logging_function (gftp_logging_error, 
-                                   request->user_data,
-                                   _("Error: Cannot open local file %s: %s\n"),
-                                   centry.file, g_strerror (errno));
-              return (-1);
-            }
+	  if ((cachefd = gftp_fd_open (request, centry.file, O_RDONLY, 0)) == -1)
+            return (-1);
 
           if (lseek (cachefd, 0, SEEK_END) == 0)
             { 
@@ -243,7 +241,7 @@
   int indexfd;
 
   indexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
-  if ((indexfd = open (indexfile, O_RDONLY)) == -1)
+  if ((indexfd = gftp_fd_open (NULL, indexfile, O_RDONLY, 0)) == -1)
     {
       g_free (indexfile);
       return;
@@ -265,7 +263,7 @@
 
 
 void
-gftp_delete_cache_entry (gftp_request * request, int ignore_directory)
+gftp_delete_cache_entry (gftp_request * request, char *descr, int ignore_directory)
 {
   char *oldindexfile, *newindexfile, buf[BUFSIZ], description[BUFSIZ];
   gftp_getline_buffer * rbuf;
@@ -273,29 +271,29 @@
   int indexfd, newfd;
   int remove;
 
-  g_snprintf (description, sizeof (description), "%s://%s@%s:%d%s",
-              request->url_prefix,
-              request->username == NULL ? "" : request->username,
-              request->hostname == NULL ? "" : request->hostname,
-              request->port, 
-              ignore_directory || request->directory == NULL ? "" : request->directory);
+  if (request != NULL)
+    {
+      gftp_generate_cache_description (request, description, sizeof (description),
+                                       ignore_directory);
+    }
+  else if (descr != NULL)
+    { 
+      strncpy (description, descr, sizeof (description));
+    }
+  else
+    return;
 
   oldindexfile = expand_path (BASE_CONF_DIR "/cache/index.db");
-  if ((indexfd = open (oldindexfile, O_RDONLY)) == -1)
+  if ((indexfd = gftp_fd_open (request, oldindexfile, O_RDONLY, 0)) == -1)
     {
       g_free (oldindexfile);
       return;
     }
 
   newindexfile = expand_path (BASE_CONF_DIR "/cache/index.db.new");
-  if ((newfd = open (newindexfile, O_WRONLY | O_CREAT, 
-                     S_IRUSR | S_IWUSR)) == -1)
+  if ((newfd = gftp_fd_open (request, newindexfile, O_WRONLY | O_CREAT, 
+                             S_IRUSR | S_IWUSR)) == -1)
     {
-      if (request != NULL)
-        request->logging_function (gftp_logging_error, request->user_data,
-                                   _("Error: Cannot open local file %s: %s\n"),
-                                   newindexfile, g_strerror (errno));
-
       g_free (oldindexfile);
       g_free (newindexfile);
       return;
--- a/lib/gftp.h	Sun Jun 15 13:22:09 2003 +0000
+++ b/lib/gftp.h	Sun Jun 15 21:28:02 2003 +0000
@@ -558,6 +558,11 @@
 extern gftp_option_type_var gftp_option_types[];
 
 /* cache.c */
+void gftp_generate_cache_description 	( gftp_request * request, 
+					  char *description,
+					  size_t len, 
+					  int ignore_directory );
+
 int gftp_new_cache_entry 		( gftp_request * request );
 
 int gftp_find_cache_entry 		( gftp_request * request );
@@ -565,6 +570,7 @@
 void gftp_clear_cache_files 		( void );
 
 void gftp_delete_cache_entry 		( gftp_request * request,
+					  char *descr,
 					  int ignore_directory );
 
 /* config_file.c */
@@ -875,6 +881,11 @@
 int gftp_get_transfer_status 		( gftp_transfer * tdata, 
 					  ssize_t num_read );
 
+int gftp_fd_open 			( gftp_request * request, 
+					  const char *pathname, 
+					  int flags,
+					  mode_t perms );
+
 /* pty.c */
 char * get_pty_impl 			( void );
 
--- a/lib/local.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/lib/local.c	Sun Jun 15 21:28:02 2003 +0000
@@ -120,13 +120,8 @@
       flags |= O_LARGEFILE;
 #endif
 
-      if ((request->datafd = open (filename, flags)) == -1)
-        {
-          request->logging_function (gftp_logging_error, request->user_data,
-                                   _("Error: Cannot open local file %s: %s\n"),
-                                   filename, g_strerror (errno));
-          return (GFTP_ERETRYABLE); 
-        }
+      if ((request->datafd = gftp_fd_open (request, filename, flags, 0)) == -1)
+        return (GFTP_ERETRYABLE); 
     }
   else
     request->datafd = fd;
@@ -172,13 +167,8 @@
       flags |= O_LARGEFILE;
 #endif
 
-      if ((request->datafd = open (filename, flags, S_IRUSR | S_IWUSR)) == -1)
-        {
-          request->logging_function (gftp_logging_error, request->user_data,
-                                   _("Error: Cannot open local file %s: %s\n"),
-                                   filename, g_strerror (errno));
-          return (GFTP_ERETRYABLE);
-        }
+      if ((request->datafd = gftp_fd_open (request, filename, flags, 0)) == -1)
+        return (GFTP_ERETRYABLE);
     }
   else
     request->datafd = fd;
--- a/lib/misc.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/lib/misc.c	Sun Jun 15 21:28:02 2003 +0000
@@ -239,14 +239,14 @@
   char buf[8192];
   ssize_t n;
 
-  if ((srcfd = open (source, O_RDONLY)) == -1)
+  if ((srcfd = gftp_fd_open (NULL, source, O_RDONLY, 0)) == -1)
     {
       printf (_("Error: Cannot open local file %s: %s\n"),
               source, g_strerror (errno));
       exit (1);
     }
 
-  if ((destfd = open (dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
+  if ((destfd = gftp_fd_open (NULL, dest, O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR)) == -1)
     {
       printf (_("Error: Cannot open local file %s: %s\n"),
               dest, g_strerror (errno));
--- a/lib/options.h	Sun Jun 15 13:22:09 2003 +0000
+++ b/lib/options.h	Sun Jun 15 21:28:02 2003 +0000
@@ -56,6 +56,10 @@
    gftp_option_type_checkbox, GINT_TO_POINTER(0), NULL, 0,
    N_("Overwrite files by default or set to resume file transfers"), 
    GFTP_PORT_GTK, NULL},
+  {"preserve_permissions", N_("Preserve file permissions"), 
+   gftp_option_type_checkbox, GINT_TO_POINTER(1), NULL, 0,
+   N_("Preserve file permissions of transfered files"), GFTP_PORT_ALL,
+   NULL},
   {"refresh_files", N_("Refresh after each file transfer"), 
    gftp_option_type_checkbox, GINT_TO_POINTER(0), NULL, 0,
    N_("Refresh the listbox after each file is transfered"), GFTP_PORT_GTK,
--- a/lib/protocols.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/lib/protocols.c	Sun Jun 15 21:28:02 2003 +0000
@@ -559,7 +559,7 @@
       return (0);
     }
 
-  if ((endhostpos = strchr (pos, '@')) != NULL)
+  if ((endhostpos = strrchr (pos, '@')) != NULL)
     {
       /* A user/password was entered */
       if ((endpos = strchr (pos, ':')) == NULL || endhostpos < endpos)
@@ -1789,6 +1789,15 @@
   port = ntohs (port);
 #endif /* HAVE_GETADDRINFO */
 
+  if (fcntl (sock, F_SETFD, 1) == -1)
+    {
+      request->logging_function (gftp_logging_error, request->user_data,
+                                 _("Error: Cannot set close on exec flag: %s\n"),
+                                 g_strerror (errno));
+
+      return (GFTP_ERETRYABLE);
+    }
+
   request->logging_function (gftp_logging_misc, request->user_data,
                              _("Connected to %s:%d\n"), connect_host, port);
 
@@ -2370,3 +2379,39 @@
   return (0);
 }
 
+
+int
+gftp_fd_open (gftp_request * request, const char *pathname, int flags, mode_t mode)
+{
+  mode_t mask;
+  int fd;
+
+  if (mode == 0 && (flags & O_CREAT))
+    {
+      mask = umask (0); /* FIXME - improve */
+      umask (mask);
+      mode = 0666 & ~mask;
+    }
+
+  if ((fd = open (pathname, flags, mode)) < 0)
+    {
+      if (request != NULL)
+        request->logging_function (gftp_logging_error, request->user_data,
+                                   _("Error: Cannot open local file %s: %s\n"),
+                                   pathname, g_strerror (errno));
+      return (GFTP_ERETRYABLE);
+    }
+
+  if (fcntl (fd, F_SETFD, 1) == -1)
+    {
+      if (request != NULL)
+        request->logging_function (gftp_logging_error, request->user_data,
+                                   _("Error: Cannot set close on exec flag: %s\n"),
+                                   g_strerror (errno));
+
+      return (-1);
+    }
+
+  return (fd);
+}
+
--- a/lib/rfc959.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/lib/rfc959.c	Sun Jun 15 21:28:02 2003 +0000
@@ -589,6 +589,15 @@
       return (GFTP_ERETRYABLE);
     }
 
+  if (fcntl (parms->data_connection, F_SETFD, 1) == -1)
+    {
+      request->logging_function (gftp_logging_error, request->user_data,
+                                 _("Error: Cannot set close on exec flag: %s\n"),
+                                 g_strerror (errno));
+
+      return (GFTP_ERETRYABLE);
+    }
+
   data_addr_len = sizeof (data_addr);
   memset (&data_addr, 0, data_addr_len);
   data_addr.sin_family = AF_INET;
@@ -727,6 +736,15 @@
       return (GFTP_ERETRYABLE);
     }
 
+  if (fcntl (parms->data_connection, F_SETFD, 1) == -1)
+    {
+      request->logging_function (gftp_logging_error, request->user_data,
+                                 _("Error: Cannot set close on exec flag: %s\n"),
+                                 g_strerror (errno));
+
+      return (GFTP_ERETRYABLE);
+    }
+
   data_addr_len = sizeof (data_addr);
   /* This condition shouldn't happen. We better check anyway... */
   if (data_addr_len != request->hostp->ai_addrlen) 
--- a/src/gtk/delete_dialog.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/src/gtk/delete_dialog.c	Sun Jun 15 21:28:02 2003 +0000
@@ -23,14 +23,7 @@
 static void
 delete_purge_cache (gpointer key, gpointer value, gpointer user_data)
 {
-  gftp_request * request;
-  char *olddir;
-
-  request = user_data;
-  olddir = request->directory;
-  request->directory = key;
-  gftp_delete_cache_entry (request, 0);
-  request->directory = olddir;
+  gftp_delete_cache_entry (NULL, key, 0);
   g_free (key);
 }
 
@@ -38,8 +31,8 @@
 static void *
 do_delete_thread (void *data)
 {
+  char *tempstr, description[BUFSIZ];
   gftp_transfer * transfer;
-  char *pos, *tempstr;
   gftp_file * tempfle;
   GHashTable * rmhash;
   GList * templist;
@@ -69,15 +62,15 @@
           else
             success = gftp_remove_file (transfer->fromreq, tempfle->file);
 
-          if (success == 0 && (pos = strrchr (tempfle->file, '/')))
+          if (success == 0)
             {
-              *pos = '\0';
-              if (g_hash_table_lookup (rmhash, tempfle->file) == NULL)
+              gftp_generate_cache_description (transfer->fromreq, description, 
+                                               sizeof (description), 0);
+              if (g_hash_table_lookup (rmhash, description) == NULL)
                 {
-                  tempstr = g_strconcat (tempfle->file, NULL);
+                  tempstr = g_strdup (description);
                   g_hash_table_insert (rmhash, tempstr, NULL);
                 }
-              *pos = '/';
             }
 
           if (templist == transfer->files || 
@@ -86,7 +79,7 @@
           templist = templist->prev;
         }
 
-      g_hash_table_foreach (rmhash, delete_purge_cache, transfer->fromreq);
+      g_hash_table_foreach (rmhash, delete_purge_cache, NULL);
       g_hash_table_destroy (rmhash);
     }
   else
--- a/src/gtk/menu-items.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/src/gtk/menu-items.c	Sun Jun 15 21:28:02 2003 +0000
@@ -136,7 +136,7 @@
   gftp_window_data * wdata;
 
   wdata = data;
-  gftp_delete_cache_entry (wdata->request, 1);
+  gftp_delete_cache_entry (wdata->request, NULL, 1);
   gftp_disconnect (wdata->request);
   remove_files_window (wdata);
   update_window (wdata);
--- a/src/gtk/misc-gtk.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/src/gtk/misc-gtk.c	Sun Jun 15 21:28:02 2003 +0000
@@ -213,7 +213,7 @@
     return;
   gtk_clist_freeze (GTK_CLIST (wdata->listbox));
   remove_files_window (wdata);
-  gftp_delete_cache_entry (wdata->request, 0);
+  gftp_delete_cache_entry (wdata->request, NULL, 0);
   ftp_list_files (wdata, 0);
   gtk_clist_thaw (GTK_CLIST (wdata->listbox));
 }
--- a/src/gtk/mkdir_dialog.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/src/gtk/mkdir_dialog.c	Sun Jun 15 21:28:02 2003 +0000
@@ -82,7 +82,7 @@
 
   if ((int) generic_thread (do_make_dir_thread, wdata))
     {
-      gftp_delete_cache_entry (wdata->request, 0);
+      gftp_delete_cache_entry (wdata->request, NULL, 0);
       refresh (wdata);
     }
 }
--- a/src/gtk/transfer.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/src/gtk/transfer.c	Sun Jun 15 21:28:02 2003 +0000
@@ -544,7 +544,7 @@
 void * 
 gftp_gtk_transfer_files (void *data)
 {
-  int i, mode, tofd, fromfd;
+  int i, mode, tofd, fromfd, preserve_permissions;
   gftp_transfer * transfer;
   char buf[8192];
   off_t fromsize, total;
@@ -558,6 +558,9 @@
   memcpy (&transfer->lasttime, &transfer->starttime, 
           sizeof (transfer->lasttime));
 
+  gftp_lookup_request_option (transfer->fromreq, "preserve_permissions", 
+                              &preserve_permissions);
+
   while (transfer->curfle != NULL)
     {
       num_read = -1;
@@ -729,7 +732,7 @@
                          curfle->file, transfer->kbs);
         }
 
-      if (!curfle->is_fd)
+      if (!curfle->is_fd && preserve_permissions)
         {
           if (curfle->attribs)
             {
--- a/src/text/gftp-text.c	Sun Jun 15 13:22:09 2003 +0000
+++ b/src/text/gftp-text.c	Sun Jun 15 21:28:02 2003 +0000
@@ -502,7 +502,7 @@
   else
     {
       if (gftp_remove_file (request, command) == 0)
-        gftp_delete_cache_entry (request, 0);
+        gftp_delete_cache_entry (request, NULL, 0);
     }
   return (1);
 }
@@ -843,12 +843,16 @@
 int
 gftp_text_transfer_files (gftp_transfer * transfer)
 {
+  int i, j, sw, tot, preserve_permissions;
   char buf[8192], *progress = "|/-\\";
   struct timeval updatetime;
   long fromsize, total;
   gftp_file * curfle;
-  int i, j, sw, tot;
   ssize_t num_read;
+  mode_t mode;
+
+  gftp_lookup_request_option (transfer->fromreq, "preserve_permissions",
+                              &preserve_permissions);
 
   for (transfer->curfle = transfer->files;
        transfer->curfle != NULL;
@@ -929,6 +933,19 @@
           gftp_end_transfer (transfer->toreq);
         }
 
+      if (!curfle->is_fd && preserve_permissions)
+        {
+          if (curfle->attribs)
+            {
+              mode = gftp_parse_attribs (curfle->attribs);
+              if (mode != 0)
+                gftp_chmod (transfer->toreq, curfle->destfile, mode);
+            } 
+
+          if (curfle->datetime != 0)
+            gftp_set_file_time (transfer->toreq, curfle->destfile,
+                                curfle->datetime);
+        }
     }
   return (1);
 }