blib/lib/Gtk2/Chmod.pm

filter: Statementstmt 77.7% 7 / 9 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 3 / 3 Podpod n/a totaltotal 83.3% 10 / 12 CC 1 SCAR 0.9
CC
1
Cov
78%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
↑ Summary
1 package Gtk2::Chmod;
2  
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 0 ▶use Gtk2;
Subroutine BEGIN: called CC=1 SCAR=2.6
6 use File::Stat::Bits;
7  
8 =head1 NAME
9  
10 Gtk2::Chmod - Provides a dialog for getting values to use with chmod.
11  
12 =head1 VERSION
13  
14 Version 0.0.0
15  
16 =cut
17  
18 our $VERSION = '0.0.0';
19  
20  
21 =head1 SYNOPSIS
22  
23 use Gtk2;
24 use Gtk2::Chmod;
25
26 Gtk2->init;
27
28 my %returned=Gtk2::Chmod->ask('/etc/passwd');
29 if($returned{error}){
30 print "Error!\n".$returned{errorString}."\n";
31 }else{
32 use Data::Dumper;
33 print Dumper(\%returned);
34 }
35  
36 =head1 METHODS
37  
38 =head2 ask
39  
40 This creates a dialog that provides a dialog for getting
41 what mode should be used for a file/directory when doing
42 chmod.
43  
44 The data is returned as a hash
45  
46 The initial settings are based off of the file/direcotry specified.
47  
48 my %returned=Gtk2::Chmod->ask($item);
49 if($returned{error}){
50 print "Error!\n".$returned{errorString}."\n";
51 }else{
52 use Data::Dumper;
53 print Dumper(\%returned);
54  
55 if($returned{pressed} eq 'ok'){
56 if(-d $item){
57 chmod($returned{dirmode}, $item);
58 }else{
59 chmod($returned{filemode}, $item);
60 }
61 }
62  
63 }
64  
65 =cut
66  
67 sub ask{
68 my $item=$_[1];
69  
70 my $self={error=>undef, errorString=>''};
71 bless($self);
72  
73 #the return value
74 my %toreturn;
75 $toreturn{mode}='0000';
76 $toreturn{error}=undef;
77 $toreturn{errorString}='';
78  
79 if (!-e $item) {
80 $toreturn{error}=1;
81 $toreturn{errorString}='The file or directory does not exist.';
82 warn('Gtk2-Chmod ask:'.$toreturn{error}.':'.$toreturn{errorString});
83 return %toreturn;
84 }
85  
86 #we will be using the mode value later
87 my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($item);
88
89 #the dialog window
90 $self->{window}=Gtk2::Dialog->new('chmod: '.$item,
91 undef,
92 [qw/modal destroy-with-parent/],
93 'gtk-cancel' => 'cancel',
94 'gtk-ok' => 'ok',
95 );
96
97 #
98 $self->{window}->set_position('center-always');
99  
100 $self->{window}->set_response_sensitive ('accept', 0);
101 $self->{window}->set_response_sensitive ('reject', 0);
102
103 $self->{vbox} = $self->{window}->vbox;
104 $self->{vbox}->set_border_width(5);
105  
106 #the boxes to hold the permissions
107 $self->{permhbox}=Gtk2::HBox->new;
108 $self->{permhbox}->show;
109 $self->{uvbox}=Gtk2::VBox->new;
110 $self->{uvbox}->show;
111 $self->{permhbox}->pack_start($self->{uvbox}, 1, 1, 0);
112 $self->{vs0}=Gtk2::VSeparator->new;
113 $self->{vs0}->show;
114 $self->{permhbox}->pack_start($self->{vs0}, 1, 1, 0);
115 $self->{gvbox}=Gtk2::VBox->new;
116 $self->{gvbox}->show;
117 $self->{permhbox}->pack_start($self->{gvbox}, 1, 1, 0);
118 $self->{vs1}=Gtk2::VSeparator->new;
119 $self->{vs1}->show;
120 $self->{permhbox}->pack_start($self->{vs1}, 1, 1, 0);
121 $self->{ovbox}=Gtk2::VBox->new;
122 $self->{ovbox}->show;
123 $self->{permhbox}->pack_start($self->{ovbox}, 1, 1, 0);
124 $self->{vbox}->pack_start($self->{permhbox}, 1, 1, 0);
125  
126 #user perms
127 $self->{user}=Gtk2::Label->new('user');
128 $self->{user}->show;
129 $self->{uvbox}->pack_start($self->{user}, 1, 1, 1);
130 $self->{ur}=Gtk2::CheckButton->new('read');
131 $self->{ur}->show;
132 $self->{uvbox}->pack_start($self->{ur}, 1, 1, 1);
133 $self->{uw}=Gtk2::CheckButton->new('write');
134 $self->{uw}->show;
135 $self->{uvbox}->pack_start($self->{uw}, 1, 1, 1);
136 $self->{ux}=Gtk2::CheckButton->new('exec/list');
137 $self->{ux}->show;
138 $self->{uvbox}->pack_start($self->{ux}, 1, 1, 1);
139 $self->{suid}=Gtk2::CheckButton->new('suid');
140 $self->{suid}->show;
141 $self->{uvbox}->pack_start($self->{suid}, 1, 1, 1);
142  
143 #group perms
144 $self->{group}=Gtk2::Label->new('group');
145 $self->{group}->show;
146 $self->{gvbox}->pack_start($self->{group}, 1, 1, 1);
147 $self->{gr}=Gtk2::CheckButton->new('read');
148 $self->{gr}->show;
149 $self->{gvbox}->pack_start($self->{gr}, 1, 1, 1);
150 $self->{gw}=Gtk2::CheckButton->new('write');
151 $self->{gw}->show;
152 $self->{gvbox}->pack_start($self->{gw}, 1, 1, 1);
153 $self->{gx}=Gtk2::CheckButton->new('exec/list');
154 $self->{gx}->show;
155 $self->{gvbox}->pack_start($self->{gx}, 1, 1, 1);
156 $self->{sgid}=Gtk2::CheckButton->new('sgid');
157 $self->{sgid}->show;
158 $self->{gvbox}->pack_start($self->{sgid}, 1, 1, 1);
159  
160 #other perms
161 $self->{other}=Gtk2::Label->new('other');
162 $self->{other}->show;
163 $self->{ovbox}->pack_start($self->{other}, 1, 1, 1);
164 $self->{or}=Gtk2::CheckButton->new('read');
165 $self->{or}->show;
166 $self->{ovbox}->pack_start($self->{or}, 1, 1, 1);
167 $self->{ow}=Gtk2::CheckButton->new('write');
168 $self->{ow}->show;
169 $self->{ovbox}->pack_start($self->{ow}, 1, 1, 1);
170 $self->{ox}=Gtk2::CheckButton->new('exec/list');
171 $self->{ox}->show;
172 $self->{ovbox}->pack_start($self->{ox}, 1, 1, 1);
173  
174 #split the perm area from the other stuff
175 $self->{hs0}=Gtk2::HSeparator->new;
176 $self->{hs0}->show;
177 $self->{vbox}->pack_start($self->{hs0}, 1, 1, 1);
178  
179 #recursive
180 $self->{recursive}=Gtk2::CheckButton->new('Recursive?');
181 $self->{recursive}->show;
182 $self->{vbox}->pack_start($self->{recursive}, 1, 1, 1);
183  
184 #split the perm area from the other stuff
185 $self->{hs1}=Gtk2::HSeparator->new;
186 $self->{hs1}->show;
187 $self->{vbox}->pack_start($self->{hs1}, 1, 1, 1);
188  
189 #dironly
190 $self->{dironlyLabel}=Gtk2::Label->new('Set exec/search bit on directories only?');
191 $self->{dironlyLabel}->show;
192 $self->{dironlyhbox}=Gtk2::HBox->new;
193 $self->{dironlyhbox}->show;
194 $self->{vbox}->pack_start($self->{dironlyLabel}, 1, 1, 1);
195 $self->{vbox}->pack_start($self->{dironlyhbox}, 1, 1, 1);
196 #user
197 $self->{udironly}=Gtk2::CheckButton->new('user');
198 $self->{udironly}->show;
199 $self->{dironlyhbox}->pack_start($self->{udironly}, 1, 1, 1);
200 #group
201 $self->{gdironly}=Gtk2::CheckButton->new('group');
202 $self->{gdironly}->show;
203 $self->{dironlyhbox}->pack_start($self->{gdironly}, 1, 1, 1);
204 #other
205 $self->{odironly}=Gtk2::CheckButton->new('other');
206 $self->{odironly}->show;
207 $self->{dironlyhbox}->pack_start($self->{odironly}, 1, 1, 1);
208  
209 $self->{window}->signal_connect(response => sub {
210 $toreturn{pressed}=$_[1];
211 #user
212 $toreturn{ur}=$self->{ur}->get_active;
213 $toreturn{ux}=$self->{ux}->get_active;
214 $toreturn{uw}=$self->{uw}->get_active;
215 $toreturn{suid}=$self->{suid}->get_active;
216 #group
217 $toreturn{gr}=$self->{gr}->get_active;
218 $toreturn{gx}=$self->{gx}->get_active;
219 $toreturn{gw}=$self->{gw}->get_active;
220 $toreturn{sgid}=$self->{sgid}->get_active;
221 #other
222 $toreturn{or}=$self->{or}->get_active;
223 $toreturn{ox}=$self->{ox}->get_active;
224 $toreturn{ow}=$self->{ow}->get_active;
225 #misc
226 $toreturn{recursive}=$self->{recursive}->get_active;
227 $toreturn{udironly}=$self->{udironly}->get_active;
228 $toreturn{gdironly}=$self->{gdironly}->get_active;
229 $toreturn{odironly}=$self->{odironly}->get_active;
230 }
231 );
232  
233 #set the switches properly
234 #user read
235 if (S_IRUSR & $mode) {
236 $self->{ur}->set_active(1);
237 }
238 #user write
239 if (S_IWUSR & $mode) {
240 $self->{uw}->set_active(1);
241 }
242 #user suid
243 if (S_ISUID & $mode) {
244 $self->{suid}->set_active(1);
245 }
246 #user exec
247 if (S_IXUSR & $mode) {
248 $self->{ux}->set_active(1);
249 }
250 #group
251 #group read
252 if (S_IRGRP & $mode) {
253 $self->{gr}->set_active(1);
254 }
255 #group write
256 if (S_IWGRP & $mode) {
257 $self->{gw}->set_active(1);
258 }
259 #group sgid
260 if (S_ISGID & $mode) {
261 $self->{sguid}->set_active(1);
262 }
263 #group exec
264 if (S_IXGRP & $mode) {
265 $self->{gx}->set_active(1);
266 }
267 #other
268 #other read
269 if (S_IROTH & $mode) {
270 $self->{or}->set_active(1);
271 }
272 #other write
273 if (S_IWOTH & $mode) {
274 $self->{ow}->set_active(1);
275 }
276 #other exec
277 if (S_IXOTH & $mode) {
278 $self->{ox}->set_active(1);
279 }
280  
281 #run what has been setup
282 my $response=$self->{window}->run;
283  
284 #sets up the mode
285 #user read
286 if ($toreturn{ur}) {
287 $toreturn{mode}=$toreturn{mode} + 400;
288 }
289 #user write
290 if ($toreturn{uw}) {
291 $toreturn{mode}=$toreturn{mode} + 200;
292 }
293 #user exec/search
294 if ($toreturn{ux}) {
295 $toreturn{mode}=$toreturn{mode} + 100;
296 }
297 #group read
298 if ($toreturn{gr}) {
299 $toreturn{mode}=$toreturn{mode} + 40;
300 }
301 #group write
302 if ($toreturn{gw}) {
303 $toreturn{mode}=$toreturn{mode} + 20;
304 }
305 #group exec/search
306 if ($toreturn{gx}) {
307 $toreturn{mode}=$toreturn{mode} + 10;
308 }
309 #other read
310 if ($toreturn{or}) {
311 $toreturn{mode}=$toreturn{mode} + 4;
312 }
313 #other write
314 if ($toreturn{ow}) {
315 $toreturn{mode}=$toreturn{mode} + 2;
316 }
317 #other exec/search
318 if ($toreturn{ox}) {
319 $toreturn{mode}=$toreturn{mode} + 1;
320 }
321 #setuid
322 if ($toreturn{suid}) {
323 $toreturn{mode}=$toreturn{mode} + 4000;
324 }
325 #setgid
326 if ($toreturn{sgid}) {
327 $toreturn{mode}=$toreturn{mode} + 2000;
328 }
329  
330 #if we are to set dir only mode for any thing, update the file stuff
331 $toreturn{filemode}=$toreturn{mode};
332 if ($toreturn{ux} && $toreturn{udironly}) {
333 $toreturn{filemode}=$toreturn{filemode} - 100;
334 }
335 if ($toreturn{gx} && $toreturn{gdironly}) {
336 $toreturn{filemode}=$toreturn{filemode} - 10;
337 }
338 if ($toreturn{ox} && $toreturn{odironly}) {
339 $toreturn{filemode}=$toreturn{filemode} - 1;
340 }
341  
342 #mode
343 if (length($toreturn{mode}) < 4) {
344 my $toadd=4 - length($toreturn{mode});
345 $toadd--;
346 my $int=0;
347 while ($int <= $toadd ) {
348 $toreturn{mode}='0'.$toreturn{mode};
349  
350 $int++;
351 }
352 $toreturn{dirmode}=$toreturn{mode};
353 }
354 #file mode
355 if (length($toreturn{filemode}) < 4) {
356 my $toadd=4 - length($toreturn{filemode});
357 $toadd--;
358 my $int=0;
359 while ($int <= $toadd ) {
360 $toreturn{filemode}='0'.$toreturn{filemode};
361  
362 $int++;
363 }
364 }
365  
366 $self->{window}->destroy;
367  
368 return %toreturn;
369 }
370  
371 =head1 RETURNED HASH
372  
373 =head2 error
374  
375 This is defined if there is a error and the integer is the
376 error code.
377  
378 =head2 errorString
379  
380 This provides a desciption of the current error.
381  
382 =head2 pressed
383  
384 If this is set to 'ok' the user has accepted the values.
385  
386 =head2 ur
387  
388 Set to '1' if the user has read permissions.
389  
390 =head2 uw
391  
392 Set to '1' if the user has write permissions.
393  
394 =head2 ux
395  
396 Set to '1' if the user has execute/search permissions.
397  
398 =head2 suid
399  
400 Set to '1' if the user has suid permissions.
401  
402 =head2 gr
403  
404 Set to '1' if the group has read permissions.
405  
406 =head2 gw
407  
408 Set to '1' if the group has write permissions.
409  
410 =head2 sgid
411  
412 Set to '1' if the group has sgid permissions.
413  
414 =head2 ox
415  
416 Set to '1' if the group has group permissions.
417  
418 =head2 or
419  
420 Set to '1' if the other has read permissions.
421  
422 =head2 ow
423  
424 Set to '1' if the other has write permissions.
425  
426 =head2 ox
427  
428 Set to '1' if the other has group permissions.
429  
430 =head1 ERROR CODES
431  
432 To check if a error is set, check $returned{error}. A
433 more verbose description of the returned error can be
434 found in $returned{errorString};
435  
436 =head2 1
437  
438 The file/directory does not exist.
439  
440 =head1 AUTHOR
441  
442 Zane C. Bowers, C<< <vvelox at vvelox.net> >>
443  
444 =head1 BUGS
445  
446 Please report any bugs or feature requests to C<bug-gtk2-chmod at rt.cpan.org>, or through
447 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Gtk2-Chmod>. I will be notified, and then you'll
448 automatically be notified of progress on your bug as I make changes.
449  
450  
451  
452  
453 =head1 SUPPORT
454  
455 You can find documentation for this module with the perldoc command.
456  
457 perldoc Gtk2::Chmod
458  
459  
460 You can also look for information at:
461  
462 =over 4
463  
464 =item * RT: CPAN's request tracker
465  
466 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Gtk2-Chmod>
467  
468 =item * AnnoCPAN: Annotated CPAN documentation
469  
470 L<http://annocpan.org/dist/Gtk2-Chmod>
471  
472 =item * CPAN Ratings
473  
474 L<http://cpanratings.perl.org/d/Gtk2-Chmod>
475  
476 =item * Search CPAN
477  
478 L<http://search.cpan.org/dist/Gtk2-Chmod/>
479  
480 =back
481  
482  
483 =head1 ACKNOWLEDGEMENTS
484  
485  
486 =head1 COPYRIGHT & LICENSE
487  
488 Copyright 2009 Zane C. Bowers, all rights reserved.
489  
490 This program is free software; you can redistribute it and/or modify it
491 under the same terms as Perl itself.
492  
493  
494 =cut
495  
496 1; # End of Gtk2::Chmod
↑ Summary