gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r2498 - gnunet-gtk/src/plugins/fs


From: grothoff
Subject: [GNUnet-SVN] r2498 - gnunet-gtk/src/plugins/fs
Date: Sun, 12 Mar 2006 17:34:53 -0800 (PST)

Author: grothoff
Date: 2006-03-12 17:34:49 -0800 (Sun, 12 Mar 2006)
New Revision: 2498

Modified:
   gnunet-gtk/src/plugins/fs/download.c
   gnunet-gtk/src/plugins/fs/fs.h
Log:
fixing Mantis 1007

Modified: gnunet-gtk/src/plugins/fs/download.c
===================================================================
--- gnunet-gtk/src/plugins/fs/download.c        2006-03-12 15:29:11 UTC (rev 
2497)
+++ gnunet-gtk/src/plugins/fs/download.c        2006-03-13 01:34:49 UTC (rev 
2498)
@@ -35,6 +35,7 @@
   struct DL * next;
   struct ECRS_URI * uri;
   char * filename;
+  char * finalName;
   GtkTreeRowReference * rr;
   GtkTreeModel * model;
 } DownloadList;
@@ -111,24 +112,18 @@
                              gpointer unused) {
   struct ECRS_URI * uri;
   struct ECRS_MetaData * meta;
-  char * filename;
-  char * pfx;
-  char * lnk;
-  char * fn;
-  char * inc;
+  char * uri_name;
+  char * final_download_destination;
+  char * final_download_dir;
   DownloadList * list;
   GtkTreeIter iiter;
   GtkWidget * spin;
   const char * name;
   const char * mime;
-  struct stat sbuf;
   unsigned int anon;
   GtkTreePath *dirTreePath;
   char *dirPath;
   unsigned int dirPathLen;
-#ifdef WINDOWS
-  unsigned int downDirLen;
-#endif
 
   uri = NULL;
   meta = NULL;
@@ -145,14 +140,6 @@
     BREAK();
     return;
   }
