summaryrefslogtreecommitdiff
path: root/Master/tlpkg/tlperl.straw/lib/Win32/File/Object.pm
blob: bb4aa90ecdc0c175bfa88cee979399b2fdbe6583 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
package Win32::File::Object;

=pod

=head1 NAME

Win32::File::Object - Simplified object abstraction over Win32::File

=head1 SYNOPSIS

  # Get a handle for the file.
  my $object = Win32::File::Object->new( $filename, $autowrite );
  
  # Read a property flag for the file.
  my $readonly = $object->readonly;
  
  # Set a propertly flag for the file.
  $object->readonly(1);
  
  # If autowrite is false, write the changes to the file.
  $object->write;

=head1 DESCRIPTION

L<Win32::File> is an interface to the Win32 API for file
attributes.

Unfortunately it is a B<direct> interface to the underlying Win32 API,
with a completely non-Perlish interface involving CamelCase function
names, bit-field flags and return-by-param.

B<Win32::File::Object> is a straight-forward object-oriented Perlish
wrapper around the raw underlying API wrapper.

=head1 METHODS

=cut

use 5.006;
use strict;
use Carp        ();
use Win32::File ();

use vars qw{$VERSION};
BEGIN {
	$VERSION = '0.02';
}





#####################################################################
# Constructor

=pod

=head2 new

  my $file = Win32::File::Object->new( $path, $autowrite );

The C<new> constructor creates a new handle to the Win32 filesystem
attributes of an existing file or directory.

The compulsory C<$filename> parameter is the name of the file or
directory to create the handle on.

The optional C<$autowrite> parameter, if true, indicates that the
object should write the filesystem attributes to the file every
time the method is called to set the property.

If the C<$autowrite> param is false or not provided, you will
need to call an explicit C<write> method in order to apply the
changes to the file.

=cut

sub new {
	my $class     = shift;
	my $path      = shift;
	my $autowrite = !! shift;
	unless ( $path ) {
		Carp::croak("Did not provide a file name");
	}
	unless ( -f $path ) {
		Carp::croak("File '$path' does not exist");
	}

	# Create the object
	my $self = bless {
		path      => $path,
		autowrite => $autowrite,
		rollback  => ! 1,
	}, $class;

	# Get the attributes
	$self->read;

	return $self;
}

=pod

=head2 path

The C<path> accessor returns the original file path as provided to
the constructor as a string.

=cut

sub path {
	$_[0]->{path};
}

=pod

=head2 autowrite

The C<autowrite> accessor returns true if the object will
automatically write changes to the filesystem, or false if
not.

=cut

sub autowrite {
	$_[0]->{autowrite};
}





#####################################################################
# Main Methods

=pod

=head2 read

the C<read> method reads (updates) the filesystem attributes, in case
they have been updated since the object was originally created.

Returns true on success or throws an exception (dies) on error.

=cut

sub read {
	my $self = shift;

	# Read the bitfield
	my $bits;
	my $path = $self->path;
	unless ( Win32::File::GetAttributes( $self->path => $bits ) ) {
		Carp::croak("GetAttributes failed for '$path'");
	}

	# Read the flags
	$self->{archive}    = ( $bits & Win32::File::ARCHIVE()    ) ? 1 : 0;
	$self->{compressed} = ( $bits & Win32::File::COMPRESSED() ) ? 1 : 0;
	$self->{directory}  = ( $bits & Win32::File::DIRECTORY()  ) ? 1 : 0;
	$self->{hidden}     = ( $bits & Win32::File::HIDDEN()     ) ? 1 : 0;
	$self->{normal}     = ( $bits & Win32::File::NORMAL()     ) ? 1 : 0;
	$self->{offline}    = ( $bits & Win32::File::OFFLINE()    ) ? 1 : 0;
	$self->{readonly}   = ( $bits & Win32::File::READONLY()   ) ? 1 : 0;
	$self->{system}     = ( $bits & Win32::File::SYSTEM()     ) ? 1 : 0;
	$self->{temporary}  = ( $bits & Win32::File::TEMPORARY()  ) ? 1 : 0;

	return 1;
}

=pod

=head2 write

the C<write> method writes the object attributes back to the filesystem.

Returns true on success or throws an exception (dies) on error.

=cut

