~ubuntu-branches/ubuntu/maverick/backintime/maverick

« back to all changes in this revision

Viewing changes to common/driveinfo.py

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Wiltshire
  • Date: 2009-05-16 23:04:32 UTC
  • mfrom: (1.1.4 upstream)
  • Revision ID: james.westby@ubuntu.com-20090516230432-orrutvtufbtuxsc6
Tags: 0.9.24-1
* New upstream version (closes: #527447):
  - backintime is no longer aware of 'backintime-gnome' and 'backintime-kde4'
    (you need run 'backintime-gnome' for GNOME version and 'backintime-kde4'
    for KDE4 version)
  - fix a bug that crashes the program after taking a snapshot
* Update homepage field in debian/control (closes: #527595)
* Refactor packaging to fit new upstream build system (an almost entire 
  re-write of debian/rules)
* Make configure scripts use /bin/sh instead of /bin/bash (they don't use
  bash features)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#    Back In Time
 
2
#    Copyright (C) 2008-2009 Oprea Dan
 
3
#
 
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.
 
8
#
 
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.
 
13
#
 
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.
 
17
 
 
18
 
 
19
import os.path
 
20
import os
 
21
import configfile
 
22
import sys
 
23
import tools
 
24
 
 
25
 
 
26
class DriveInfo( configfile.ConfigFile ):
 
27
        def __init__( self, path ):
 
28
                configfile.ConfigFile.__init__( self )
 
29
 
 
30
                self.path = path
 
31
                self.load( self._get_driveinfo_file_() )
 
32
 
 
33
                dirty = False
 
34
 
 
35
                if sys.platform == 'win32':
 
36
                        #there is nothing to do
 
37
                        pass
 
38
                else:
 
39
                        if not self.has_value( 'hardlinks' ):
 
40
                                self.set_bool_value( 'hardlinks', self._check_hardlinks_() )
 
41
                                dirty = True
 
42
 
 
43
                        if not self.has_value( 'permissions' ):
 
44
                                self.set_bool_value( 'permissions', self._check_perms_() )
 
45
                                dirty = True
 
46
 
 
47
                        if not self.has_value( 'usergroup' ):
 
48
                                self.set_bool_value( 'usergroup', self._check_usergroup_() )
 
49
                                dirty = True
 
50
 
 
51
                if dirty:
 
52
                        self.save( self._get_driveinfo_file_() )
 
53
 
 
54
        def support_hardlinks( self ):
 
55
                return self.get_bool_value( 'hardlinks', False )
 
56
                
 
57
        def support_permissions( self ):
 
58
                return self.get_bool_value( 'permissions', False )
 
59
                
 
60
        def support_usergroup( self ):
 
61
                return self.get_bool_value( 'usergroup', False )
 
62
                
 
63
        def _get_driveinfo_file_( self ):
 
64
                return os.path.join( self.path, 'driveinfo' )
 
65
 
 
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 ):
 
70
                        return False
 
71
 
 
72
                file1_path = os.path.join( tmp_path, 'file1' )
 
73
                file2_path = os.path.join( tmp_path, 'file2' )
 
74
 
 
75
                ret_val = False
 
76
 
 
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 )
 
80
 
 
81
                if os.path.exists( file1_path ) and os.path.exists( file2_path ):
 
82
                        try:
 
83
                                info1 = os.stat( file1_path )
 
84
                                info2 = os.stat( file2_path )
 
85
 
 
86
                                if info1.st_size == info2.st_size:
 
87
                                        ret_val = True
 
88
                        except:
 
89
                                pass
 
90
 
 
91
                os.system( "rm -rf \"%s\"" % tmp_path )
 
92
                return ret_val
 
93
 
 
94
        def _check_perms_for_file_( self, file_path, mode ):
 
95
                ret_val = False
 
96
 
 
97
                os.system( "chmod %s \"%s\"" % ( mode, file_path ) )
 
98
                try:
 
99
                        info = "%o" % os.stat( file_path ).st_mode
 
100
                        info = info[ -3 : ]
 
101
                        if info == mode:
 
102
                                ret_val = True
 
103
                except:
 
104
                        pass
 
105
 
 
106
                return ret_val
 
107
 
 
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 ):
 
112
                        return False
 
113
 
 
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 ):
 
117
                        return False
 
118
 
 
119
                ret_val = False
 
120
 
 
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' ):
 
127
                                                                ret_val = True
 
128
 
 
129
                os.system( "rm -rf \"%s\"" % tmp_path )
 
130
                return ret_val
 
131
 
 
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 ):
 
136
                        return False
 
137
 
 
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 ):
 
141
                        return False
 
142
 
 
143
                ret_val = False
 
144
 
 
145
                uid = os.getuid()
 
146
                gid = os.getgid()
 
147
 
 
148
                try:
 
149
                        info = os.stat( file_path )
 
150
                        if info.st_uid == uid and info.st_gid == gid:
 
151
                                ret_val = True
 
152
                except:
 
153
                        pass
 
154
 
 
155
                if ret_val and uid == 0:
 
156
                        #try to change the group
 
157
                        import grp
 
158
 
 
159
                        #search for another group
 
160
                        new_gid = gid
 
161
                        new_name = ''
 
162
                        for group in grp.getgrall():
 
163
                                if group.gr_gid != gid:
 
164
                                        new_gid = group.gr_gid
 
165
                                        new_name = group.gr_name
 
166
                                        break
 
167
 
 
168
                        if new_gid != gid:
 
169
                                os.system( "chgrp %s \"%s\"" % ( new_name, file_path ) )
 
170
                                try:
 
171
                                        info = os.stat( file_path )
 
172
                                        if info.st_gid != new_gid:
 
173
                                                ret_val = False
 
174
                                except:
 
175
                                        ret_val = False
 
176
 
 
177
                os.system( "rm -rf \"%s\"" % tmp_path )
 
178
                return ret_val
 
179