summaryrefslogtreecommitdiffstats
path: root/src/global/file_id.c
blob: b373056fe542d47ac3daeb99b6040ebd6633c839 (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
/*++
/* NAME
/*	file_id 3
/* SUMMARY
/*	file ID printable representation
/* SYNOPSIS
/*	#include <file_id.h>
/*
/*	const char *get_file_id_fd(fd, long_flag)
/*	int	fd;
/*	int	long_flag;
/*
/*	const char *get_file_id_st(st, long_flag)
/*	struct stat *st;
/*	int	long_flag;
/*
/*	const char *get_file_id(fd)
/*	int	fd;
/* DESCRIPTION
/*	get_file_id_fd() queries the operating system for the unique
/*	file identifier for the specified file descriptor and returns
/*	a printable representation.  The result is volatile.  Make
/*	a copy if it is to be used for any appreciable amount of
/*	time.
/*
/*	get_file_id_st() returns the unique identifier for the
/*	specified file status information.
/*
/*	get_file_id() provides binary compatibility for old programs.
/*	This function should not be used by new programs.
/*
/*	Arguments:
/* .IP fd
/*	A valid file descriptor that is associated with an open file.
/* .IP st
/*	The result from e.g., stat(2) or fstat(2).
/* .IP long_flag
/*	Encode the result as appropriate for long or short queue
/*	identifiers.
/* DIAGNOSTICS
/*	All errors are fatal.
/* LICENSE
/* .ad
/* .fi
/*	The Secure Mailer license must be distributed with this software.
/* AUTHOR(S)
/*	Wietse Venema
/*	IBM T.J. Watson Research
/*	P.O. Box 704
/*	Yorktown Heights, NY 10598, USA
/*--*/

/* System library. */

#include <sys_defs.h>
#include <sys/stat.h>
#include <string.h>

/* Utility library */

#include <msg.h>
#include <vstring.h>
#include <warn_stat.h>

/* Global library. */

#define MAIL_QUEUE_INTERNAL
#include <mail_queue.h>
#include "file_id.h"

/* get_file_id - binary compatibility */

const char *get_file_id(int fd)
{
    return (get_file_id_fd(fd, 0));
}

/* get_file_id_fd - return printable file identifier for file descriptor */

const char *get_file_id_fd(int fd, int long_flag)
{
    struct stat st;

    if (fstat(fd, &st) < 0)
	msg_fatal("fstat: %m");
    return (get_file_id_st(&st, long_flag));
}

/* get_file_id_st - return printable file identifier for file status */

const char *get_file_id_st(struct stat * st, int long_flag)
{
    static VSTRING *result;

    if (result == 0)
	result = vstring_alloc(1);
    if (long_flag)
	return (MQID_LG_ENCODE_INUM(result, st->st_ino));
    else
	return (MQID_SH_ENCODE_INUM(result, st->st_ino));
}