-  spin = getAnonymityButtonFromTM(model);
-  if (spin == NULL) {
-    BREAK();
-    anon = 1;
-  } else {
-    anon = gtk_spin_button_get_value_as_int
-      (GTK_SPIN_BUTTON(spin));
-  }
   if (! ECRS_isFileUri(uri)) {
     if (ECRS_isNamespaceUri(uri)) {
       /* start namespace search; would probably be better
@@ -172,130 +159,36 @@
       return;
     }
   }
-  filename = ECRS_uriToString(uri);
-  if ( (filename == NULL) ||
-       (strlen(filename) <
+
+
+  uri_name = ECRS_uriToString(uri);
+  if ( (uri_name == NULL) ||
+       (strlen(uri_name) <
         strlen(ECRS_URI_PREFIX) +
         strlen(ECRS_FILE_INFIX)) ) {
     BREAK();
-    FREENONNULL(filename);
+    FREENONNULL(uri_name);
     return;
   }
+
   if (name == NULL)
-    name = filename;
+    name = uri_name;
 
-  fn = getFileName("FS",
-                   "DOWNLOADDIR",
-                   _("You must specify a directory in the configuration"
-                     " in section `%s' under `%s'."));
-  inc = getFileName("FS",
-                   "INCOMINGDIR",
-                   _("You must specify a directory in the configuration"
-                     " in section `%s' under `%s'."));
-                     
-  if (strcmp(fn, inc) == 0) {
-    LOG(LOG_ERROR, _("You must specify different directories in the "
-                      "configuration in section `%s' under `%s' and `%s'."),
-                      "FS", "DOWNLOADDIR", "INCOMINGDIR");
-                      
-    FREE(filename);
-    FREENONNULL(fn);
-    FREENONNULL(inc);
-                      
-    return;
-  }
+  final_download_dir = getFileName("FS",
+                                  "INCOMINGDIR",
+                                  _("You must specify a directory in the 
configuration"
+                                    " in section `%s' under `%s'."));
+  mkdirp(final_download_dir);
 
-  addLogEntry(_("Downloading `%s'"), name);
 
-  list = MALLOC(sizeof(DownloadList));
-  list->next = head;
-  list->rr = NULL;
-  list->model = NULL;
-  if (YES == ECRS_isDirectory(meta)) {
-    list->rr = gtk_tree_row_reference_new(model, path);
-    list->model = model;
-  }
-  list->uri = ECRS_dupUri(uri);
-                     
-#ifdef WINDOWS
-  downDirLen = strlen(fn);
-#endif
-  mkdirp(fn);
-  pfx = MALLOC(strlen(fn) + 2 +
-               strlen(name) + 4); /* 4 = ".gnd" */
-  lnk = MALLOC(strlen(fn) + 2 +
-               strlen(filename));
-  strcpy(pfx, fn);
-  if (pfx[strlen(pfx)-1] != DIR_SEPARATOR)
-    strcat(pfx,
-           DIR_SEPARATOR_STR);
-  strcpy(lnk, fn);
-  FREE(fn);
-  if (lnk[strlen(lnk)-1] != DIR_SEPARATOR)
-    strcat(lnk,
-           DIR_SEPARATOR_STR);
-  strcat(pfx,
-         name);
-  strcat(lnk,
-         &filename[strlen(ECRS_URI_PREFIX) +
-                   strlen(ECRS_FILE_INFIX)]);
-
-  /* Append ".gnd" if needed */
-  if (mime && strcmp(mime, GNUNET_DIRECTORY_MIME) == 0) {
-    int len = strlen(pfx);
-    if (len > 4 && strcmp(pfx + len - 4, GNUNET_DIRECTORY_EXT) != 0) {
-      char *end = pfx + len - 1;
-      if (*end == '/' || *end == '\\')
-        *end = 0;
-    }
-    strcat(pfx, GNUNET_DIRECTORY_EXT);
-  }
-
-#ifdef WINDOWS
-  {
-    /* Do not exceed MAX_PATH under Windows
-       The longest path we get is
-         $DOWNLOADDIR\hash.lnk
-        or
-         $INCOMINGDIR\hash.lnk
-
-       We truncate the hash here accordingly. */
-
-    int diff; /* How much is INCOMINGDIR larger than DOWNLOADDIR */
-
-    diff = downDirLen - strlen(inc);
-    if (diff < 0)
-      diff = 0;
-
-    char *end = lnk + MAX_PATH - 5 - diff; /* 4 = ".lnk" */
-
-    *end = 0;
-
-    if (strchr(end + 1, DIR_SEPARATOR)) {
-      BREAK();
-      return;
-    }
-  }
-#endif
-
-  FREENONNULL(inc);
-
-  if (0 != STAT(pfx,
-                &sbuf)) {
-    if (0 != SYMLINK(lnk, pfx))
-      LOG_FILE_STRERROR(LOG_ERROR, "symlink", pfx);
-  } else {
-    FREE(pfx);
-    pfx = STRDUP(lnk);
-  }
-
   /* If file is inside a directory, get the full path */
   dirTreePath = gtk_tree_path_copy(path);
   dirPath = MALLOC(1);
+  dirPath[0] = '\0';
   dirPathLen = 0;
   while (gtk_tree_path_get_depth(dirTreePath) > 1) {
-    const char *dirname;
-    char *new;
+    const char * dirname;
+    char * new;
 
     if (! gtk_tree_path_up(dirTreePath))
       break;
@@ -308,17 +201,58 @@
                        &iiter,
                        SEARCH_NAME, &dirname,
                        -1);
-    dirPathLen += strlen(dirname) + 1;
+    dirPathLen = strlen(dirPath) + strlen(dirname) + strlen(DIR_SEPARATOR_STR) 
+ 1;
     new = MALLOC(dirPathLen + 1);
     strcpy(new, dirname);
-    strcat(new, DIR_SEPARATOR_STR);
+    if (new[strlen(new)-1] != DIR_SEPARATOR)
+      strcat(new, DIR_SEPARATOR_STR);
     strcat(new, dirPath);
     FREE(dirPath);
     dirPath = new;
   }
   gtk_tree_path_free(dirTreePath);
 
-  list->filename = pfx;
+
+  /* construct completed/directory/real-filename */
+  final_download_destination = MALLOC(strlen(final_download_dir) + 2 +
+               strlen(name) + strlen(GNUNET_DIRECTORY_EXT) +
+              strlen(dirPath));
+  strcpy(final_download_destination, final_download_dir);
+  if (final_download_destination[strlen(final_download_destination)-1] != 
DIR_SEPARATOR)
+    strcat(final_download_destination,
+           DIR_SEPARATOR_STR);
+  strcat(final_download_destination, dirPath);
+  mkdirp(final_download_destination);
+  strcat(final_download_destination, name);
+  if ( (final_download_destination[strlen(final_download_destination) - 1] == 
'/') ||
+       (final_download_destination[strlen(final_download_destination) - 1] == 
'\\') )
+    final_download_destination[strlen(final_download_destination) - 1] = '\0'; 
+  /* append ".gnd" if needed (== directory and .gnd not present) */
+  if ( (mime != NULL) && 
+       (0 == strcmp(mime, GNUNET_DIRECTORY_MIME)) &&
+       ( (strlen(final_download_destination) < strlen(GNUNET_DIRECTORY_EXT)) ||
+        (0 != 
strcmp(&final_download_destination[strlen(final_download_destination) - 
strlen(GNUNET_DIRECTORY_EXT)],
+                     GNUNET_DIRECTORY_EXT)) ) )
+    strcat(final_download_destination, GNUNET_DIRECTORY_EXT);
+    
+  /* setup visualization */
+  list = MALLOC(sizeof(DownloadList));
+  list->next = head;
+  list->rr = NULL;
+  list->model = NULL;
+  if (YES == ECRS_isDirectory(meta)) {
+    list->rr = gtk_tree_row_reference_new(model, path);
+    list->model = model;
+  }
+  list->uri = ECRS_dupUri(uri);
+  list->filename = final_download_destination;
+  list->finalName = MALLOC(strlen(final_download_dir) + strlen(dirPath) + 
strlen(name) + 2);
+  strcpy(list->finalName, final_download_dir);
+  if (final_download_dir[strlen(final_download_dir)-1] != DIR_SEPARATOR)
+    strcat(list->finalName, DIR_SEPARATOR_STR);
+  strcat(list->finalName, dirPath);
+  mkdirp(list->finalName);
+  strcat(list->finalName, name);
   head = list;
   gtk_tree_store_insert(summary,
                         &iiter,
@@ -326,23 +260,34 @@
                         0);
   gtk_tree_store_set(summary,
                      &iiter,
-                     DOWNLOAD_FILENAME, lnk,
-                     DOWNLOAD_LINKNAME, pfx,
+                     DOWNLOAD_FILENAME, final_download_destination,
+                     DOWNLOAD_SHORTNAME, name,
                      DOWNLOAD_SIZE, ECRS_fileSize(uri),
                      DOWNLOAD_PROGRESS, 0, /* progress */
-                     DOWNLOAD_URISTRING, filename,
+                     DOWNLOAD_URISTRING, uri_name,
                      DOWNLOAD_URI, ECRS_dupUri(uri),
-                     DOWNLOAD_TREEPATH, list->rr,
-                     DOWNLOAD_DIRPATH, dirPath,
-                     /* internal: row reference! */
+                     DOWNLOAD_TREEPATH, list->rr, /* internal: row reference! 
*/
+                     DOWNLOAD_DIRPATH, dirPath,                     
                      -1);
-  FREE(filename);
+  FREE(uri_name);
+  FREE(dirPath);
+  FREENONNULL(final_download_dir);
 
+
+  spin = getAnonymityButtonFromTM(model);
+  if (spin == NULL) {
+    BREAK();
+    anon = 1;
+  } else {
+    anon = gtk_spin_button_get_value_as_int
+      (GTK_SPIN_BUTTON(spin));
+  }
+
+  addLogEntry(_("Downloading `%s'"), name);
   FSUI_startDownload(ctx,
                      anon,
                      uri,
-                     pfx);
-  FREE(lnk);
+                     final_download_destination);
 }
 
 void on_downloadButton_clicked(GtkWidget * treeview,
@@ -408,19 +353,15 @@
  */
 void displayDownloadComplete(const struct ECRS_URI * uri,
                              const char * filename) {
-  char * ren;
-  const char * fn;
-  const char * ln;
   unsigned long long size;
   char * data;
   int fd;
   struct ECRS_MetaData * meta;
   DownloadList * pos;
-  GtkTreeIter iter;
-  GtkTreePath * path;
-  struct ECRS_URI * u;
-  char *dirPath;
 
+  LOG(LOG_DEBUG,
+      "Download '%s' complete\n",
+      filename);
   pos = head;
   while (pos != NULL) {
     if (ECRS_equalsUri(uri,
@@ -432,148 +373,43 @@
   /* Not available for resumed downloads */
   if (pos != NULL) {
     if ( (pos->rr != NULL) &&
-         (gtk_tree_row_reference_valid(pos->rr)) ) {
-  
+         (gtk_tree_row_reference_valid(pos->rr)) ) { 
       /* update directory view (if applicable!) */
       if (OK == getFileSize(filename, &size)) {
-        fd = fileopen(filename, O_RDONLY);
-        data = MMAP(NULL,
-                    size,
-                    PROT_READ,
-                    MAP_SHARED,
-                    fd,
-                    0);
-        meta = NULL;
-        if (data != NULL) {
-          ECRS_listDirectory(data,
-                             size,
-                             &meta,
-                             &addFilesToDirectory,
-                             (void*)uri);
-          MUNMAP(data, size);
-        }
-        CLOSE(fd);
-        if (meta != NULL)
-          ECRS_freeMetaData(meta);
+       LOG(LOG_DEBUG,
+           "Updating directory view of '%s'\n",
+           filename);
+
+       meta = NULL;
+       fd = fileopen(filename, O_RDONLY);
+       if (fd == -1) {
+         LOG_FILE_STRERROR(LOG_ERROR, "open", filename);
+       } else {
+         data = MMAP(NULL,
+                     size,
+                     PROT_READ,
+                     MAP_SHARED,
+                     fd,
+                     0);
+         if (data == MAP_FAILED) {
+           LOG_FILE_STRERROR(LOG_ERROR, "mmap", filename);
+         } else {
+           if (data != NULL) {
+             ECRS_listDirectory(data,
+                                size,
+                                &meta,
+                                &addFilesToDirectory,
+                                (void*)uri);
+             MUNMAP(data, size);
+           }
+         }
+         CLOSE(fd);
+       }
+       if (meta != NULL)
+         ECRS_freeMetaData(meta);      
       }
-  
-      path = gtk_tree_row_reference_get_path(pos->rr);
-      if (gtk_tree_path_get_depth(path) > 1) {
-        gtk_tree_path_free(path);
-        return;
-      }
-      gtk_tree_path_free(path);
     }
   }
-
-  /* only rename top-level files, not files inside of directories! */
-  if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary),
-                                    &iter)) {
-    do {
-      ln = NULL;
-      fn = NULL;
-      gtk_tree_model_get(GTK_TREE_MODEL(summary),
-                         &iter,
-                         DOWNLOAD_URI, &u,
-                         DOWNLOAD_FILENAME, &fn,
-                         DOWNLOAD_LINKNAME, &ln,
-                         DOWNLOAD_DIRPATH, &dirPath,
-                         -1);
-      if (ECRS_equalsUri(u, uri)) {
-        char *dstPath, *newFn, *dstFile;
-        char fnURL[PATH_MAX + 1], dummy[2];
-        size_t len;
-
-        len = strlen(ln);
-        /* Don't change the name of GNUnet directories (.gnd) */
-        if (len >= 4 && strcmp(ln + len - 4, ".gnd") != 0)
-          ren = ECRS_suggestFilename(fn);
-        else
-          ren = NULL;
-          
-        newFn = strrchr(ren ? ren : ln, DIR_SEPARATOR) + 1;
-
-        dstPath = getFileName("FS",
-                         "INCOMINGDIR",
-                         _("You must specify a directory in the configuration"
-                           " in section `%s' under `%s'."));
-        if (!dstPath) {
-          FREENONNULL(ren);
-          return;
-        }
-
-        /* If file is contained in a directory, create directory structure in
-           the file system. */
-        if (dirPath) {
-          unsigned int pathLen = strlen(dirPath);
-          if (pathLen) {
-            pathLen += strlen(dstPath) + 2;
-            dstPath = REALLOC(dstPath, pathLen);
-            strcat(dstPath, DIR_SEPARATOR_STR);
-            strcat(dstPath, dirPath);
-            FREE(dirPath);
-          }
-        }
-
-        mkdirp(dstPath);
-
-        dstFile = MALLOC(strlen(dstPath) + strlen(newFn) + 2);
-        strcpy(dstFile, dstPath);
-        strcat(dstFile, DIR_SEPARATOR_STR);
-        strcat(dstFile, newFn);
-        
-        if ((len = READLINK(ln ? ln : fn, fnURL, PATH_MAX)) == -1) {
-          LOG(LOG_ERROR, _("Could not open symlink `%s': %s\n"),
-            ln ? ln : fn, STRERROR(errno));
-            
-          FREE(dstFile);
-          FREENONNULL(ren);
-            
-          return;
-        }
-        fnURL[len] = 0;
-        
-        /* If the file was downloaded before, fnURL is a symlink to
-           dstFile */
-        if ((READLINK(fnURL, dummy, 1) == -1) && (errno == EINVAL)) {
-          if (RENAME(fnURL, dstFile) == -1) {
-            /* renaming failed, try to copy */
-            if (!copyFile(fnURL, dstFile)) {
-              LOG(LOG_ERROR, _("Could not move or copy downloaded file %s to 
%s: %s."),
-                fnURL, dstFile, STRERROR(errno));
-              FREE(dstFile);
-              FREENONNULL(ren);
-  
-              return;
-            }
-  
-            if (REMOVE(fnURL) == -1)
-              LOG(LOG_ERROR, _("Could not remove temporary file %s: %s\n"), 
fnURL, STRERROR(errno));
-          }
-
-          SYMLINK(dstFile, fnURL);
-
-        }  
-
-        if (ren)
-          REMOVE(ren);
-        else
-          REMOVE(ln);
-
-        gtk_tree_store_set(summary,
-                           &iter,
-                           DOWNLOAD_LINKNAME, dstFile,
-                           -1);
-        FREENONNULL(ren);
-        FREE(dstFile);
-        FREE(dstPath);
-
-        break;
-      }
-    } while (gtk_tree_model_iter_next(GTK_TREE_MODEL(summary),
-                                      &iter));
-  }
-
 }
 
 static int delDownloadView(void * cls,
@@ -585,7 +421,8 @@
                            int isRecursive,
                            unsigned int anonymityLevel) {
   GtkTreeIter iter;
-  char * f, * f2, * fn;
+  char * f;
+  char * fn;
   struct ECRS_URI * u;
 
   if (gtk_tree_model_get_iter_first(GTK_TREE_MODEL(summary),
@@ -594,16 +431,14 @@
       gtk_tree_model_get(GTK_TREE_MODEL(summary),
                          &iter,
                          DOWNLOAD_FILENAME, &f,
-                         DOWNLOAD_LINKNAME, &f2,
                          DOWNLOAD_URI, &u,
                          -1);
                          
       f = strrchr(f, DIR_SEPARATOR);
-      f2 = strrchr(f2, DIR_SEPARATOR);
       fn = strrchr(filename, DIR_SEPARATOR);
                          
       if ( (ECRS_equalsUri(u, uri)) &&
-           (0 == strcmp(f, fn) || 0 == strcmp(f2, fn)) ) {
+           (0 == strcmp(f, fn)) ) {
         gtk_tree_store_remove(summary,
                               &iter);
         break;
@@ -679,6 +514,7 @@
   GtkTreeIter iiter;
   int progress;
   char * uriname;
+  const char * sname;
 
   if (filesize != 0)
     progress = bytesCompleted * 100 / filesize;
@@ -689,10 +525,15 @@
                         &iiter,
                         NULL,
                         0);
+  sname = filename[strlen(filename)-1];
+  while ( (sname > filename) &&
+         (sname[-1] != '/') &&
+         (sname[-1] != '\\') )
+    sname--;
   gtk_tree_store_set(summary,
                      &iiter,
                      DOWNLOAD_FILENAME, filename,
-                     DOWNLOAD_LINKNAME, filename,
+                     DOWNLOAD_SHORTNAME, sname,
                      DOWNLOAD_SIZE, filesize,
                      DOWNLOAD_PROGRESS, progress,
                      DOWNLOAD_URISTRING, uriname,
@@ -714,14 +555,14 @@
                                       "activeDownloadsList");
   summary =
     gtk_tree_store_new(DOWNLOAD_NUM,
-                       G_TYPE_STRING, /* name (URI as string) */
+                       G_TYPE_STRING, /* name (full-path file name) */
                        G_TYPE_STRING, /* name (user-friendly name) */
                        G_TYPE_UINT64,  /* size */
                        G_TYPE_INT,  /* progress */
                        G_TYPE_STRING, /* uri */
                        G_TYPE_POINTER,  /* url */
                        G_TYPE_POINTER, /* internal: gtk tree path / NULL */
-                       G_TYPE_POINTER); /* directory path if file is inside a 
dir */
+                       G_TYPE_STRING); /* directory path if file is inside a 
dir */
   gtk_tree_view_set_model(GTK_TREE_VIEW(downloadList),
                           GTK_TREE_MODEL(summary));
   renderer = gtk_cell_renderer_progress_new();
@@ -730,7 +571,7 @@
                                               _("Name"),
                                               renderer,
                                               "value", DOWNLOAD_PROGRESS,
-                                              "text", DOWNLOAD_LINKNAME,
+                                              "text", DOWNLOAD_SHORTNAME,
                                               NULL);
   
gtk_tree_view_column_set_resizable(gtk_tree_view_get_column(GTK_TREE_VIEW(downloadList),
                                               col - 1),

Modified: gnunet-gtk/src/plugins/fs/fs.h
===================================================================
--- gnunet-gtk/src/plugins/fs/fs.h      2006-03-12 15:29:11 UTC (rev 2497)
+++ gnunet-gtk/src/plugins/fs/fs.h      2006-03-13 01:34:49 UTC (rev 2498)
@@ -59,7 +59,7 @@
 
 enum {
   DOWNLOAD_FILENAME = 0,
-  DOWNLOAD_LINKNAME,
+  DOWNLOAD_SHORTNAME,
   DOWNLOAD_SIZE,
   DOWNLOAD_PROGRESS,
   DOWNLOAD_URISTRING,





reply via email to

[Prev in Thread] Current Thread [Next in Thread]