System.Data.SQLite

Check-in [c62df132f4]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:More work on the native library pre-loader for POSIX.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | netStandard20
Files: files | file ages | folders
SHA1: c62df132f4ee0d37dca69b1f271630e7ea5111ad
User & Date: mistachkin 2018-04-07 13:30:50.795
Context
2018-04-07
14:40
Enhancements for the native library pre-loader for .NET Core. check-in: c37b617ed1 user: mistachkin tags: netStandard20
13:30
More work on the native library pre-loader for POSIX. check-in: c62df132f4 user: mistachkin tags: netStandard20
10:59
Another experimental change to the native library pre-loader for POSIX. check-in: cedca9ac3f user: mistachkin tags: netStandard20
Changes
Unified Diff Ignore Whitespace Patch
Changes to System.Data.SQLite/UnsafeNativeMethods.cs.
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
  #region Native Library Helper Class
  /// <summary>
  /// This static class provides a thin wrapper around the native library
  /// loading features of the underlying platform.
  /// </summary>
  internal static class NativeLibraryHelper
  {
      #region Private Constants
#if !PLATFORM_COMPACTFRAMEWORK
      /// <summary>
      /// The list of characters used as path delimiters for this platform.
      /// </summary>
      private static readonly char[] PathSeparators = { Path.PathSeparator };

      /////////////////////////////////////////////////////////////////////////

      /// <summary>
      /// This is the name of the environment variable containing the POSIX
      /// shared library search path.  The value is assumed to be a list of
      /// directories, delimited by <see cref="Path.PathSeparator" />.
      /// </summary>
      private static readonly string LdLibraryPath = "LD_LIBRARY_PATH";
#endif
      #endregion

      /////////////////////////////////////////////////////////////////////////

      #region Private Delegates
      /// <summary>
      /// This delegate is used to wrap the concept of loading a native
      /// library, based on a file name, and returning the loaded module
      /// handle.
      /// </summary>
      /// <param name="fileName">







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







834
835
836
837
838
839
840




















841
842
843
844
845
846
847
  #region Native Library Helper Class
  /// <summary>
  /// This static class provides a thin wrapper around the native library
  /// loading features of the underlying platform.
  /// </summary>
  internal static class NativeLibraryHelper
  {




















      #region Private Delegates
      /// <summary>
      /// This delegate is used to wrap the concept of loading a native
      /// library, based on a file name, and returning the loaded module
      /// handle.
      /// </summary>
      /// <param name="fileName">
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
      {
          return UnsafeNativeMethodsWin32.LoadLibrary(fileName);
      }

      /////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK
      /// <summary>
      /// Attempts to determine if a directory exists in a search path.
      /// </summary>
      /// <param name="directory">
      /// The directory to search for.  This is case-sensitive.
      /// </param>
      /// <param name="path">
      /// The search path to inspect.  This is case-sensitive.
      /// </param>
      /// <returns>
      /// Non-zero if the directory exists in the search path; otherwise,
      /// zero.
      /// </returns>
      private static bool IsDirectoryInPath(
          string directory,
          string path
          )
      {
          //
          // NOTE: Nothing meaningful can be done if either of the parameters
          //       are null (or an empty string).  Therefore, return false in
          //       that case.
          //
          if (String.IsNullOrEmpty(directory) || String.IsNullOrEmpty(path))
              return false;

          string[] pathDirectories = path.Split(
              PathSeparators, StringSplitOptions.RemoveEmptyEntries);

          //
          // NOTE: No search can be performed if the list of path directories
          //       is invalid (or empty).  Therefore, just return false.
          //
          if ((pathDirectories == null) || (pathDirectories.Length == 0))
              return false;

          foreach (string pathDirectory in pathDirectories)
          {
              if (String.Equals(
                    pathDirectory, directory, StringComparison.Ordinal))
              {
                  return true;
              }
          }

          return false;
      }

      /////////////////////////////////////////////////////////////////////////

      /// <summary>
      /// Ensures that the directory containing the specified file is included
      /// in the POSIX shared library search path via the environment variable
      /// LD_LIBRARY_PATH.
      /// </summary>
      /// <param name="fileName">
      /// The file name of the native library that was successfully loaded.
      /// </param>
      /// <returns>
      /// Non-zero if the POSIX shared library path was updated; otherwise,
      /// zero.
      /// </returns>
      private static bool MaybeUpdateLdLibraryPath(
          string fileName
          )
      {
          string directory = Path.GetDirectoryName(fileName);

          if (!String.IsNullOrEmpty(directory))
          {
              string path = Environment.GetEnvironmentVariable(LdLibraryPath);

              if (!IsDirectoryInPath(directory, path))
              {
                  if (!String.IsNullOrEmpty(path))
                  {
                      path = HelperMethods.StringFormat(
                          CultureInfo.InvariantCulture, "{0}{1}{2}",
                          directory, Path.PathSeparator, path);
                  }
                  else
                  {
                      path = directory;
                  }

                  Environment.SetEnvironmentVariable(LdLibraryPath, path);
                  return true;
              }
          }

          return false;
      }

      /////////////////////////////////////////////////////////////////////////

      /// <summary>
      /// Attempts to load the specified native library file using the POSIX
      /// API.
      /// </summary>
      /// <param name="fileName">
      /// The file name of the native library to load.
      /// </param>
      /// <returns>
      /// The native module handle upon success -OR- IntPtr.Zero on failure.
      /// </returns>
      private static IntPtr LoadLibraryPosix(
          string fileName
          )
      {
          IntPtr nativeModuleHandle = UnsafeNativeMethodsPosix.dlopen(
              fileName, UnsafeNativeMethodsPosix.RTLD_DEFAULT);

          if (nativeModuleHandle != IntPtr.Zero)
          {
              if (MaybeUpdateLdLibraryPath(fileName))
              {
                  UnsafeNativeMethodsPosix.dlclose(nativeModuleHandle);
                  nativeModuleHandle = IntPtr.Zero;
              }
          }

          return nativeModuleHandle;
      }
#endif
      #endregion

      /////////////////////////////////////////////////////////////////////////

      #region Public Methods







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<














|

<
<
<
<
<
<
<
<
<
<
<







874
875
876
877
878
879
880































































































881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896











897
898
899
900
901
902
903
      {
          return UnsafeNativeMethodsWin32.LoadLibrary(fileName);
      }

      /////////////////////////////////////////////////////////////////////////

#if !PLATFORM_COMPACTFRAMEWORK































































































      /// <summary>
      /// Attempts to load the specified native library file using the POSIX
      /// API.
      /// </summary>
      /// <param name="fileName">
      /// The file name of the native library to load.
      /// </param>
      /// <returns>
      /// The native module handle upon success -OR- IntPtr.Zero on failure.
      /// </returns>
      private static IntPtr LoadLibraryPosix(
          string fileName
          )
      {
          return UnsafeNativeMethodsPosix.dlopen(
              fileName, UnsafeNativeMethodsPosix.RTLD_DEFAULT);











      }
#endif
      #endregion

      /////////////////////////////////////////////////////////////////////////

      #region Public Methods