1 /*-
2  * Copyright (c) 2009, 2020 Oracle and/or its affiliates.  All rights reserved.
3  *
4  * See the file EXAMPLES-LICENSE for license information.
5  *
6  */
7 using System;
8 using System.IO;
9 using System.Collections.Generic;
10 using System.Collections;
11 using System.Diagnostics;
12 using System.Text;
13 using System.Runtime.Serialization.Formatters.Binary;
14 using BerkeleyDB;
15 
16 namespace excs_env {
17     class Program {
18         const int EXIT_FAILURE = 1;
19         const int EXIT_SUCCESS = 0;
20         const string progName = "excs_env";
Main(string[] args)21         static void Main(string[] args) {
22             string data_dir, home;
23 
24             /*
25              * excs_env is meant to be run from build_windows\AnyCPU, in either
26              * the Debug or Release directory. The required core libraries,
27              * however, are in either build_windows\Win32 or build_windows\x64,
28              * depending upon the platform.  That location needs to be added to
29              * the PATH environment variable for the P/Invoke calls to work.
30              */
31             try {
32                 String pwd = Environment.CurrentDirectory;
33                 pwd = Path.Combine(pwd, "..");
34                 pwd = Path.Combine(pwd, "..");
35                 if (IntPtr.Size == 4)
36                     pwd = Path.Combine(pwd, "Win32");
37                 else
38                     pwd = Path.Combine(pwd, "x64");
39 #if DEBUG
40                 pwd = Path.Combine(pwd, "Debug");
41 #else
42                 pwd = Path.Combine(pwd, "Release");
43 #endif
44                 pwd += ";" + Environment.GetEnvironmentVariable("PATH");
45                 Environment.SetEnvironmentVariable("PATH", pwd);
46             } catch (Exception e) {
47                 Console.WriteLine(
48                     "Unable to set the PATH environment variable.");
49                 Console.WriteLine(e.Message);
50                 return;
51             }
52 
53             data_dir = home = null;
54             switch (args.Length) {
55                 case 0:
56                     data_dir = "envData";
57                     home = "envHome";
58                     break;
59                 case 2:
60                     home = args[0];
61                     data_dir = args[1];
62                     break;
63                 case 1:
64                     home = args[0];
65                     break;
66                 default:
67                     usage();
68                     return;
69             }
70 
71             if (!Directory.Exists(home)) {
72                 Console.WriteLine("Creating home directory: {0}", home);
73                 try {
74                     Directory.CreateDirectory(home);
75                 } catch {
76                     Console.WriteLine("Unable to create {0}", home);
77                     return;
78                 }
79             }
80             if (data_dir != null && !Directory.Exists(home + "/" + data_dir)) {
81                 Console.WriteLine(
82                     "Creating home directory: {0}/{1}", home, data_dir);
83                 try {
84                     Directory.CreateDirectory(home + "/" + data_dir);
85                 } catch {
86                     Console.WriteLine(
87                         "Unable to create {0}/{1}", home, data_dir);
88                     return;
89                 }
90             }
91 
92             /* Set up environment. */
93             if (SetUpEnv(home, data_dir) == EXIT_FAILURE) {
94                 Console.WriteLine("Fail to set up the environment.");
95                 return;
96             }
97             Console.WriteLine("Set up the environment.");
98 
99             /* Tear down the environment and remove its files. */
100             if (TearDownEnv(home) == EXIT_FAILURE) {
101                 Console.WriteLine(
102                     "Fail to tear down the environment.");
103                 return;
104             }
105             Console.WriteLine("Tear down the environment.");
106         }
107 
108         /*
109          * Set up environment.
110          */
SetUpEnv(string home, string data_dir)111         public static int SetUpEnv(string home, string data_dir) {
112             DatabaseEnvironment env;
113             DatabaseEnvironmentConfig envConfig;
114 
115             /* Configure an environment. */
116             envConfig = new DatabaseEnvironmentConfig();
117             envConfig.MPoolSystemCfg = new MPoolConfig();
118             envConfig.MPoolSystemCfg.CacheSize = new CacheInfo(
119                 0, 64 * 1024, 1);
120             envConfig.Create = true;
121             envConfig.DataDirs.Add(data_dir);
122             envConfig.CreationDir = data_dir;
123             envConfig.ErrorPrefix = progName;
124             envConfig.UseLogging = true;
125             envConfig.UseLocking = true;
126             envConfig.UseMPool = true;
127             envConfig.UseTxns = true;
128 
129             /* Create and open the environment. */
130             try {
131                 env = DatabaseEnvironment.Open(home, envConfig);
132             } catch (Exception e) {
133                 Console.WriteLine("{0}", e.Message);
134                 return EXIT_FAILURE;
135             }
136 
137             Console.ReadLine();
138             env.Close();
139             return EXIT_SUCCESS;
140         }
141 
142         /*
143          * Tear down environment and remove its files.
144          * Any log or database files and the environment
145          * directory are not removed.
146          */
TearDownEnv(string home)147         public static int TearDownEnv(string home) {
148             /* Remove environment regions. */
149             try {
150                 DatabaseEnvironment.Remove(home);
151             } catch (Exception e) {
152                 Console.WriteLine("{0}: {1}\n{2}",
153                     e.Source, e.Message, e.StackTrace);
154                 return EXIT_FAILURE;
155             }
156 
157             return EXIT_SUCCESS;
158         }
159 
usage()160         public static void usage() {
161             Console.WriteLine("Usage: excs_env [home] [data dir]");
162         }
163     }
164 }