1/*++
2/* NAME
3/*	file_id 3
4/* SUMMARY
5/*	file ID printable representation
6/* SYNOPSIS
7/*	#include <file_id.h>
8/*
9/*	const char *get_file_id_fd(fd, long_flag)
10/*	int	fd;
11/*	int	long_flag;
12/*
13/*	const char *get_file_id_st(st, long_flag)
14/*	struct stat *st;
15/*	int	long_flag;
16/*
17/*	const char *get_file_id(fd)
18/*	int	fd;
19/* DESCRIPTION
20/*	get_file_id_fd() queries the operating system for the unique
21/*	file identifier for the specified file descriptor and returns
22/*	a printable representation.  The result is volatile.  Make
23/*	a copy if it is to be used for any appreciable amount of
24/*	time.
25/*
26/*	get_file_id_st() returns the unique identifier for the
27/*	specified file status information.
28/*
29/*	get_file_id() provides binary compatibility for old programs.
30/*	This function should not be used by new programs.
31/*
32/*	Arguments:
33/* .IP fd
34/*	A valid file descriptor that is associated with an open file.
35/* .IP st
36/*	The result from e.g., stat(2) or fstat(2).
37/* .IP long_flag
38/*	Encode the result as appropriate for long or short queue
39/*	identifiers.
40/* DIAGNOSTICS
41/*	All errors are fatal.
42/* LICENSE
43/* .ad
44/* .fi
45/*	The Secure Mailer license must be distributed with this software.
46/* AUTHOR(S)
47/*	Wietse Venema
48/*	IBM T.J. Watson Research
49/*	P.O. Box 704
50/*	Yorktown Heights, NY 10598, USA
51/*--*/
52
53/* System library. */
54
55#include <sys_defs.h>
56#include <sys/stat.h>
57#include <string.h>
58
59/* Utility library */
60
61#include <msg.h>
62#include <vstring.h>
63#include <warn_stat.h>
64
65/* Global library. */
66
67#define MAIL_QUEUE_INTERNAL
68#include <mail_queue.h>
69#include "file_id.h"
70
71/* get_file_id - binary compatibility */
72
73const char *get_file_id(int fd)
74{
75    return (get_file_id_fd(fd, 0));
76}
77
78/* get_file_id_fd - return printable file identifier for file descriptor */
79
80const char *get_file_id_fd(int fd, int long_flag)
81{
82    struct stat st;
83
84    if (fstat(fd, &st) < 0)
85	msg_fatal("fstat: %m");
86    return (get_file_id_st(&st, long_flag));
87}
88
89/* get_file_id_st - return printable file identifier for file status */
90
91const char *get_file_id_st(struct stat * st, int long_flag)
92{
93    static VSTRING *result;
94
95    if (result == 0)
96	result = vstring_alloc(1);
97    if (long_flag)
98	return (MQID_LG_ENCODE_INUM(result, st->st_ino));
99    else
100	return (MQID_SH_ENCODE_INUM(result, st->st_ino));
101}
102