xref: /openbsd-src/gnu/usr.bin/perl/os2/OS2/OS2-ExtAttr/ExtAttr.pm (revision b8851fcc53cbe24fd20b090f26dd149e353f6174)
1b39c5158Smillertpackage OS2::ExtAttr;
2b39c5158Smillert
3b39c5158Smillertuse strict;
4b39c5158Smillertuse XSLoader;
5b39c5158Smillert
6*b8851fccSafresh1our $VERSION = '0.04';
7b39c5158SmillertXSLoader::load 'OS2::ExtAttr', $VERSION;
8b39c5158Smillert
9b39c5158Smillert# Preloaded methods go here.
10b39c5158Smillert
11b39c5158Smillert# Format of the array:
12b39c5158Smillert# 0 ead, 1 file name, 2 file handle. 3 length, 4 position, 5 need to write.
13b39c5158Smillert
14b39c5158Smillertsub TIEHASH {
15b39c5158Smillert  my $class = shift;
16b39c5158Smillert  my $ea = _create() || die "Cannot create EA: $!";
17b39c5158Smillert  my $file = shift;
18b39c5158Smillert  my ($name, $handle);
19b39c5158Smillert  if (ref $file eq 'GLOB' or ref \$file eq 'GLOB') {
20b39c5158Smillert    die "File handle is not opened" unless $handle = fileno $file;
21b39c5158Smillert    _read($ea, undef, $handle, 0);
22b39c5158Smillert  } else {
23b39c5158Smillert    $name = $file;
24b39c5158Smillert    _read($ea, $name, 0, 0);
25b39c5158Smillert  }
26b39c5158Smillert  bless [$ea, $name, $handle, 0, 0, 0], $class;
27b39c5158Smillert}
28b39c5158Smillert
29b39c5158Smillertsub DESTROY {
30b39c5158Smillert  my $eas = shift;
31b39c5158Smillert  # 0 means: discard eas which are not in $eas->[0].
32b39c5158Smillert  _write( $eas->[0], $eas->[1], $eas->[2], 0) and die "Cannot write EA: $!"
33b39c5158Smillert    if $eas->[5];
34b39c5158Smillert  _destroy( $eas->[0] );
35b39c5158Smillert}
36b39c5158Smillert
37b39c5158Smillertsub FIRSTKEY {
38b39c5158Smillert  my $eas = shift;
39b39c5158Smillert  $eas->[3] = _count($eas->[0]);
40b39c5158Smillert  $eas->[4] = 1;
41b39c5158Smillert  return undef if $eas->[4] > $eas->[3];
42b39c5158Smillert  return _get_name($eas->[0], $eas->[4]);
43b39c5158Smillert}
44b39c5158Smillert
45b39c5158Smillertsub NEXTKEY {
46b39c5158Smillert  my $eas = shift;
47b39c5158Smillert  $eas->[4]++;
48b39c5158Smillert  return undef if $eas->[4] > $eas->[3];
49b39c5158Smillert  return _get_name($eas->[0], $eas->[4]);
50b39c5158Smillert}
51b39c5158Smillert
52b39c5158Smillertsub FETCH {
53b39c5158Smillert  my $eas = shift;
54b39c5158Smillert  my $index = _find($eas->[0], shift);
55b39c5158Smillert  return undef if $index <= 0;
56b39c5158Smillert  return value($eas->[0], $index);
57b39c5158Smillert}
58b39c5158Smillert
59b39c5158Smillertsub EXISTS {
60b39c5158Smillert  my $eas = shift;
61b39c5158Smillert  return _find($eas->[0], shift) > 0;
62b39c5158Smillert}
63b39c5158Smillert
64b39c5158Smillertsub STORE {
65b39c5158Smillert  my $eas = shift;
66b39c5158Smillert  $eas->[5] = 1;
67b39c5158Smillert  add($eas->[0], shift, shift) > 0 or die "Error setting EA: $!";
68b39c5158Smillert}
69b39c5158Smillert
70b39c5158Smillertsub DELETE {
71b39c5158Smillert  my $eas = shift;
72b39c5158Smillert  my $index = _find($eas->[0], shift);
73b39c5158Smillert  return undef if $index <= 0;
74b39c5158Smillert  my $value = value($eas->[0], $index);
75b39c5158Smillert  _delete($eas->[0], $index) and die "Error deleting EA: $!";
76b39c5158Smillert  $eas->[5] = 1;
77b39c5158Smillert  return $value;
78b39c5158Smillert}
79b39c5158Smillert
80b39c5158Smillertsub CLEAR {
81b39c5158Smillert  my $eas = shift;
82b39c5158Smillert  _clear($eas->[0]);
83b39c5158Smillert  $eas->[5] = 1;
84b39c5158Smillert}
85b39c5158Smillert
86b39c5158Smillert# Here are additional methods:
87b39c5158Smillert
88b39c5158Smillert*new = \&TIEHASH;
89b39c5158Smillert
90b39c5158Smillertsub copy {
91b39c5158Smillert  my $eas = shift;
92b39c5158Smillert  my $file = shift;
93b39c5158Smillert  my ($name, $handle);
94b39c5158Smillert  if (ref $file eq 'GLOB' or ref \$file eq 'GLOB') {
95b39c5158Smillert    die "File handle is not opened" unless $handle = fileno $file;
96b39c5158Smillert    _write($eas->[0], undef, $handle, 0) or die "Cannot write EA: $!";
97b39c5158Smillert  } else {
98b39c5158Smillert    $name = $file;
99b39c5158Smillert    _write($eas->[0], $name, 0, 0) or die "Cannot write EA: $!";
100b39c5158Smillert  }
101b39c5158Smillert}
102b39c5158Smillert
103b39c5158Smillertsub update {
104b39c5158Smillert  my $eas = shift;
105b39c5158Smillert  # 0 means: discard eas which are not in $eas->[0].
106b39c5158Smillert  _write( $eas->[0], $eas->[1], $eas->[2], 0) and die "Cannot write EA: $!";
107b39c5158Smillert}
108b39c5158Smillert
109b39c5158Smillert# Autoload methods go after =cut, and are processed by the autosplit program.
110b39c5158Smillert
111b39c5158Smillert1;
112b39c5158Smillert__END__
113b39c5158Smillert# Below is the stub of documentation for your module. You better edit it!
114b39c5158Smillert
115b39c5158Smillert=head1 NAME
116b39c5158Smillert
117b39c5158SmillertOS2::ExtAttr - Perl access to extended attributes.
118b39c5158Smillert
119b39c5158Smillert=head1 SYNOPSIS
120b39c5158Smillert
121b39c5158Smillert  use OS2::ExtAttr;
122b39c5158Smillert  tie %ea, 'OS2::ExtAttr', 'my.file';
123b39c5158Smillert  print $ea{eaname};
124b39c5158Smillert  $ea{myfield} = 'value';
125b39c5158Smillert
126b39c5158Smillert  untie %ea;
127b39c5158Smillert
128b39c5158Smillert=head1 DESCRIPTION
129b39c5158Smillert
130b39c5158SmillertThe package provides low-level and high-level interface to Extended
131b39c5158SmillertAttributes under OS/2.
132b39c5158Smillert
133b39c5158Smillert=head2 High-level interface: C<tie>
134b39c5158Smillert
135b39c5158SmillertThe only argument of tie() is a file name, or an open file handle.
136b39c5158Smillert
137b39c5158SmillertNote that all the changes of the tied hash happen in core, to
138b39c5158Smillertpropagate it to disk the tied hash should be untie()ed or should go
139b39c5158Smillertout of scope. Alternatively, one may use the low-level C<update>
140b39c5158Smillertmethod on the corresponding object. Example:
141b39c5158Smillert
142b39c5158Smillert  tied(%hash)->update;
143b39c5158Smillert
144b39c5158SmillertNote also that setting/getting EA flag is not supported by the
145b39c5158Smillerthigh-level interface, one should use the low-level interface
146b39c5158Smillertinstead. To use it on a tied hash one needs undocumented way to find
147b39c5158SmillertC<eas> give the tied hash.
148b39c5158Smillert
149b39c5158Smillert=head2 Low-level interface
150b39c5158Smillert
151b39c5158SmillertTwo low-level methods are supported by the objects: copy() and
152b39c5158Smillertupdate(). The copy() takes one argument: the name of a file to copy
153b39c5158Smillertthe attributes to, or an opened file handle. update() takes no
154b39c5158Smillertarguments, and is discussed above.
155b39c5158Smillert
156b39c5158SmillertThree convenience functions are provided:
157b39c5158Smillert
158b39c5158Smillert  value($eas, $key)
159b39c5158Smillert  add($eas, $key, $value [, $flag])
160b39c5158Smillert  replace($eas, $key, $value [, $flag])
161b39c5158Smillert
162b39c5158SmillertThe default value for C<flag> is 0.
163b39c5158Smillert
164b39c5158SmillertIn addition, all the C<_ea_*> and C<_ead_*> functions defined in EMX
165*b8851fccSafresh1library are supported, with leading C<_ea> and C<_ead> stripped.
166b39c5158Smillert
167b39c5158Smillert=head1 AUTHOR
168b39c5158Smillert
169b39c5158SmillertIlya Zakharevich, ilya@math.ohio-state.edu
170b39c5158Smillert
171b39c5158Smillert=head1 SEE ALSO
172b39c5158Smillert
173b39c5158Smillertperl(1).
174b39c5158Smillert
175b39c5158Smillert=cut
176