1 // Copyright (c) 1998-1999 Matra Datavision
2 // Copyright (c) 1999-2014 OPEN CASCADE SAS
4 // This file is part of Open CASCADE Technology software library.
6 // This library is free software; you can redistribute it and/or modify it under
7 // the terms of the GNU Lesser General Public License version 2.1 as published
8 // by the Free Software Foundation, with special exception defined in the file
9 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
10 // distribution for complete text of the license and disclaimer of any warranty.
12 // Alternatively, this file may be used under the terms of Open CASCADE
13 // commercial license or contractual agreement.
18 #include <OSD_Directory.hxx>
19 #include <OSD_File.hxx>
20 #include <OSD_FileNode.hxx>
21 #include <OSD_Protection.hxx>
22 #include <OSD_WhoAmI.hxx>
25 // Ci-joint le tableau de gestion des protection (Ajout et Retrait). Les
26 // tableaux sont des tableaux a deux dimensions, indices par l'enumeration
27 // OSD_SingleProtection. Il y a en tout 16 possibilites dans l enumeration.
28 // Voir JPT pour tous renseignements....
29 static OSD_SingleProtection TabProtAdd [16][16] =
31 {OSD_None,OSD_R,OSD_W,OSD_RW,OSD_X,OSD_RX,OSD_WX,OSD_RWX,OSD_D,OSD_RD,OSD_WD,OSD_RWD,OSD_XD,OSD_RXD,OSD_WXD,OSD_RWXD},
33 {OSD_R,OSD_R,OSD_RW,OSD_RW,OSD_RX,OSD_RX,OSD_RWX,OSD_RWX,OSD_RD,OSD_RD,OSD_RWD,OSD_RWD,OSD_RXD,OSD_RXD,OSD_RWXD,OSD_RWXD},
35 {OSD_W,OSD_RW,OSD_W,OSD_RW,OSD_WX,OSD_RWX,OSD_WX,OSD_RWX,OSD_WD,OSD_RWD,OSD_WD,OSD_RWD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD},
37 {OSD_RW,OSD_RW,OSD_RW,OSD_RW,OSD_RWX,OSD_RWX,OSD_RWX,OSD_RWX,OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD},
39 {OSD_X,OSD_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OSD_RWXD,OSD_RWXD},
55 {OSD_XD,OSD_RXD,OSD_WXD,OSD_RWXD,OSD_XD,OSD_RXD,OSD_WXD,OSD_RWXD,OSD_XD,OSD_RXD,OSD_WXD,OSD_RWXD,OSD_XD,OSD_RXD,OSD_WXD,OSD_RWXD},
57 {OSD_RXD,OSD_RXD,OSD_RWXD,OSD_RWXD,OSD_RXD,OSD_RXD,OSD_RWXD,OSD_RWXD,OSD_RXD,OSD_RXD,OSD_RWXD,OSD_RWXD,OSD_RXD,OSD_RXD,OSD_RWXD,OSD_RWXD},
59 {OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD,OSD_WXD,OSD_RWXD},
61 {OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD,OSD_RWXD}
65 // -----------------------
67 static OSD_SingleProtection TabProtSub [16][16] =
69 {OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None},
71 {OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None},
73 {OSD_W,OSD_W,OSD_None,OSD_None,OSD_W,OSD_W,OSD_None,OSD_None,OSD_W,OSD_W,OSD_None,OSD_None,OSD_W,OSD_W,OSD_None,OSD_None},
75 {OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RW,OSD_W,OSD_R,OSD_None},
77 {OSD_X,OSD_X,OSD_X,OSD_None,OSD_None,OSD_None,OSD_None,OSD_X,OSD_X,OSD_X,OSD_X,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None},
79 {OSD_RX,OSD_X,OSD_RX,OSD_X,OSD_R,OSD_None,OSD_R,OSD_None,OSD_RX,OSD_X,OSD_RX,OSD_X,OSD_R,OSD_None,OSD_R,OSD_None},
81 {OSD_WX,OSD_WX,OSD_X,OSD_X,OSD_W,OSD_W,OSD_None,OSD_None,OSD_WX,OSD_WX,OSD_X,OSD_X,OSD_W,OSD_W,OSD_None,OSD_None},
83 {OSD_RWX,OSD_WX,OSD_RX,OSD_X,OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RWX,OSD_WX,OSD_RX,OSD_X,OSD_RW,OSD_W,OSD_R,OSD_None},
85 {OSD_D,OSD_D,OSD_D,OSD_D,OSD_D,OSD_D,OSD_D,OSD_D,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None,OSD_None},
87 {OSD_RD,OSD_D,OSD_RD,OSD_D,OSD_RD,OSD_D,OSD_RD,OSD_D,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None,OSD_R,OSD_None},
89 {OSD_WD,OSD_WD,OSD_D,OSD_D,OSD_WD,OSD_WD,OSD_D,OSD_D,OSD_W,OSD_W,OSD_None,OSD_None,OSD_W,OSD_W,OSD_None,OSD_None},
91 {OSD_RWD,OSD_WD,OSD_RD,OSD_D,OSD_RWD,OSD_WD,OSD_RD,OSD_D,OSD_RW,OSD_W,OSD_R,OSD_None,OSD_RW,OSD_W,OSD_R,OSD_None},
93 {OSD_XD,OSD_XD,OSD_XD,OSD_XD,OSD_D,OSD_D,OSD_D,OSD_D,OSD_X,OSD_X,OSD_X,OSD_X,OSD_None,OSD_None,OSD_None,OSD_None},
95 {OSD_RXD,OSD_XD,OSD_RXD,OSD_XD,OSD_RD,OSD_D,OSD_RD,OSD_D,OSD_RX,OSD_X,OSD_RX,OSD_X,OSD_R,OSD_None,OSD_R,OSD_None},
97 {OSD_WXD,OSD_WXD,OSD_XD,OSD_XD,OSD_WD,OSD_WD,OSD_D,OSD_D,OSD_WX,OSD_WX,OSD_X,OSD_X,OSD_W,OSD_W,OSD_None,OSD_None},
99 {OSD_RWXD,OSD_WXD,OSD_RXD,OSD_XD,OSD_RWD,OSD_WD,OSD_RD,OSD_D,OSD_RWX,OSD_WX,OSD_RX,OSD_X,OSD_RW,OSD_W,OSD_R,OSD_None}
102 //const OSD_WhoAmI Iam = OSD_WProtection;
105 // Initialize System, Group, World for read only and User for read & write
107 OSD_Protection::OSD_Protection(){
114 OSD_Protection::OSD_Protection(const OSD_SingleProtection System,
115 const OSD_SingleProtection User,
116 const OSD_SingleProtection Group,
117 const OSD_SingleProtection World){
125 void OSD_Protection::Values(OSD_SingleProtection& System,
126 OSD_SingleProtection& User,
127 OSD_SingleProtection& Group,
128 OSD_SingleProtection& World){
136 void OSD_Protection::SetValues(const OSD_SingleProtection System,
137 const OSD_SingleProtection User,
138 const OSD_SingleProtection Group,
139 const OSD_SingleProtection World){
148 void OSD_Protection::SetSystem (const OSD_SingleProtection priv){
152 void OSD_Protection::SetUser (const OSD_SingleProtection priv){
156 void OSD_Protection::SetGroup (const OSD_SingleProtection priv){
160 void OSD_Protection::SetWorld (const OSD_SingleProtection priv){
165 OSD_SingleProtection OSD_Protection::System()const{
169 OSD_SingleProtection OSD_Protection::User()const{
173 OSD_SingleProtection OSD_Protection::Group()const{
177 OSD_SingleProtection OSD_Protection::World()const{
182 void OSD_Protection::Add(OSD_SingleProtection& aProtection,
183 const OSD_SingleProtection aRight){
184 aProtection = TabProtAdd[aProtection][aRight];
188 void OSD_Protection::Sub(OSD_SingleProtection& aProtection,
189 const OSD_SingleProtection aRight){
190 aProtection = TabProtSub[aProtection][aRight];
194 /* Get internal UNIX's access rights for user, group and other */
196 Standard_Integer OSD_Protection::Internal()const{
198 Standard_Integer internal_prot = 0;
200 if (u & OSD_R) internal_prot |= S_IRUSR;
201 if (u & OSD_W) internal_prot |= S_IWUSR;
202 if (u & OSD_D) internal_prot |= S_IWUSR;
203 if (u & OSD_X) internal_prot |= S_IXUSR;
205 if (g & OSD_R) internal_prot |= S_IRGRP;
206 if (g & OSD_W) internal_prot |= S_IWGRP;
207 if (g & OSD_D) internal_prot |= S_IWGRP;
208 if (g & OSD_X) internal_prot |= S_IXGRP;
210 if (w & OSD_R) internal_prot |= S_IROTH;
211 if (w & OSD_W) internal_prot |= S_IWOTH;
212 if (w & OSD_D) internal_prot |= S_IWOTH;
213 if (w & OSD_X) internal_prot |= S_IXOTH;
215 if (s & OSD_R) internal_prot |= S_IROTH;
216 if (s & OSD_W) internal_prot |= S_IWOTH;
217 if (s & OSD_D) internal_prot |= S_IWOTH;
218 if (s & OSD_X) internal_prot |= S_IXOTH;
220 return ( internal_prot );
226 //------------------------------------------------------------------------
227 //------------------- WNT Sources of OSD_Protection ---------------------
228 //------------------------------------------------------------------------
230 #include <OSD_Protection.hxx>
232 #define FLAG_READ 0x00000001
233 #define FLAG_WRITE 0x00000002
234 #define FLAG_EXECUTE 0x00000004
235 #define FLAG_DELETE 0x00000008
237 static Standard_Integer __fastcall _get_mask ( OSD_SingleProtection );
238 static OSD_SingleProtection __fastcall _get_prot ( Standard_Integer );
240 OSD_Protection :: OSD_Protection () {
247 } // end constructor ( 1 )
249 OSD_Protection :: OSD_Protection (
250 const OSD_SingleProtection System,
251 const OSD_SingleProtection User,
252 const OSD_SingleProtection Group,
253 const OSD_SingleProtection World
256 SetValues ( System, User, Group, World );
258 } // end constructor ( 2 )
260 void OSD_Protection :: Values (
261 OSD_SingleProtection& System,
262 OSD_SingleProtection& User,
263 OSD_SingleProtection& Group,
264 OSD_SingleProtection& World
271 } // end OSD_Protection :: Values
273 void OSD_Protection :: SetValues (
274 const OSD_SingleProtection System,
275 const OSD_SingleProtection User,
276 const OSD_SingleProtection Group,
277 const OSD_SingleProtection World
285 } // end OSD_Protection :: SetValues
287 void OSD_Protection :: SetSystem ( const OSD_SingleProtection priv ) {
291 } // end OSD_Protection :: SetSystem
293 void OSD_Protection :: SetUser ( const OSD_SingleProtection priv ) {
297 } // end OSD_Protection :: SetUser
299 void OSD_Protection :: SetGroup ( const OSD_SingleProtection priv ) {
303 } // end OSD_Protection :: SetGroup
305 void OSD_Protection :: SetWorld ( const OSD_SingleProtection priv ) {
309 } // end OSD_Protection :: SetWorld
311 OSD_SingleProtection OSD_Protection :: System () const {
315 } // end OSD_Protection :: System
317 OSD_SingleProtection OSD_Protection :: User () const {
321 } // end OSD_Protection :: User
323 OSD_SingleProtection OSD_Protection :: Group () const {
327 } // end OSD_Protection :: Group
329 OSD_SingleProtection OSD_Protection :: World () const {
333 } // end OSD_Protection :: World
335 void OSD_Protection :: Add (
336 OSD_SingleProtection& aProt,
337 const OSD_SingleProtection aRight
340 Standard_Integer pMask = 0;
341 Standard_Integer rMask = 0;
342 Standard_Integer sMask = 0;
344 pMask = _get_mask ( aProt );
345 rMask = _get_mask ( aRight );
347 if ( ( rMask & FLAG_READ && !( pMask & FLAG_READ ) ) || pMask & FLAG_READ )
351 if ( ( rMask & FLAG_WRITE && !( pMask & FLAG_WRITE ) ) || pMask & FLAG_WRITE )
355 if ( ( rMask & FLAG_EXECUTE && !( pMask & FLAG_EXECUTE ) ) || pMask & FLAG_EXECUTE )
357 sMask |= FLAG_EXECUTE;
359 if ( ( rMask & FLAG_DELETE && !( pMask & FLAG_DELETE ) ) || pMask & FLAG_DELETE )
361 sMask |= FLAG_DELETE;
363 aProt = _get_prot ( sMask );
365 } // end OSD_Protection :: Add
367 void OSD_Protection :: Sub (
368 OSD_SingleProtection& aProt,
369 const OSD_SingleProtection aRight
372 Standard_Integer pMask = 0;
373 Standard_Integer rMask = 0;
375 pMask = _get_mask ( aProt );
376 rMask = _get_mask ( aRight );
378 if ( rMask & FLAG_READ )
382 if ( rMask & FLAG_WRITE )
384 pMask &= ~FLAG_WRITE;
386 if ( rMask & FLAG_EXECUTE )
388 pMask &= ~FLAG_EXECUTE;
390 if ( rMask & FLAG_DELETE )
392 pMask &= ~FLAG_DELETE;
394 aProt = _get_prot ( pMask );
396 } // end OSD_Protection :: Sub
398 Standard_Integer OSD_Protection :: Internal () const {
402 } // end OSD_Protection :: Internal
404 static Standard_Integer __fastcall _get_mask ( OSD_SingleProtection p ) {
406 Standard_Integer retVal = 0;
408 if ( p == OSD_R || p == OSD_RW || p == OSD_RX || p == OSD_RWX ||
409 p == OSD_RXD || p == OSD_RWXD || p == OSD_RD || p == OSD_RWD
410 ) retVal |= FLAG_READ;
412 if ( p == OSD_W || p == OSD_RW || p == OSD_WX || p == OSD_RWX ||
413 p == OSD_WXD || p == OSD_RWXD || p == OSD_WD || p == OSD_RWD
414 ) retVal |= FLAG_WRITE;
416 if ( p == OSD_X || p == OSD_RX || p == OSD_WX || p == OSD_RWX ||
417 p == OSD_XD || p == OSD_RXD || p == OSD_WXD || p == OSD_RWXD
418 ) retVal |= FLAG_EXECUTE;
420 if ( p == OSD_D || p == OSD_RD || p == OSD_WD || p == OSD_RWD ||
421 p == OSD_XD || p == OSD_RXD || p == OSD_WXD || p == OSD_RWXD
422 ) retVal |= FLAG_DELETE;
428 static OSD_SingleProtection __fastcall _get_prot ( Standard_Integer m ) {
430 OSD_SingleProtection retVal;
446 case FLAG_READ | FLAG_WRITE:
458 case FLAG_READ | FLAG_EXECUTE:
464 case FLAG_WRITE | FLAG_EXECUTE:
470 case FLAG_READ | FLAG_WRITE | FLAG_EXECUTE:
482 case FLAG_READ | FLAG_DELETE:
488 case FLAG_WRITE | FLAG_DELETE:
494 case FLAG_READ | FLAG_WRITE | FLAG_DELETE:
500 case FLAG_EXECUTE | FLAG_DELETE:
506 case FLAG_READ | FLAG_EXECUTE | FLAG_DELETE:
512 case FLAG_WRITE | FLAG_EXECUTE | FLAG_DELETE:
518 case FLAG_READ | FLAG_WRITE | FLAG_EXECUTE | FLAG_DELETE: