| Class | Dsadmin::SystemPath |
| In: |
lib/dsadmin/system_path.rb
|
| Parent: | Object |
Wrapper class around filesystem paths or path patterns (typically specified in the configfile).
| See also: | ../config.html |
| group | [RW] | |
| perm | [RW] | |
| user | [RW] |
Constructor.
| csect: | a ConfigSection to read the path specification from. If set to nil, everything has to be set manually via the accessors |
# File lib/dsadmin/system_path.rb, line 165
165: def initialize(csect = nil)
166: @user = Process.euid
167: @group = Process.egid
168: @perm = 0644
169:
170: if(csect)
171: requireKindOf(ConfigSection, csect)
172:
173: self.rawpath = csect.get("path", String)
174:
175: @user = csect.get('user', String, false) || @user
176: @group = csect.get('group', String, false) || @group
177: permtmp = csect.get('perm', String, false)
178:
179: if(permtmp)
180: assertMatch(/^[0-7]{3,}$/, permtmp)
181: @perm = permtmp.to_i(8)
182: end
183: end
184: end
Delete all files that match the path pattern
This replaces all placeholders not set in aSubstList with "*", passes the result to Dir.glob and tries to delete the resulting files.
| aSubstList: | Placeholder replacement hash as for open etc, but here it may be incomplete. All placeholders not mentioned in it are automatically substituted with "*"!! |
| WARNING: | Be very cautious in using this!! |
| WARNING: | The files are deleted using the user and groups specified in the path config. If the user is "root" and your path is too permissive, you‘re royally screwed! |
# File lib/dsadmin/system_path.rb, line 139
139: def delete(aSubstList = {})
140: requireKindOf(Hash, aSubstList)
141:
142: sl = aSubstList.dup
143: @placeholders.each { |key| sl[key] ||= '*' }
144: lpath = localpath(sl)
145:
146: sys.runAs(user, group) {
147: Dir.glob(lpath) { |fname| File.delete(fname) }
148: }
149: end
Same as path, but with BaseConfigManager#localpath applied
# File lib/dsadmin/system_path.rb, line 73
73: def localpath(aSubstList = {})
74: cfg.localpath(path(aSubstList))
75: end
Wrapper around FileUtils::mkpath
| aParentOnly: | Create the parent directory (e.g. if the file actually points to a file) |
| aSubstList: | See path (or rather localpath, as that‘s what‘s used here) |
# File lib/dsadmin/system_path.rb, line 112
112: def makedirs(aParentOnly = false, aSubstList = {})
113: requireKindOf(Hash, aSubstList)
114:
115: pn = Pathname.new(localpath(aSubstList))
116: pn = pn.parent if(aParentOnly)
117:
118: # If 'read' is allowed, also allow 'search'
119: localperm = perm | ((perm & 0444) >> 2)
120: sys.runAs(user, group) {
121: FileUtils.mkpath(pn.to_s, :mode => localperm)
122: }
123: end
Wrapper around File::open.
| aSubstList: | See path (or rather localpath, as that‘s what‘s used here) |
| aMode: | Mode string or integer, as for the normal File::open |
| aBlock: | Optional, for the block form of File::open |
Warning: If you use the block form, do not place any long-running code in that block. The called File::open (which in turn executes the given block) is wrapped in System#runAs, which blocks the execution of all other threads while it is in effect!
# File lib/dsadmin/system_path.rb, line 88
88: def open(aSubstList = {}, aMode = 'r', &aBlock)
89: requireKindOf(Hash, aSubstList)
90:
91: usedpath = localpath(aSubstList)
92:
93: res = nil
94:
95: sys.runAs(user, group) {
96: if(aBlock)
97: res = File.open(usedpath, aMode, perm, &aBlock)
98: else
99: res = File.open(usedpath, aMode, perm)
100: end
101: }
102:
103: res
104: end
Get the path, with placeholder substitutions applied.
All placeholders have to be present in aSubstList (placeholder name => value). Additional placeholder mappings are silently ignored.
# File lib/dsadmin/system_path.rb, line 57
57: def path(aSubstList = {})
58: requireKindOf(Hash, aSubstList)
59: requireEqual(@placeholders, aSubstList.keys.sort & @placeholders)
60:
61: res = rawpath.dup
62:
63: aSubstList.each { |key, val|
64: key = Regexp.escape(key)
65: res.gsub!(/\{#{key}\}/, val)
66: }
67:
68: res
69: end
# File lib/dsadmin/system_path.rb, line 152
152: def to_s
153: res = "#{rawpath}[#{user}:#{group}:#{sprintf('0%03o', perm)}]"
154: end