~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/core/CollectionStore/.svn/text-base/DataStore.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
 |
3
 
 | Copyright (c) 2007 Novell, Inc.
4
 
 | All Rights Reserved.
5
 
 |
6
 
 | This program is free software; you can redistribute it and/or
7
 
 | modify it under the terms of version 2 of the GNU General Public License as
8
 
 | published by the Free Software Foundation.
9
 
 |
10
 
 | This program is distributed in the hope that it will be useful,
11
 
 | but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 | GNU General Public License for more details.
14
 
 |
15
 
 | You should have received a copy of the GNU General Public License
16
 
 | along with this program; if not, contact Novell, Inc.
17
 
 |
18
 
 | To contact Novell about this file by physical or electronic mail,
19
 
 | you may find current contact information at www.novell.com 
20
 
 |
21
 
 |   Author: Ravi Kumar M <rkumar1@novell.com>
22
 
 |***************************************************************************/
23
 
 
24
 
#if MONO
25
 
using System;
26
 
using System.Collections;
27
 
using System.IO;
28
 
using System.Text;
29
 
using System.Net;
30
 
using System.Security.Cryptography;
31
 
using System.Security.Cryptography.X509Certificates;
32
 
using System.Diagnostics;
33
 
using System.Threading;
34
 
using System.Xml;
35
 
using System.Text;
36
 
using Mono.Unix;
37
 
 
38
 
using Simias;
39
 
using Simias.Client;
40
 
using Simias.Event;
41
 
using Simias.Policy;
42
 
using Simias.Storage.Provider;
43
 
using Simias.Sync;
44
 
using Persist = Simias.Storage.Provider;
45
 
 
46
 
namespace Simias.Storage 
47
 
