1 /***********************************************************************
3 * This software is part of the ast package *
4 * Copyright (c) 1985-2010 AT&T Intellectual Property *
5 * and is licensed under the *
6 * Common Public License, Version 1.0 *
7 * by AT&T Intellectual Property *
9 * A copy of the License is available at *
10 * http://www.opensource.org/licenses/cpl1.0.txt *
11 * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
13 * Information and Software Systems Research *
17 * Glenn Fowler <gsf@research.att.com> *
18 * David Korn <dgk@research.att.com> *
19 * Phong Vo <kpv@research.att.com> *
21 ***********************************************************************/
27 * mounted filesystem scan support
28 * where are the standards when you really need them
35 #if _lib_mntopen && _lib_mntread && _lib_mntclose
42 * the original interface just had mode
45 #define FIXARGS(p,m,s) do { \
46 if ((p)&&*(p)!='/') { \
64 #include <ast_windows.h>
68 set(register Header_t
* hp
, const char* fs
, const char* dir
, const char* type
, const char* options
)
73 if (x
= (const char*)strchr(fs
, ':'))
75 if (*++x
&& *x
!= '\\')
77 hp
->mnt
.flags
|= MNT_REMOTE
;
85 else if (x
= (const char*)strchr(fs
, '@'))
87 hp
->mnt
.flags
|= MNT_REMOTE
;
88 sfsprintf(hp
->buf
, sizeof(hp
->buf
) - 1, "%s:%*.*s", x
+ 1, x
- fs
, x
- fs
, fs
);
89 fs
= (const char*)hp
->buf
;
91 else if (strmatch(type
, "[aAnN][fF][sS]*"))
92 hp
->mnt
.flags
|= MNT_REMOTE
;
93 if (streq(fs
, "none"))
95 hp
->mnt
.fs
= (char*)fs
;
96 hp
->mnt
.dir
= (char*)dir
;
97 hp
->mnt
.type
= (char*)type
;
98 hp
->mnt
.options
= (char*)options
;
100 if (streq(type
, "system") || streq(type
, "user"))
109 mode
= SetErrorMode(SEM_FAILCRITICALERRORS
);
114 if (GetVolumeInformation(drive
, 0, 0, &vser
, &len
, &flags
, hp
->typ
, sizeof(hp
->typ
) - 1))
115 hp
->mnt
.type
= hp
->typ
;
119 s
= strcopy(hp
->mnt
.options
= hp
->opt
, type
);
120 s
= strcopy(s
, ",ignorecase");
132 #if _lib_getmntinfo && _sys_mount
137 * what a crappy interface
138 * data returned in static buffer -- ok
139 * big chunk of allocated memory that cannot be freed -- come on
140 * *and* netbsd changed the interface somewhere along the line
141 * private interface? my bad -- public interface? par for the bsd course
144 #include <sys/param.h> /* expect some macro redefinitions here */
145 #include <sys/mount.h>
147 #if _lib_getmntinfo_statvfs
148 #define statfs statvfs
149 #define f_flags f_flag
161 #define TYPE(f) ((f)->f_fstypename)
163 #ifdef INITMOUNTNAMES
164 #define TYPE(f) ((char*)type[(f)->f_type])
165 static const char* type
[] = INITMOUNTNAMES
;
168 #define TYPE(f) ((char*)mnt_names[(f)->f_type])
169 #include <sys/fs_types.h>
171 #define TYPE(f) (strchr((f)->f_mntfromname,':')?"nfs":"ufs")
176 static struct Mnt_options_t
184 MNT_RDONLY
, "rdonly",
186 #ifdef MNT_SYNCHRONOUS
187 MNT_SYNCHRONOUS
,"synchronous",
190 MNT_NOEXEC
, "noexec",
193 MNT_NOSUID
, "nosuid",
204 #ifdef MNT_NOCOREDUMP
205 MNT_NOCOREDUMP
, "nocoredump",
208 MNT_NOATIME
, "noatime",
211 MNT_SYMPERM
, "symperm",
213 #ifdef MNT_NODEVMTIME
214 MNT_NODEVMTIME
, "nodevmtime",
217 MNT_SOFTDEP
, "softdep",
220 MNT_EXRDONLY
, "exrdonly",
223 MNT_EXPORTED
, "exported",
225 #ifdef MNT_DEFEXPORTED
226 MNT_DEFEXPORTED
,"defexported",
228 #ifdef MNT_EXPORTANON
229 MNT_EXPORTANON
, "exportanon",
232 MNT_EXKERB
, "exkerb",
234 #ifdef MNT_EXNORESPORT
235 MNT_EXNORESPORT
,"exnoresport",
238 MNT_EXPUBLIC
, "expublic",
247 MNT_ROOTFS
, "rootfs",
253 mntopen(const char* path
, const char* mode
)
255 register Handle_t
* mp
;
258 FIXARGS(path
, mode
, 0);
259 if (!(mp
= newof(0, Handle_t
, 1, 0)))
261 if ((n
= getmntinfo(&mp
->next
, 0)) <= 0)
266 mp
->last
= mp
->next
+ n
;
271 mntread(void* handle
)
273 register Handle_t
* mp
= (Handle_t
*)handle
;
276 register unsigned long flags
;
278 if (mp
->next
< mp
->last
)
280 flags
= mp
->next
->f_flags
;
282 for (i
= 0; i
< elementsof(options
); i
++)
283 if (flags
& options
[i
].flag
)
284 n
+= sfsprintf(mp
->opt
+ n
, sizeof(mp
->opt
) - n
- 1, ",%s", options
[i
].name
);
285 set(&mp
->hdr
, mp
->next
->f_mntfromname
, mp
->next
->f_mntonname
, TYPE(mp
->next
), n
? (mp
->opt
+ 1) : (char*)0);
293 mntclose(void* handle
)
295 register Handle_t
* mp
= (Handle_t
*)handle
;
305 #if _lib_mntctl && _sys_vmount
311 #include <sys/vmount.h>
313 #define SIZE (16 * 1024)
315 static const char* type
[] =
317 "aix", "aix#1", "nfs", "jfs", "aix#4", "cdrom"
327 struct vmount info
[1];
331 mntopen(const char* path
, const char* mode
)
333 register Handle_t
* mp
;
335 FIXARGS(path
, mode
, 0);
336 if (!(mp
= newof(0, Handle_t
, 1, SIZE
)))
338 if ((mp
->count
= mntctl(MCTL_QUERY
, sizeof(Handle_t
) + SIZE
, &mp
->info
)) <= 0)
348 mntread(void* handle
)
350 register Handle_t
* mp
= (Handle_t
*)handle
;
357 if (vmt2datasize(mp
->next
, VMT_HOST
) && (s
= vmt2dataptr(mp
->next
, VMT_HOST
)) && !streq(s
, "-"))
359 sfsprintf(mp
->remote
, sizeof(mp
->remote
) - 1, "%s:%s", s
, vmt2dataptr(mp
->next
, VMT_OBJECT
));
363 s
= vmt2dataptr(mp
->next
, VMT_OBJECT
);
364 if (vmt2datasize(mp
->next
, VMT_ARGS
))
365 o
= vmt2dataptr(mp
->next
, VMT_ARGS
);
368 switch (mp
->next
->vmt_gfstype
)
411 sfsprintf(t
= mp
->type
, sizeof(mp
->type
), "aix%+d", mp
->next
->vmt_gfstype
);
414 set(&mp
->hdr
, s
, vmt2dataptr(mp
->next
, VMT_STUB
), t
, o
);
416 mp
->next
= (struct vmount
*)((char*)mp
->next
+ mp
->next
->vmt_length
);
423 mntclose(void* handle
)
425 register Handle_t
* mp
= (Handle_t
*)handle
;
436 #undef _lib_getmntent
437 #if !_SCO_COFF && !_SCO_ELF && !_UTS
442 #if _lib_getmntent && ( _hdr_mntent || _sys_mntent && !_sys_mnttab )
444 #if defined(__STDPP__directive) && defined(__STDPP__hide)
445 __STDPP__directive pragma pp
:hide endmntent getmntent
447 #define endmntent ______endmntent
448 #define getmntent ______getmntent
455 #include <sys/mntent.h>
458 #if defined(__STDPP__directive) && defined(__STDPP__hide)
459 __STDPP__directive pragma pp
:nohide endmntent getmntent
465 extern int endmntent(FILE*);
466 extern struct mntent
* getmntent(FILE*);
470 #undef _lib_getmntent
476 #include <sys/mnttab.h>
484 #define MOUNTED MNT_MNTTAB
486 #if _hdr_mnttab || _sys_mnttab
487 #define MOUNTED "/etc/mnttab"
489 #define MOUNTED "/etc/mtab"
496 #define MOUNTED "/etc/fstab"
503 #if _mem_mnt_opts_mntent
504 #define OPTIONS(p) ((p)->mnt_opts)
506 #define OPTIONS(p) NiL
515 mntopen(const char* path
, const char* mode
)
517 register Handle_t
* mp
;
519 FIXARGS(path
, mode
, MOUNTED
);
520 if (!(mp
= newof(0, Handle_t
, 1, 0)))
522 if (!(mp
->fp
= setmntent(path
, mode
)))
531 mntread(void* handle
)
533 register Handle_t
* mp
= (Handle_t
*)handle
;
534 register struct mntent
* mnt
;
536 if (mnt
= getmntent(mp
->fp
))
538 set(&mp
->hdr
, mnt
->mnt_fsname
, mnt
->mnt_dir
, mnt
->mnt_type
, OPTIONS(mnt
));
545 mntclose(void* handle
)
547 register Handle_t
* mp
= (Handle_t
*)handle
;
558 #if _sys_mntent && _lib_w_getmntent
560 #include <sys/mntent.h>
562 #define mntent w_mntent
564 #define mnt_dir mnt_mountpoint
565 #define mnt_type mnt_fstname
567 #define MNTBUFSIZE (sizeof(struct w_mnth)+16*sizeof(struct w_mntent))
569 #if _mem_mnt_opts_w_mntent
570 #define OPTIONS(p) ((p)->mnt_opts)
572 #define OPTIONS(p) NiL
577 #undef _lib_w_getmntent
579 #define MNTBUFSIZE sizeof(struct mntent)
581 #if !_mem_mt_dev_mnttab || !_mem_mt_filsys_mnttab
587 #define mntent mnttab
589 #define mnt_fsname mt_dev
590 #define mnt_dir mt_filsys
591 #if _mem_mt_fstyp_mnttab
592 #define mnt_type mt_fstyp
595 #if _mem_mnt_opts_mnttab
596 #define OPTIONS(p) ((p)->mnt_opts)
598 #define OPTIONS(p) NiL
605 char mnt_fsname
[256];
611 #define OPTIONS(p) ((p)->mnt_opts)
625 char buf
[MNTBUFSIZE
];
629 mntopen(const char* path
, const char* mode
)
631 register Handle_t
* mp
;
633 FIXARGS(path
, mode
, MOUNTED
);
634 if (!(mp
= newof(0, Handle_t
, 1, 0)))
637 if ((mp
->count
= w_getmntent(mp
->buf
, sizeof(mp
->buf
))) > 0)
638 mp
->mnt
= (struct mntent
*)(((struct w_mnth
*)mp
->buf
) + 1);
641 mp
->mnt
= (struct mntent
*)mp
->buf
;
642 if (!(mp
->fp
= sfopen(NiL
, path
, mode
)))
652 mntread(void* handle
)
654 register Handle_t
* mp
= (Handle_t
*)handle
;
658 if (mp
->count
-- <= 0)
660 if ((mp
->count
= w_getmntent(mp
->buf
, sizeof(mp
->buf
))) <= 0)
663 mp
->mnt
= (struct mntent
*)(((struct w_mnth
*)mp
->buf
) + 1);
665 set(&mp
->hdr
, mp
->mnt
->mnt_fsname
, mp
->mnt
->mnt_dir
, mp
->mnt
->mnt_type
, OPTIONS(mp
->mnt
));
673 while (sfread(mp
->fp
, &mp
->buf
, sizeof(mp
->buf
)) == sizeof(mp
->buf
))
674 if (*mp
->mnt
->mnt_fsname
&& *mp
->mnt
->mnt_dir
)
681 set(&mp
->hdr
, mp
->mnt
->mnt_fsname
, mp
->mnt
->mnt_dir
, stat(mp
->mnt
->mnt_dir
, &st
) ? FS_default
: strncpy(typ
, fmtfs(&st
), sizeof(typ
) - 1), OPTIONS(mp
->mnt
));
683 set(&mp
->hdr
, mp
->mnt
->mnt_fsname
, mp
->mnt
->mnt_dir
, mp
->mnt
->mnt_type
, OPTIONS(mp
->mnt
));
701 b
= s
= mp
->mnt
->mnt_fsname
;
702 m
= s
+ sizeof(mp
->mnt
->mnt_fsname
) - 1;
703 for (;;) switch (c
= sfgetc(mp
->fp
))
720 if (s
!= b
&& !q
) switch (++x
)
724 b
= s
= mp
->mnt
->mnt_dir
;
725 m
= s
+ sizeof(mp
->mnt
->mnt_dir
) - 1;
729 b
= s
= mp
->mnt
->mnt_type
;
730 m
= s
+ sizeof(mp
->mnt
->mnt_type
) - 1;
734 b
= s
= mp
->mnt
->mnt_opts
;
735 m
= s
+ sizeof(mp
->mnt
->mnt_opts
) - 1;
746 set(&mp
->hdr
, mp
->mnt
->mnt_fsname
, mp
->mnt
->mnt_dir
, mp
->mnt
->mnt_type
, OPTIONS(mp
->mnt
));
763 mntclose(void* handle
)
765 register Handle_t
* mp
= (Handle_t
*)handle
;
785 mntwrite(void* handle
, const Mnt_t
* mnt
)