2004-05-28 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mono / metadata / assembly.c
index 148cddb681e8507920aedde450d6145bde0e810b..d5b4fdcd8d6b19e78c1aad09fb5f1364cce91e89 100644 (file)
@@ -28,6 +28,9 @@
 #include <mono/utils/mono-uri.h>
 #include <mono/metadata/mono-config.h>
 #include <mono/utils/mono-digest.h>
+#ifdef PLATFORM_WIN32
+#include <mono/os/util.h>
+#endif
 
 /* the default search path is just MONO_ASSEMBLIES */
 static const char*
@@ -50,23 +53,7 @@ static CRITICAL_SECTION assemblies_mutex;
 /* A hastable of thread->assembly list mappings */
 static GHashTable *assemblies_loading;
 
-static gboolean allow_user_gac = FALSE;
-
-#ifdef PLATFORM_WIN32
-
-static void
-init_default_path (void)
-{
-       int i;
-
-       default_path [0] = g_strdup (MONO_ASSEMBLIES);
-       for (i = strlen (MONO_ASSEMBLIES) - 1; i >= 0; i--) {
-               if (default_path [0][i] == '/')
-                       ((char*) default_path [0])[i] = '\\';
-       }
-}
-#endif
-
+static char **extra_gac_paths = NULL;
 
 static gchar*
 encode_public_tok (const guchar *token, gint32 len)
@@ -85,17 +72,51 @@ encode_public_tok (const guchar *token, gint32 len)
 }
 
 static void
-check_env (void) {
+check_path_env (void) {
        const char *path;
        char **splitted;
        
-       path = getenv ("MONO_PATH");
+       path = g_getenv ("MONO_PATH");
        if (!path)
                return;
+
        splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
        if (assemblies_path)
                g_strfreev (assemblies_path);
        assemblies_path = splitted;
+       if (g_getenv ("MONO_DEBUG") == NULL)
+               return;
+
+       while (*splitted) {
+               if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
+                       g_warning ("'%s' in MONO_PATH doesn't exist or has wrong permissions.", *splitted);
+
+               splitted++;
+       }
+}
+
+static void
+check_extra_gac_path_env (void) {
+       const char *path;
+       char **splitted;
+       
+       path = g_getenv ("MONO_GAC_PATH");
+       if (!path)
+               return;
+
+       splitted = g_strsplit (path, G_SEARCHPATH_SEPARATOR_S, 1000);
+       if (extra_gac_paths)
+               g_strfreev (extra_gac_paths);
+       extra_gac_paths = splitted;
+       if (g_getenv ("MONO_DEBUG") == NULL)
+               return;
+
+       while (*splitted) {
+               if (**splitted && !g_file_test (*splitted, G_FILE_TEST_IS_DIR))
+                       g_warning ("'%s' in MONO_GAC_PATH doesn't exist or has wrong permissions.", *splitted);
+
+               splitted++;
+       }
 }
 
 static gboolean
@@ -199,6 +220,12 @@ mono_assembly_setrootdir (const char *root_dir)
        default_path [0] = g_strdup (root_dir);
 }
 
