Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / utility / rw_lock.C
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*
24  * $XConsortium: rw_lock.cc /main/3 1996/06/11 17:38:50 cde-hal $
25  *
26  * Copyright (c) 1993 HAL Computer Systems International, Ltd.
27  * All rights reserved.  Unpublished -- rights reserved under
28  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
29  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
30  * OR DISCLOSURE.
31  * 
32  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
33  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
34  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
35  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
36  * INTERNATIONAL, LTD.
37  * 
38  *                         RESTRICTED RIGHTS LEGEND
39  * Use, duplication, or disclosure by the Government is subject
40  * to the restrictions as set forth in subparagraph (c)(l)(ii)
41  * of the Rights in Technical Data and Computer Software clause
42  * at DFARS 252.227-7013.
43  *
44  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
45  *                  1315 Dell Avenue
46  *                  Campbell, CA  95008
47  * 
48  */
49
50
51 #include "atomic_lock.h"
52 #include "rw_lock.h"
53
54 Boolean read_lock(char* lock_file_path,
55                   char* writing_lock_file_path, 
56                   char* ai_path, 
57                   char* reader_info, int& offset,
58                   char*& ai_info
59                  )
60 {
61    atomic_lock l(lock_file_path);
62
63    if ( l.lock() == false ) {
64       MESSAGE(cerr, "read_lock(): can't do atomic locking");
65       return false;
66    }
67
68    Boolean ok;
69
70    if ( false == exist_file( writing_lock_file_path ) ) {
71
72       fstream x(ai_path, ios::app);
73       if ( !x ) {
74          MESSAGE(cerr, "read_lock(): can't open lock file");
75          throw(streamException(x.rdstate()));
76       }
77
78 #ifdef C_API
79       offset = bytes(x.rdbuf() -> fd());
80 #else
81       offset = bytes(ai_info);
82 #endif
83       x << "A-" << reader_info << "\n";
84
85       x.close();
86       ok = true;
87
88    } else {
89
90       fstream x(ai_path, ios::in);
91       if ( !x ) {
92          MESSAGE(cerr, "read_lock(): can't open lock file");
93          throw(streamException(x.rdstate()));
94       }
95
96 #ifdef C_API
97       int sz = bytes(x.rdbuf() -> fd());
98 #else
99       int sz = bytes(ai_info);
100 #endif
101
102       ai_info = new char[sz+1];
103       ai_info[0] = 0;
104
105       x.getline(ai_info, sz);
106
107       x.close();
108
109       ok = false;
110    }
111
112    if ( l.unlock() == false ) {
113      MESSAGE(cerr, "read_lock(): can't do atomic unlocking");
114      return false;
115    }
116
117    return ok;
118 }
119
120 Boolean read_unlock(char* lock_file_path, char* ai_path, int offset)
121 {
122    atomic_lock l(lock_file_path);
123
124    if ( l.lock() == false ) {
125       MESSAGE(cerr, "read_lock(): can't do atomic locking");
126       return false;
127    }
128
129    Boolean ok ;
130
131    fstream x(ai_path, ios::in|ios::out);
132    if ( !x ) {
133       MESSAGE(cerr, "read_unlock(): can't open lock file");
134       throw(streamException(x.rdstate()));
135    }
136
137    x.seekg( offset, ios::beg );
138    x.put('I');
139
140 ///////////////////////////////////////////////
141 // truncate the info_file if no active readers
142 // and the file size is over 1k
143 ///////////////////////////////////////////////
144
145 #ifdef C_API
146    if ( bytes(x.rdbuf() -> fd()) > 1024 ) {
147 #else
148    if ( bytes(ai_path) > 1024 ) {
149 #endif
150
151       ok = false;
152       char buf[BUFSIZ];
153 /////////////////////////////////////////
154 // scan the info file for active readers
155 /////////////////////////////////////////
156       while ( x.getline(buf, BUFSIZ) ) {
157          if ( buf[0] == 'A' ) {
158             ok = true;
159             break;
160          }
161       }
162
163       if ( ok == false )
164          if ( truncate(ai_path, 0) != 0 ) {
165             MESSAGE(cerr, "read_unlock(): can't truncate");
166             throw(systemException(errno));
167          }
168    }
169
170    x.close();
171
172    if ( l.unlock() == false ) {
173      MESSAGE(cerr, "read_lock(): can't do atomic locking");
174      return false;
175    }
176
177    return true;
178 }
179
180
181 Boolean write_lock(char* lock_file_path, 
182                    char* writing_lock_path, 
183                    char* ai_path, char* writer_info,
184                    char*& ai_info
185                   )
186 {
187    unsigned int len, slen;
188    atomic_lock l(lock_file_path);
189
190    if ( l.lock() == false ) {
191       MESSAGE(cerr, "write_lock(): can't do atomic locking");
192       return false;
193    }
194
195    Boolean ok = true;
196
197    fstream x(ai_path, ios::in|ios::out); 
198
199    if (!x) {
200       MESSAGE(cerr, "write_lock(): can't open info file");
201       throw(streamException(x.rdstate()));
202    }
203
204    char buf[BUFSIZ];
205
206 #ifdef C_API
207    int sz = bytes(x.rdbuf() -> fd());
208 #else
209    int sz = bytes(ai_path);
210 #endif
211    ai_info = new char[sz+1]; 
212    ai_info[0] = 0;
213    
214 /////////////////////////////////////////
215 // scan the info file for active readers
216 /////////////////////////////////////////
217    while ( x.getline(buf, BUFSIZ) ) {
218       if ( buf[0] == 'A' ) {
219          ok = false;
220
221          slen = strlen(ai_info);
222          len = MIN(strlen(buf+1), BUFSIZ - 1 - slen);
223          *((char *) memcpy(ai_info + slen, buf+1, len) + len) = '\0';
224
225          slen = strlen(ai_info);
226          len = MIN(1, BUFSIZ - 1 - slen);
227          *((char *) memcpy(ai_info + slen, "\n", len) + len) = '\0';
228       }
229    }
230    
231    x.close();
232
233    if ( exist_file( writing_lock_path ) == false ) {
234
235       if ( ok == true ) {
236    
237          delete ai_info;
238 /////////////////////////////////////////
239 // create the access info file
240 /////////////////////////////////////////
241          if(truncate(ai_path, 0) != 0 ) {
242             throw(systemException(errno));
243          }
244          fstream x(ai_path, ios::out);
245          x << "A-" << writer_info << "\n";
246    
247 /////////////////////////////////////////
248 // create the writing lock file
249 /////////////////////////////////////////
250          if ( creat(writing_lock_path, 0755) == -1 )
251             ok = false;
252      }
253
254    } else 
255       ok = false;
256
257    if ( l.unlock() == false ) {
258       MESSAGE(cerr, "write_lock(): can't do atomic unlocking");
259       return false;
260    }
261
262    return ok;
263 }
264
265 Boolean write_unlock(char* lock_file_path, char* writing_lock_path, 
266                      char* ai_path 
267                     )
268 {
269    atomic_lock l(lock_file_path);
270
271    if ( l.lock() == false ) {
272       MESSAGE(cerr, "write_unlock(): can't do atomic locking");
273       return false;
274    }
275
276    Boolean ok; 
277
278    if ( del_file(writing_lock_path) == 0 &&
279         del_file(ai_path) == 0 
280       ) 
281       ok = true; 
282    else
283       ok = false; 
284
285    if ( l.unlock() == false ) {
286       MESSAGE(cerr, "write_unlock(): can't do atomic unlocking");
287       return false;
288    }
289
290    return ok;
291 }