{
48
 
        /// <summary>
49
 
        /// This is the top level object for the Collection Store.  The Store object can contain multiple
50
 
        /// collection objects.
51
 
        /// </summary>
52
 
        public class DataStore : IComparable
53
 
        {
54
 
                #region class members
55
 
                /// <summary>
56
 
                /// Object used to store free space available on Mount point.
57
 
                /// </summary>
58
 
                public long AvailableFreeSpace;
59
 
 
60
 
                /// <summary>
61
 
                /// Used to log messages.
62
 
                /// </summary>
63
 
                static private readonly ISimiasLog log = SimiasLogManager.GetLogger( typeof( Store ) );
64
 
 
65
 
                /// <summary>
66
 
                /// Object used to store the name of the Unmanaged path.
67
 
                /// </summary>
68
 
                public string DataPath;
69
 
 
70
 
                /// <summary>
71
 
                /// Object used to store the full path of the Unmanaged Path.
72
 
                /// </summary>
73
 
                public string FullPath;
74
 
 
75
 
                /// <summary>
76
 
                /// Object used to store the status of datapaths.
77
 
                /// </summary>
78
 
                public bool Enabled;
79
 
 
80
 
                /// </summary>
81
 
                private const string apacheUser = "wwwrun";
82
 
 
83
 
                /// </summary>
84
 
                private const string apacheGroup = "www";
85
 
 
86
 
 
87
 
 
88
 
                #endregion
89
 
 
90
 
                #region Properties
91
 
                
92
 
                ///<summary>
93
 
                ///Constructor
94
 
                ///</summary>
95
 
                public DataStore(string datapath,string fullpath,string enabled)
96
 
                {
97
 
                        try
98
 
                        {
99
 
                                this.DataPath = datapath;
100
 
                                this.FullPath = fullpath;
101
 
                                string enable = "True";
102
 
                                UnixDriveInfo mntpt = new UnixDriveInfo(this.FullPath);
103
 
                                this.AvailableFreeSpace =  mntpt.AvailableFreeSpace;
104
 
                                if( enabled.Equals( enable ) )
105
 
                                        this.Enabled = true;
106
 
                                else
107
 
                                        this.Enabled = false;
108
 
                        }
109
 
                        catch
110
 
                        {
111
 
                                //Ignore if the Volume is not mounted.
112
 
                        }
113
 
                }
114
 
 
115
 
                ///<summary>
116
 
                ///Constructor
117
 
                ///</summary>
118
 
                public DataStore(string datapath,string fullpath,bool enabled)
119
 
                {
120
 
                        try
121
 
                        {
122
 
                                this.DataPath = datapath;
123
 
                                this.FullPath = fullpath;
124
 
                                UnixDriveInfo mntpt = new UnixDriveInfo(this.FullPath);
125
 
                                this.AvailableFreeSpace =  mntpt.AvailableFreeSpace;
126
 
                                this.Enabled = enabled;
127
 
                        }
128
 
                        catch
129
 
                        {
130
 
                                //Ignore if the Volume is not mounted.
131
 
                        }
132
 
                }
133
 
 
134
 
                
135
 
                ///<summary>
136
 
                ///Constructor
137
 
                ///</summary>
138
 
                public DataStore()
139
 
                {
140
 
                }               
141
 
                
142
 
                ///<summary>
143
 
                ///Constructor
144
 
                ///</summary>
145
 
                public DataStore(string datapath)
146
 
                {
147
 
                        Store store = Store.GetStore();
148
 
                        this.DataPath = datapath;
149
 
                        this.FullPath = Store.StorePath;
150
 
                        UnixDriveInfo mntpt = new UnixDriveInfo(this.FullPath);
151
 
                        this.AvailableFreeSpace =  mntpt.AvailableFreeSpace;
152
 
                        this.Enabled = true;
153
 
                }
154
 
 
155
 
                ///<summary>
156
 
                ///Implementing CompareTo
157
 
                ///</summary>
158
 
                public int CompareTo(Object obj)
159
 
                {
160
 
                        DataStore compare = (DataStore)obj;
161
 
                        int result = this.AvailableFreeSpace.CompareTo(compare.AvailableFreeSpace);
162
 
                        return result;
163
 
                }
164
 
 
165
 
                /// <summary>
166
 
                /// Add a data store for an iFolder Server.
167
 
                /// </summary>
168
 
                /// <returns>Bool true on success.</returns>
169
 
                public int AddStore(string ServerID)
170
 
                {
171
 
                        Store store = Store.GetStore();
172
 
                        Domain domain = store.GetDomain( store.DefaultDomain );
173
 
                        string storepath = Store.StorePath;
174
 
                        string tmppath = Path.Combine(storepath,"SimiasFiles");
175
 
                        tmppath = Path.Combine(tmppath,this.DataPath);
176
 
                        int result = 0;
177
 
                        if( Directory.Exists( tmppath ) == true )
178
 
                                return 1;
179
 
                        else if( Directory.Exists( this.FullPath ) != true )
180
 
                                return 2;
181
 
                        else if( Execute( "chown", "-R {0}:{1} {2}", apacheUser, apacheGroup, this.FullPath ) != 0 )
182
 
                                return 3;
183
 
 
184
 
                        Mono.Posix.Syscall.symlink(this.FullPath,tmppath);
185
 
 
186
 
                        string storageFormat = String.Format( "{0}|{1}", this.DataPath, this.FullPath);
187
 
                        storageFormat = String.Format( "{0}|{1}",storageFormat,this.Enabled.ToString());
188
 
                        HostNode host = HostNode.GetLocalHost();
189
 
                        host.Properties.AddProperty( PropertyTags.DataPath, storageFormat );
190
 
                        domain.Commit(host);
191
 
                        return result;
192
 
                }
193
 
 
194
 
                /// <summary>
195
 
                /// Modify data store for an iFolder Server.
196
 
                /// </summary>
197
 
                /// <param name="name">The name of the data store.</param>
198
 
                /// <returns>Bool true on success.</returns>
199
 
                public bool ModifyStore( string datapath, bool enabled )
200
 
                {
201
 
                        HostNode host = HostNode.GetLocalHost();
202
 
                        MultiValuedList mv = host.Properties.GetProperties( PropertyTags.DataPath );
203
 
 
204
 
                        foreach( Property prop in mv )
205
 
                        {
206
 
                                string[] comps = ( (string) prop.Value ).Split( '|' );
207
 
                                if( (datapath.Equals(comps[0])))
208
 
                                {
209
 
                                        Store store = Store.GetStore();
210
 
                                        Domain domain = store.GetDomain( store.DefaultDomain );
211
 
 
212
 
                                        prop.Delete();
213
 
                                        domain.Commit(host);
214
 
                                        string storageFormat = String.Format( "{0}|{1}", comps[0], comps[1]);
215
 
                                        storageFormat = String.Format( "{0}|{1}", storageFormat, enabled.ToString());
216
 
                                        host.Properties.AddProperty( PropertyTags.DataPath, storageFormat );
217
 
                                        domain.Commit(host);    
218
 
                                }
219
 
                        }
220
 
 
221
 
                        return true;
222
 
                }
223
 
 
224
 
 
225
 
                /// <summary>
226
 
                /// Restrns the storepath based on the load on each volume
227
 
                /// </summary>
228
 
                /// <returns>Storepath</returns>                                
229
 
                public static DataStore[] GetVolumes()
230
 
                {
231
 
                        HostNode host = HostNode.GetLocalHost();
232
 
                        if( host != null )
233
 
                        {
234
 
                                MultiValuedList mv = host.Properties.GetProperties( PropertyTags.DataPath );
235
 
                                ArrayList VolumeList = new ArrayList();
236
 
                                int count = 1;
237
 
                                if( mv != null )
238
 
                                {
239
 
                                        foreach( Property prop in mv )
240
 
                                        {
241
 
                                                VolumeList.Add(prop.Value);
242
 
                                        }
243
 
                                }       
244
 
                                string[] stringArray = new string[VolumeList.Count];
245
 
                                VolumeList.CopyTo(stringArray);
246
 
                                DataStore[] DataStoreArray = new DataStore[VolumeList.Count+1];
247
 
                                DataStoreArray[0] = new DataStore("Default-Store");
248
 
                                foreach(string a in stringArray)
249
 
                                {
250
 
                                        string[] comps = a.Split( '|' );
251
 
                                        DataStoreArray[ count ] = new DataStore(comps[0],comps[1],comps[2]);
252
 
                                        count++;
253
 
                                }
254
 
                                return DataStoreArray;
255
 
                        }
256
 
                        else
257
 
                        {
258
 
                                
259
 
                                DataStore[] DataStoreArray = new DataStore[1];
260
 
                                DataStoreArray[0] = new DataStore("Default-Store");     
261
 
                                return DataStoreArray;
262
 
                        }
263
 
                }
264
 
 
265
 
                /// <summary>
266
 
                /// Execute the command in the shell.
267
 
                /// </summary>
268
 
                /// <param name="command">The command.</param>
269
 
                /// <param name="format">The arguments of the command.</param>
270
 
                /// <param name="args">The arguments for the format.</param>
271
 
                /// <returns>The results of the command.</returns>
272
 
                static int Execute(string command, string format, params object[] args)
273
 
                {
274
 
                        ProcessStartInfo info = new ProcessStartInfo( command, String.Format( format, args ) );                        Process p = Process.Start(info);
275
 
                        p.WaitForExit();
276
 
                        return p.ExitCode;
277
 
                }
278
 
 
279
 
                #endregion
280
 
        }
281
 
                
282
 
}
283
 
#endif