[OpenAFS] Re: how to tell if a dir resides in AFS volume?

shuaijie wang wangshuaijie@gmail.com
Wed, 18 Sep 2013 13:08:50 +0800


--089e0115e84ad4f58304e6a16e16
Content-Type: text/plain; charset=ISO-8859-1

Thanks very much!


2013/9/18 Andrew Deason <adeason@sinenomine.net>

> On Tue, 17 Sep 2013 18:32:31 +0800
> shuaijie wang <wangshuaijie@gmail.com> wrote:
>
> > Is there any method to check if a dir resides in AFS volume? Since AFS
> > can be mounted in any local logical path, simply checking prefix
> > "/afs" seems not sufficient for all the cases. So is there any system
> > call like "is_inafs()" to accomplish this?
>
> The most portable and "correct" way is to issue a pioctl. Stuff based on
> mountpoint information or statfs/statvfs info I think can vary depending
> on the system, so may not be portable. If you don't care about that, you
> can just look at the fs info from statfs/statvfs.
>
> If this is for a script, just call an afs command like 'fs examine' or
> 'fs lq' or whatever. For a C program, you can call a variant on pioctl()
> itself. From libkopenafs, you have k_pioctl. An example of usage:
>
> $ cat foo.c
> #include <stdio.h>
> #include <stdint.h>
> #include <string.h>
> #include <errno.h>
>
> #include <sys/ioctl.h>
> #include <kopenafs.h>
>
> int
> main(int argc, char *argv[])
> {
>     struct ViceIoctl iob;
>     uint32_t fid[4];
>     int code;
>
>     if (argc < 2) {
>         return 1;
>     }
>
>     memset(&iob, 0, sizeof(iob));
>     iob.out = (void*)&fid;
>     iob.out_size = sizeof(fid);
>
>     code = k_pioctl(argv[1], VIOCGETFID, &iob, 1);
>     if (code) {
>         printf("File %s is NOT in afs (or there was an error)\n", argv[1]);
>         return 1;
>     }
>     printf("File %s is in afs\n", argv[1]);
>     return 0;
> }
> $ gcc foo.c -lkopenafs
> $ ./a.out /etc/passwd
> File /etc/passwd is NOT in afs (or there was an error)
> $ ./a.out /afs/.localcell
> File /afs/.localcell is in afs
> $ ln -s /afs/.localcell /tmp/fooafs
> $ ./a.out /tmp/fooafs
> File /tmp/fooafs is in afs
>
> The last argument to k_pioctl is a flag for whether or not to follow
> symlinks (setting it to 0 would change the result of that last example).
> If a file is actually in afs, k_pioctl can obviously still fail if we
> can't resolve the whole path. The return code should be -1 for 'not in
> afs', 0 for 'in afs', and something else for an error. Sometimes other
> errors appear as -1, though, so I wouldn't interpret a -1 as an
> authoritative answer.
>
> That k_pioctl method also requires effectively stat()ing the file. If
> you run it on a mountpoint, we will try to access the target directory.
> You can avoid that (and possibly run more quickly) by using some
> different pioctl calls, but if you don't care, just go with the above
> simple example.
>
> --
> Andrew Deason
> adeason@sinenomine.net
>
> _______________________________________________
> OpenAFS-info mailing list
> OpenAFS-info@openafs.org
> https://lists.openafs.org/mailman/listinfo/openafs-info
>

--089e0115e84ad4f58304e6a16e16
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr">Thanks very much!<br></div><div class=3D"gmail_extra"><br>=
<br><div class=3D"gmail_quote">2013/9/18 Andrew Deason <span dir=3D"ltr">&l=
t;<a href=3D"mailto:adeason@sinenomine.net" target=3D"_blank">adeason@sinen=
omine.net</a>&gt;</span><br>
<blockquote class=3D"gmail_quote" style=3D"margin:0 0 0 .8ex;border-left:1p=
x #ccc solid;padding-left:1ex"><div class=3D"im">On Tue, 17 Sep 2013 18:32:=
31 +0800<br>
shuaijie wang &lt;<a href=3D"mailto:wangshuaijie@gmail.com">wangshuaijie@gm=
ail.com</a>&gt; wrote:<br>
<br>
</div><div><div class=3D"h5">&gt; Is there any method to check if a dir res=
ides in AFS volume? Since AFS<br>
&gt; can be mounted in any local logical path, simply checking prefix<br>
&gt; &quot;/afs&quot; seems not sufficient for all the cases. So is there a=
ny system<br>
&gt; call like &quot;is_inafs()&quot; to accomplish this?<br>
<br>
</div></div>The most portable and &quot;correct&quot; way is to issue a pio=
ctl. Stuff based on<br>
mountpoint information or statfs/statvfs info I think can vary depending<br=
>
on the system, so may not be portable. If you don&#39;t care about that, yo=
u<br>
can just look at the fs info from statfs/statvfs.<br>
<br>
If this is for a script, just call an afs command like &#39;fs examine&#39;=
 or<br>
