* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
+
#include "libfsid.h"
+
#include <vfs/isofs/cd9660/iso.h>
static char buffer[ISO_DEFAULT_BLOCK_SIZE];
-int
+fsid_t
cd9660_probe(const char *dev)
{
struct iso_primary_descriptor *pdsc;
if (fsid_dev_read(dev, 32768L, sizeof(buffer), buffer) != 0)
- return 0;
+ return FSID_UNKNOWN;
pdsc = (struct iso_primary_descriptor *)&buffer;
if ((strncmp(pdsc->id, ISO_STANDARD_ID, 4)) == 0)
- return 1;
+ return FSID_CD9660;
- return 0;
+ return FSID_UNKNOWN;
}
char *
pdsc = (struct iso_primary_descriptor *)&buffer;
- if ((strncmp(pdsc->id, ISO_STANDARD_ID, 4)) != 0)
+ if (strncmp(pdsc->id, ISO_STANDARD_ID, 4) != 0 ||
+ pdsc->volume_id[0] == '\0')
return NULL;
- if (pdsc->volume_id[0] == '\0')
- return NULL;
+ pdsc->volume_id[sizeof(pdsc->volume_id) - 1] = '\0';
- pdsc->volume_id[30] = '\0';
return pdsc->volume_id;
}
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
-#include "libfsid.h"
+
#include <vfs/gnu/ext2fs/ext2_fs.h>
#include <vfs/gnu/ext2fs/fs.h>
+#include "libfsid.h"
+
static char buffer[sizeof(struct ext2_super_block)*2];
-int
+fsid_t
ext2_probe(const char *dev)
{
static struct ext2_super_block *fs;
if (fsid_dev_read(dev, SBOFF, sizeof(buffer), buffer) != 0)
- return 0;
+ return FSID_UNKNOWN;
fs = (struct ext2_super_block *)&buffer;
if (fs->s_magic == EXT2_SUPER_MAGIC)
- return 1;
+ return FSID_EXT2;
- return 0;
+ return FSID_UNKNOWN;
}
char *
fs = (struct ext2_super_block *)&buffer;
- if (fs->s_magic != EXT2_SUPER_MAGIC)
+ if (fs->s_magic != EXT2_SUPER_MAGIC || fs->s_volume_name[0] == '\0')
return NULL;
- if (fs->s_volume_name[0] == '\0')
- return NULL;
+ fs->s_volume_name[sizeof(fs->s_volume_name) - 1] = '\0';
- fs->s_volume_name[15] = '\0';
- return fs->s_volume_name;
+ return fs->s_volume_name;
}
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
+
#include "libfsid.h"
+
#include <vfs/hammer/hammer_disk.h>
-static char buffer[16384];
+static char buffer[HAMMER_BUFSIZE];
-int
+fsid_t
hammer_probe(const char *dev)
{
static struct hammer_volume_ondisk *fs;
if (fsid_dev_read(dev, 0L, sizeof(buffer), buffer) != 0)
- return 0;
+ return FSID_UNKNOWN;
fs = (struct hammer_volume_ondisk *)&buffer;
- if (fs->vol_signature != HAMMER_FSBUF_VOLUME) {
- return 0;
- }
+ if (fs->vol_signature != HAMMER_FSBUF_VOLUME)
+ return FSID_UNKNOWN;
- return 1;
+ return FSID_HAMMER;
}
char *
fs = (struct hammer_volume_ondisk *)&buffer;
- if (fs->vol_signature != HAMMER_FSBUF_VOLUME) {
+ if (fs->vol_signature != HAMMER_FSBUF_VOLUME ||
+ fs->vol_name[0] == '\0')
return NULL;
- }
- if (fs->vol_name[0] == '\0')
- return NULL;
+ fs->vol_name[sizeof(fs->vol_name) - 1] = '\0';
- fs->vol_name[63] = '\0';
return fs->vol_name;
}
.\" Copyright (c) 2010 The DragonFly Project. All rights reserved.
.\"
.\" This code is derived from software contributed to The DragonFly Project
-.\" by Ákos Kovács <akoskovacs@gmx.com>
+.\" by Akos Kovacs <akoskovacs@gmx.com>
.\"
.\" Redistribution and use in source and binary forms, with or without
.\" modification, are permitted provided that the following conditions
.Nm fsid_probe_all ,
.Nm fsid_volname ,
.Nm fsid_volname_all ,
-.Nm fsid_fsname
+.Nm fsid_fsname ,
.Nm fsid_fs_count
.Nd general libfsid functions
.Sh LIBRARY
-A simple interface for filesystem recognising.
-Programs should be linked with
-.Fl lfsid .
+.Lb libfsid
.Sh SYNOPSIS
.In libfsid.h
-.Ft int
-.Fn fsid_probe "const char *dev" "const char *fs_type"
-.Ft int
+.Ft fsid_t
+.Fn fsid_probe "const char *dev" "const char *fs_name"
+.Ft fsid_t
.Fn fsid_probe_all "const char *dev"
.Ft char *
-.Fn fsid_volname "const char *dev" "const char *fs_type"
+.Fn fsid_volname "const char *dev" "const char *fs_name"
.Ft char *
.Fn fsid_volname_all "const char *dev"
.Ft char *
-.Fn fsid_fsname "int id"
+.Fn fsid_fsname "fsid_t id"
.Ft int
.Fn fsid_fs_count "void"
.Sh DESCRIPTION
-.Pp
.\" General description
-The
+The
.Lb libfsid
provides an interface to determine several filesystems
and to get their volume labels.
.Pp
-Big part of the functions use the first parameter
+Most functions use the parameter
.Fa dev
-as the full path of the device, some of them has additionally an
-.Fa fs_type
-parameter wich is the name of the choosen filesystem. It can be:
-.Pp
-.Bd -literal -offset indent -compact
-HAMMER HAMMER
-UFS UFS
-CD9660 ISO 9660
-EXT2 Extended 2
-MSDOSFS FAT32, FAT16
-.Ed
-.Pp
+for specifying the full path of the device and some of them have an additional
+.Fa fs_name
+parameter wich is the name of the chosen filesystem.
+It can be:
+.Bl -tag -width ".Li MSDOSFS" -offset indent
+.It Li HAMMER
+A
+.Xr HAMMER 5
+filesystem
+.It Li UFS
+A
+.Xr UFS 5
+filesystem
+.It Li CD9660
+An ISO 9660 filesystem
+.It Li EXT2
+Linux' second extended filesystem
+.It Li MSDOSFS
+An
+.Xr msdos 5
+filesystem
+.El
.Pp
.\" fsid_probe function
The
.Fn fsid_probe
-returns 1 if the device has the filesystem type as expected by the
-.Fa fs_type
-parameter, otherwise 0.
+function returns one of
+.Dv FSID_CD9660 ,
+.Dv FSID_EXT2 ,
+.Dv FSID_HAMMER
+or
+.Dv FSID_UFS
+if the device has the filesystem type as expected by the
+.Fa fs_name
+parameter, otherwise
+.Dv FSID_UNKNOWN .
.Pp
.\" fsid_probe_all function
+The
.Fn fsid_probe_all
-function try to identify the filesystem type, it returns 0 if the filesystem unknown, or the check fails. Otherwise the returned value is one of this macros:
-.Pp
-.Bd -literal -offset indent -compact
-FSID_HAMMER HAMMER
-FSID_UFS UFS
-FSID_CD9660 ISO 9660
-FSID_EXT2 Extended 2
-FSID_MSDOSFS FAT32, FAT16
-.Ed
+function tries to identify the filesystem type.
+It returns
+.Dv FSID_UNKNOWN
+if the filesystem is unknown, or the check fails.
+Otherwise the returned value is one of:
+.Bl -tag -width ".Dv FSID_MSDOSFS" -offset indent
+.It Dv FSID_HAMMER
+A
+.Xr HAMMER 5
+filesystem
+.It Dv FSID_UFS
+A
+.Xr UFS 5
+filesystem
+.It Dv FSID_CD9660
+An ISO 9660 filesystem
+.It Dv FSID_EXT2
+Linux' second extended filesystem
+.It Dv FSID_MSDOSFS
+An
+.Xr msdos 5
+filesystem
+.El
.Pp
-The name of the filesystem can easily queried with the
-.Fn fsid_fsname .
+The name of the filesystem can be easily queried with the
+.Fn fsid_fsname
+function.
.Pp
+The
.Fn fsid_volname
-function returns with the volume label, if the filesystem is the same as the
-.Fa fs_type ,
-if not just returns with
+function returns the volume label, if the filesystem name is the same
+as specified by
+.Fa fs_name .
+If not, it returns
.Dv NULL .
.Pp
+The
.Fn fsid_volname_all
-give back the volume label, if the filesystem known otherwise returns with
+function returns the volume label, if the filesystem is known.
+Otherwise it returns
.Dv NULL .
.Pp
+The
.Fn fsid_fsname
-give back the name of the filesystem as a pointer to string. The
+function returns the name of the filesystem as a pointer to string.
+The
.Fa id
-is mostly the return value of the
-.Fn fsid_probe_all .
+is usually the return value of a
+.Fn fsid_probe_all
+call.
.Pp
+The
.Fn fsid_fs_count
-return with a number of supported filesystems.
-.Sh AUTHOR
-The library and the manual page wrote by Ákos Kovács <akoskovacs@gmx.com>.
+function returns the number of supported filesystems.
.Sh SEE ALSO
-.Xr devattr 3
+.Xr devattr 3
+.Sh AUTHOR
+.An -nosplit
+The library and the manual page were written by
+.An Akos Kovacs
+.Aq akoskovacs@gmx.com .
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
-#include "libfsid.h"
+
#include <sys/stat.h>
+
#include <errno.h>
-struct fs_type fs_types[] = {
+#include "libfsid.h"
+
+static struct fs_type fs_types[] = {
{ "HAMMER", hammer_probe, hammer_volname },
{ "UFS", ufs_probe, ufs_volname },
{ "CD9660", cd9660_probe, cd9660_volname },
};
const char *
-fsid_fsname(int id)
+fsid_fsname(fsid_t id)
{
if (id < 1)
return 0;
fsid_fs_count(void)
{
int count;
+
for (count = 0; fs_types[count].fs_name != NULL; count++)
- ;
+ ; /* nothing */
return count;
}
-int
+fsid_t
fsid_probe(const char *dev, const char *fs_type)
{
int i;
+
if (dev == NULL || fs_type == NULL)
- return 0;
+ return FSID_UNKNOWN;
for (i = 0; fs_types[i].fs_name != NULL; i++) {
if ((strcmp(fs_type, fs_types[i].fs_name)) == 0)
return fs_types[i].fs_probe(dev);
}
- return 0;
+ return FSID_UNKNOWN;
}
-int
+fsid_t
fsid_probe_all(const char *dev)
{
int i;
+ fsid_t ret;
+
if (dev == NULL)
- return 0;
+ return FSID_UNKNOWN;
for (i = 0; fs_types[i].fs_name != NULL; i++) {
- if ((fs_types[i].fs_probe(dev)) == 1)
- return i + 1;
+ if ((ret = fs_types[i].fs_probe(dev)) != FSID_UNKNOWN)
+ return ret;
}
- return 0;
+ return FSID_UNKNOWN;
}
char *
fsid_volname(const char *dev, const char *fs_type)
{
int i;
+
if (dev == NULL || fs_type == NULL)
return NULL;
fsid_volname_all(const char *dev)
{
int fs_id;
+
if (dev == NULL)
return NULL;
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
+
#ifndef LIBFSID_H
#define LIBFSID_H
#include <sys/types.h>
#include <sys/uio.h>
-#include <stdlib.h>
-#include <stdio.h>
+
#include <err.h>
#include <fcntl.h>
-#include <unistd.h>
+#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
+#include <unistd.h>
-#define FSID_HAMMER 0x01
-#define FSID_UFS 0x02
-#define FSID_CD9660 0x03
-#define FSID_EXT2 0x04
-#define FSID_MSDOSFS 0x05
+typedef enum {
+ FSID_UNKNOWN,
+ FSID_HAMMER,
+ FSID_UFS,
+ FSID_CD9660,
+ FSID_EXT2,
+ FSID_MSDOSFS
+} fsid_t;
-typedef int (probe_func_t)(const char *);
+typedef fsid_t (probe_func_t)(const char *);
typedef char *(volname_func_t)(const char *);
struct fs_type {
- const char *fs_name;
+ const char *fs_name;
probe_func_t *fs_probe;
volname_func_t *fs_volname;
};
volname_func_t ext2_volname;
volname_func_t msdosfs_volname;
-int fsid_probe(const char *dev, const char *fs_type);
-int fsid_probe_all(const char *dev);
+fsid_t fsid_probe(const char *dev, const char *fs_name);
+fsid_t fsid_probe_all(const char *dev);
-char *fsid_volname(const char *dev, const char *fs_type);
+char *fsid_volname(const char *dev, const char *fs_name);
char *fsid_volname_all(const char *dev);
/* Extra functions */
-const char *fsid_fsname(int);
+const char *fsid_fsname(fsid_t);
int fsid_fs_count(void);
-
#ifdef _FSID_INTERNAL
int fsid_dev_read(const char *dev, off_t off, size_t len, char *buf);
#endif
+
#endif /* LIBFSID_H */
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
+
#include "libfsid.h"
+
#include <vfs/msdosfs/bootsect.h>
#define MSDOS_BOOT_BLOCK_SIZE 512
-static char buffer[MSDOS_BOOT_BLOCK_SIZE * 4];
static char *get_volname(char *buf);
+static char buffer[MSDOS_BOOT_BLOCK_SIZE * 4];
-int
+fsid_t
msdosfs_probe(const char *dev)
{
if (fsid_dev_read(dev, 0L, sizeof(buffer), buffer) != 0)
- return 0;
+ return FSID_UNKNOWN;
if (get_volname(buffer) != NULL)
- return 1;
- else
- return 0;
+ return FSID_MSDOSFS;
+
+ return FSID_MSDOSFS;
}
char *
msdosfs_volname(const char *dev)
return NULL;
volname = get_volname(buffer);
- if (volname == NULL)
- return NULL;
- if (volname[0] == '\0')
+ if (volname == NULL || volname[0] == '\0')
return NULL;
- volname[10] = '\0';
+ volname[sizeof(volname) - 1] = '\0';
+
return volname;
}
* If this is not a BPB v7, try it as a bpb v4.
*/
extb = (struct extboot *)bpb4->bsExt;
- if ((extb->exBootSignature == 0x28 || extb->exBootSignature == 0x29) &&
+ if ((extb->exBootSignature == EXBOOTSIG ||
+ extb->exBootSignature == EXBOOTSIG2) &&
strncmp(extb->exFileSysType, "FAT", 3) == 0)
return extb->exVolumeLabel;
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
+
#include "libfsid.h"
+
#include <vfs/ufs/ufs_types.h>
#include <vfs/ufs/fs.h>
static char buffer[MAXBSIZE];
-int
+fsid_t
ufs_probe(const char *dev)
{
static struct fs *fs;
if(fsid_dev_read(dev, SBOFF, sizeof(buffer), buffer) != 0)
- return 0;
+ return FSID_UNKNOWN;
fs = (struct fs *)&buffer;
if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
(unsigned)fs->fs_bsize < sizeof(struct fs)) {
- return 0;
+ return FSID_UNKNOWN;
}
- return 1;
+ return FSID_UFS;
}
char *
fs = (struct fs *)&buffer;
if (fs->fs_magic != FS_MAGIC || fs->fs_bsize > MAXBSIZE ||
- (unsigned)fs->fs_bsize < sizeof(struct fs)) {
+ (unsigned)fs->fs_bsize < sizeof(struct fs) ||
+ fs->fs_volname[0] == '\0') {
return NULL;
}
- if (fs->fs_volname[0] == '\0')
- return NULL;
-
fs->fs_volname[MAXVOLLEN - 1] = '\0';
+
return fs->fs_volname;
}