sub write {
	my $self = shift;

	# Generate the bitfield from the attributes
	my $bits = 0;
	if ( $self->archive ) {
		$bits += Win32::File::ARCHIVE();
	}
	if ( $self->compressed ) {
		$bits += Win32::File::COMPRESSED();
	}
	if ( $self->directory ) {
		$bits += Win32::File::DIRECTORY();
	}
	if ( $self->hidden ) {
		$bits += Win32::File::HIDDEN();
	}
	if ( $self->normal ) {
		$bits += Win32::File::NORMAL();
	}
	if ( $self->offline ) {
		$bits += Win32::File::OFFLINE();
	}
	if ( $self->readonly ) {
		$bits += Win32::File::READONLY();
	}
	if ( $self->system ) {
		$bits += Win32::File::SYSTEM();
	}
	if ( $self->temporary ) {
		$bits += Win32::File::TEMPORARY();
	}

	# Apply the attributes to the file
	my $path = $self->path;
	unless ( Win32::File::SetAttributes( $path, $bits ) ) {
		Carp::croak("SetAttributes failed for '$path'");
	}

	return 1;
}





#####################################################################
# Attribute Methods

=pod

=head2 archive

  # Get the value
  my $archive = $file->archive;
  
  # Set the value
  $file->archive(1);

The C<archive> accessor gets or set the Win32 "archive" status for
the file.

=cut

sub archive {
	shift->_attr( archive => @_ );
}

=pod

=head2 compressed

  # Get the value
  my $compressed = $file->compressed;
  
  # Set the value
  $file->compressed(1);

The C<compressed> accessor gets or set the Win32 "compressed" status
for the file.

=cut

sub compressed {
	shift->_attr( compressed => @_ );
}

=pod

=head2 directory

  # Get the value
  my $directory = $file->directory;
  
  # Set the value
  $file->directory(1);

The C<directory> accessor gets or set the Win32 "directory" status for
the file.

=cut

sub directory {
	shift->_attr( directory => @_ );
}

=pod

=head2 hidden

  # Get the value
  my $hidden = $file->hidden;
  
  # Set the value
  $file->hidden(1);

The C<hidden> accessor gets or set the Win32 "hidden" status for
the file.

=cut

sub hidden {
	shift->_attr( hidden => @_ );
}

=pod

=head2 normal

  # Get the value
  my $normal = $file->normal;
  
  # Set the value
  $file->normal(1);

The C<normal> accessor gets or set the Win32 "normal" status for
the file.

=cut

sub normal {
	shift->_attr( normal => @_ );
}

=pod

=head2 offline

  # Get the value
  my $offline = $file->offline;
  
  # Set the value
  $file->offline(1);

The C<offline> accessor gets or set the Win32 "offline" status for
the file.

=cut

sub offline {
	shift->_attr( offline => @_ );
}

=pod

=head2 readonly

  # Get the value
  my $readonly = $file->readonly;
  
  # Set the value
  $file->readonly(1);

The C<readonly> accessor gets or set the Win32 "readonly" status for
the file.

=cut

sub readonly {
	shift->_attr( readonly => @_ );
}

=pod

=head2 system

  # Get the value
  my $system = $file->system;
  
  # Set the value
  $file->system(1);

The C<system> accessor gets or set the Win32 "system" status for
the file.

=cut

sub system {
	shift->_attr( system => @_ );
}

=pod

=head2 temporary

  # Get the value
  my $temporary = $file->temporary;
  
  # Set the value
  $file->temporary(1);

The C<temporary> accessor gets or set the Win32 "temporary" status for
the file.

=cut

sub temporary {
	shift->_attr( temporary => @_ );
}

sub _attr {
	my $self = shift;
	my $name = shift;
	my $new  = $_[0] ? 1 : 0;
	return $self->{$name} unless @_;
	return $self->{$name} if $new == $self->{$name};

	# Set the rollback if needed
	if ( $self->{rollback} and ! exists $self->{rollback}->{$name} ) {
		$self->{rollback}->{$name} = $new;
	}

	# Set the new value
	$self->{$name} = $new;
	$self->write if $self->autowrite;

	return $self->{$name};
}

1;

=pod

=head1 SUPPORT

Bugs should be reported via the CPAN bug tracker at

L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Win32-File-Object>

For other issues, or commercial enhancement or support, contact the author.

=head1 AUTHOR

Adam Kennedy E<lt>adamk@cpan.orgE<gt>

=head1 SEE ALSO

L<Win32::File>

=head1 COPYRIGHT

Copyright 2008 - 2009 Adam Kennedy.

This program is free software; you can redistribute
it and/or modify it under the same terms as Perl itself.

The full text of the license can be found in the
LICENSE file included with this module.

=cut