+G_CONST_RETURN gchar *
+mono_assembly_getrootdir (void)
+{
+       return default_path [0];
+}
+
 /**
  * mono_assemblies_init:
  *
@@ -208,10 +235,11 @@ void
 mono_assemblies_init (void)
 {
 #ifdef PLATFORM_WIN32
-       init_default_path ();
+       mono_set_rootdir ();
 #endif
 
-       check_env ();
+       check_path_env ();
+       check_extra_gac_path_env ();
 
        InitializeCriticalSection (&assemblies_mutex);
 
@@ -333,7 +361,7 @@ mono_assembly_load_references (MonoImage *image, MonoImageOpenStatus *status)
                                references [i] = mono_assembly_load (&aname, image->assembly->basedir, status);
                        }
                        if (references [i] != NULL){
-                               if (getenv ("MONO_SILENT_WARNING") == NULL)
+                               if (g_getenv ("MONO_SILENT_WARNING") == NULL)
                                        g_printerr ("Compat mode: the request from %s to load %s was remapped (http://www.go-mono.com/remap.html)\n",
                                                 image->name, aname.name);
                                
@@ -736,6 +764,77 @@ mono_assembly_load_from (MonoImage *image, const char*fname,
        return ass;
 }
 
+static MonoAssembly*
+probe_for_partial_name (const char *basepath, const char *fullname, MonoImageOpenStatus *status)
+{
+       MonoAssembly *res = NULL;
+       gchar *fullpath;
+       GDir *dirhandle;
+       const char* direntry;
+
+       dirhandle = g_dir_open (basepath, 0, NULL);
+       if (!dirhandle)
+               return NULL;
+
+       while ((direntry = g_dir_read_name (dirhandle))) {
+               fullpath = g_build_path (G_DIR_SEPARATOR_S, basepath, direntry, fullname, NULL);
+               res = mono_assembly_open (fullpath, status);
+               g_free (fullpath);
+               if (res)
+                       break;
+       }
+       g_dir_close (dirhandle);
+
+       return res;
+}
+
+MonoAssembly*
+mono_assembly_load_with_partial_name (const char *name, MonoImageOpenStatus *status)
+{
+       MonoAssembly *res;
+       MonoAssemblyName aname;
+       gchar *fullname, *gacpath;
+       gchar **paths;
+
+       memset (&aname, 0, sizeof (MonoAssemblyName));
+       aname.name = name;
+
+       res = mono_assembly_loaded (&aname);
+
+       if (res)
+               return res;
+
+       fullname = g_strdup_printf ("%s.dll", name);
+
+       if (extra_gac_paths) {
+               paths = extra_gac_paths;
+               while (!res && *paths) {
+                       gacpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "gac", name, NULL);
+                       res = probe_for_partial_name (gacpath, fullname, status);
+                       g_free (gacpath);
+                       paths++;
+               }
+       }
+
+       if (res) {
+               res->in_gac = TRUE;
+               g_free (fullname);
+               return res;
+               
+       }
+
+       gacpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (), "mono", "gac", name, NULL);
+       res = probe_for_partial_name (gacpath, fullname, status);
+       g_free (gacpath);
+
+       if (res)
+               res->in_gac = TRUE;
+
+       g_free (fullname);
+
+       return res;
+}
+
 /**
  * mono_assembly_load_from_gac
  *
@@ -745,8 +844,9 @@ static MonoAssembly*
 mono_assembly_load_from_gac (MonoAssemblyName *aname,  gchar *filename, MonoImageOpenStatus *status)
 {
        MonoAssembly *result = NULL;
-       gchar *name, *version, *fullpath;
+       gchar *name, *version, *fullpath, *subpath;
        gint32 len;
+       gchar **paths;
 
        if (!aname->public_tok_value) {
                return NULL;
@@ -765,24 +865,35 @@ mono_assembly_load_from_gac (MonoAssemblyName *aname,  gchar *filename, MonoImag
                        aname->culture == NULL ? "" : aname->culture,
                        aname->public_tok_value);
        
-       fullpath = g_build_path (G_DIR_SEPARATOR_S, MONO_ASSEMBLIES, "mono", "gac",
-                       name, version, filename, NULL);
-       result = mono_assembly_open (fullpath, status);
+       subpath = g_build_path (G_DIR_SEPARATOR_S, name, version, filename, NULL);
+       g_free (name);
+       g_free (version);
 
-       g_free (fullpath);
+       if (extra_gac_paths) {
+               paths = extra_gac_paths;
+               while (!result && *paths) {
+                       fullpath = g_build_path (G_DIR_SEPARATOR_S, *paths, "gac", subpath, NULL);
+                       result = mono_assembly_open (fullpath, status);
+                       g_free (fullpath);
+                       paths++;
+               }
+       }
 
-       if (!result && allow_user_gac) {
-               fullpath = g_build_path (G_DIR_SEPARATOR_S, g_get_home_dir (), ".mono", "gac",
-                               name, version, filename, NULL);
-               result = mono_assembly_open (fullpath, status);
-               g_free (fullpath);
+       if (result) {
+               result->in_gac = TRUE;
+               g_free (subpath);
+               return result;
        }
 
+       fullpath = g_build_path (G_DIR_SEPARATOR_S, mono_assembly_getrootdir (),
+                       "mono", "gac", subpath, NULL);
+       result = mono_assembly_open (fullpath, status);
+       g_free (fullpath);
+
        if (result)
                result->in_gac = TRUE;
        
-       g_free (name);
-       g_free (version);
+       g_free (subpath);
 
        return result;
 }
@@ -943,9 +1054,4 @@ mono_assembly_get_main (void)
        return(main_assembly);
 }
 
-void
-mono_assembly_allow_user_gac (gboolean allow)
-{
-       allow_user_gac = allow;
-}