&#39;fs lq&#39; or whatever. For a C program, you can call a variant on pio=
ctl()<br>
itself. From libkopenafs, you have k_pioctl. An example of usage:<br>
<br>
$ cat foo.c<br>
#include &lt;stdio.h&gt;<br>
#include &lt;stdint.h&gt;<br>
#include &lt;string.h&gt;<br>
#include &lt;errno.h&gt;<br>
<br>
#include &lt;sys/ioctl.h&gt;<br>
#include &lt;kopenafs.h&gt;<br>
<br>
int<br>
main(int argc, char *argv[])<br>
{<br>
=A0 =A0 struct ViceIoctl iob;<br>
=A0 =A0 uint32_t fid[4];<br>
=A0 =A0 int code;<br>
<br>
=A0 =A0 if (argc &lt; 2) {<br>
=A0 =A0 =A0 =A0 return 1;<br>
=A0 =A0 }<br>
<br>
=A0 =A0 memset(&amp;iob, 0, sizeof(iob));<br>
=A0 =A0 iob.out =3D (void*)&amp;fid;<br>
=A0 =A0 iob.out_size =3D sizeof(fid);<br>
<br>
=A0 =A0 code =3D k_pioctl(argv[1], VIOCGETFID, &amp;iob, 1);<br>
=A0 =A0 if (code) {<br>
=A0 =A0 =A0 =A0 printf(&quot;File %s is NOT in afs (or there was an error)\=
n&quot;, argv[1]);<br>
=A0 =A0 =A0 =A0 return 1;<br>
=A0 =A0 }<br>
=A0 =A0 printf(&quot;File %s is in afs\n&quot;, argv[1]);<br>
=A0 =A0 return 0;<br>
}<br>
$ gcc foo.c -lkopenafs<br>
$ ./a.out /etc/passwd<br>
File /etc/passwd is NOT in afs (or there was an error)<br>
$ ./a.out /afs/.localcell<br>
File /afs/.localcell is in afs<br>
$ ln -s /afs/.localcell /tmp/fooafs<br>
$ ./a.out /tmp/fooafs<br>
File /tmp/fooafs is in afs<br>
<br>
The last argument to k_pioctl is a flag for whether or not to follow<br>
symlinks (setting it to 0 would change the result of that last example).<br=
>
If a file is actually in afs, k_pioctl can obviously still fail if we<br>
can&#39;t resolve the whole path. The return code should be -1 for &#39;not=
 in<br>
afs&#39;, 0 for &#39;in afs&#39;, and something else for an error. Sometime=
s other<br>
errors appear as -1, though, so I wouldn&#39;t interpret a -1 as an<br>
authoritative answer.<br>
<br>
That k_pioctl method also requires effectively stat()ing the file. If<br>
you run it on a mountpoint, we will try to access the target directory.<br>
You can avoid that (and possibly run more quickly) by using some<br>
different pioctl calls, but if you don&#39;t care, just go with the above<b=
r>
simple example.<br>
<span class=3D"HOEnZb"><font color=3D"#888888"><br>
--<br>
Andrew Deason<br>
<a href=3D"mailto:adeason@sinenomine.net">adeason@sinenomine.net</a><br>
</font></span><div class=3D"HOEnZb"><div class=3D"h5"><br>
_______________________________________________<br>
OpenAFS-info mailing list<br>
<a href=3D"mailto:OpenAFS-info@openafs.org">OpenAFS-info@openafs.org</a><br=
>
<a href=3D"https://lists.openafs.org/mailman/listinfo/openafs-info" target=
=3D"_blank">https://lists.openafs.org/mailman/listinfo/openafs-info</a><br>
</div></div></blockquote></div><br></div>

--089e0115e84ad4f58304e6a16e16--