2
# Copyright (C) 2008-2009 Oprea Dan
4
# This program is free software; you can redistribute it and/or modify
5
# it under the terms of the GNU General Public License as published by
6
# the Free Software Foundation; either version 2 of the License, or
7
# (at your option) any later version.
9
# This program is distributed in the hope that it will be useful,
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
# GNU General Public License for more details.
14
# You should have received a copy of the GNU General Public License along
15
# with this program; if not, write to the Free Software Foundation, Inc.,
16
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26
class DriveInfo( configfile.ConfigFile ):
27
def __init__( self, path ):
28
configfile.ConfigFile.__init__( self )
31
self.load( self._get_driveinfo_file_() )
35
if sys.platform == 'win32':
36
#there is nothing to do
39
if not self.has_value( 'hardlinks' ):
40
self.set_bool_value( 'hardlinks', self._check_hardlinks_() )
43
if not self.has_value( 'permissions' ):
44
self.set_bool_value( 'permissions', self._check_perms_() )
47
if not self.has_value( 'usergroup' ):
48
self.set_bool_value( 'usergroup', self._check_usergroup_() )
52
self.save( self._get_driveinfo_file_() )
54
def support_hardlinks( self ):
55
return self.get_bool_value( 'hardlinks', False )
57
def support_permissions( self ):
58
return self.get_bool_value( 'permissions', False )
60
def support_usergroup( self ):
61
return self.get_bool_value( 'usergroup', False )
63
def _get_driveinfo_file_( self ):
64
return os.path.join( self.path, 'driveinfo' )
66
def _check_hardlinks_( self ):
67
tmp_path = os.path.join( self.path, 'driveinfo.tmp' )
68
tools.make_dirs( tmp_path )
69
if not os.path.isdir( tmp_path ):
72
file1_path = os.path.join( tmp_path, 'file1' )
73
file2_path = os.path.join( tmp_path, 'file2' )
77
os.system( "echo abc > \"%s\"" % file1_path )
78
os.system( "ln \"%s\" \"%s\"" % ( file1_path, file2_path ) )
79
os.system( "echo abc > \"%s\"" % file2_path )
81
if os.path.exists( file1_path ) and os.path.exists( file2_path ):
83
info1 = os.stat( file1_path )
84
info2 = os.stat( file2_path )
86
if info1.st_size == info2.st_size:
91
os.system( "rm -rf \"%s\"" % tmp_path )
94
def _check_perms_for_file_( self, file_path, mode ):
97
os.system( "chmod %s \"%s\"" % ( mode, file_path ) )
99
info = "%o" % os.stat( file_path ).st_mode
108
def _check_perms_( self ):
109
tmp_path = os.path.join( self.path, 'driveinfo.tmp' )
110
tools.make_dirs( tmp_path )
111
if not os.path.isdir( tmp_path ):
114
file_path = os.path.join( tmp_path, 'file' )
115
os.system( "echo abc > \"%s\"" % file_path )
116
if not os.path.isfile( file_path ):
121
if self._check_perms_for_file_( file_path, '111' ):
122
if self._check_perms_for_file_( file_path, '700' ):
123
if self._check_perms_for_file_( file_path, '600' ):
124
if self._check_perms_for_file_( file_path, '711' ):
125
if self._check_perms_for_file_( file_path, '300' ):
126
if self._check_perms_for_file_( file_path, '666' ):
129
os.system( "rm -rf \"%s\"" % tmp_path )
132
def _check_usergroup_( self ):
133
tmp_path = os.path.join( self.path, 'driveinfo.tmp' )
134
tools.make_dirs( tmp_path )
135
if not os.path.isdir( tmp_path ):
138
file_path = os.path.join( tmp_path, 'file' )
139
os.system( "echo abc > \"%s\"" % file_path )
140
if not os.path.isfile( file_path ):
149
info = os.stat( file_path )
150
if info.st_uid == uid and info.st_gid == gid:
155
if ret_val and uid == 0:
156
#try to change the group
159
#search for another group
162
for group in grp.getgrall():
163
if group.gr_gid != gid:
164
new_gid = group.gr_gid
165
new_name = group.gr_name
169
os.system( "chgrp %s \"%s\"" % ( new_name, file_path ) )
171
info = os.stat( file_path )
172
if info.st_gid != new_gid:
177
os.system( "rm -rf \"%s\"" % tmp_path )