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