Imported Upstream version 2.6.0
[debian/amanda] / common-src / file.h
1 /*
2  * Amanda, The Advanced Maryland Automatic Network Disk Archiver
3  * Copyright (c) 1997-1998 University of Maryland at College Park
4  * All Rights Reserved.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of U.M. not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  U.M. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * U.M. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL U.M.
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author: AMANDA core development group.
24  */
25
26 extern int    mkpdir(char *file, mode_t mode, uid_t uid, gid_t gid);
27 extern int    rmpdir(char *file, char *topdir);
28
29 /* Given a pathname, convert it to "canonical form" for this system.  Currently,
30  * this means nothing on POSIX, but means substituting /cygdrive, etc. on Cygwin.
31  *
32  * @param pathname: the pathname to canonicalize
33  * @param result_buf (output): the canonicalize pathname; this should be a buffer of
34  * at least PATH_MAX bytes.
35  */
36 void canonicalize_pathname(char *pathname, char *result_buf);
37
38 extern char  *sanitise_filename(char *inp);
39 char  *old_sanitise_filename(char *inp);
40 void    safe_fd(int fd_start, int fd_count);
41 void    safe_cd(void);
42 void    save_core(void);
43
44 /* Get the uid of CLIENT_LOGIN, or -1 if it doesn't exist.  Note that, if
45  * only running a server, CLIENT_LOGIN may legitimately not exist.
46  *
47  * @returns: userid, or -1 if invalid
48  */
49 uid_t get_client_uid(void);
50
51 /* Get the gid of CLIENT_LOGIN, or -1 if it doesn't exist.  Note that, if
52  * only running a server, CLIENT_LOGIN may legitimately not exist.
53  *
54  * @returns: groupid, or -1 if invalid
55  */
56 gid_t get_client_gid(void);
57
58 extern /*@only@*/ /*@null@*/ char *debug_agets(const char *c, int l, FILE *file);
59 extern /*@only@*/ /*@null@*/ char *debug_areads(const char *c, int l, int fd);
60 #define agets(f)              debug_agets(__FILE__,__LINE__,(f))
61 #define areads(f)             debug_areads(__FILE__,__LINE__,(f))
62
63 ssize_t areads_dataready(int fd);
64 void    areads_relbuf(int fd);
65
66 /*
67  * "Safe" close macros.  Close the object then set it to a value that
68  * will cause an error if referenced.
69  *
70  * aclose(fd) -- close a file descriptor and set it to -1.
71  * afclose(f) -- close a stdio file and set it to NULL.
72  * apclose(p) -- close a stdio pipe file and set it to NULL.
73  *
74  * Note: be careful not to do the following:
75  *
76  *  for(fd = low; fd < high; fd++) {
77  *      aclose(fd);
78  *  }
79  *
80  * Since aclose() sets the argument to -1, this will loop forever.
81  * Just copy fd to a temp variable and use that with aclose().
82  *
83  * Aclose() interacts with areads() to inform it to release any buffer
84  * it has outstanding on the file descriptor.
85  */
86
87 #define aclose(fd) do {                                                 \
88     if((fd) >= 0) {                                                     \
89         close(fd);                                                      \
90         areads_relbuf(fd);                                              \
91     }                                                                   \
92     (fd) = -1;                                                          \
93 } while(0)
94
95 #define afclose(f) do {                                                 \
96     if((f) != NULL) {                                                   \
97         fclose(f);                                                      \
98     }                                                                   \
99     (f) = NULL;                                                         \
100 } while(0)
101
102 #define apclose(p) do {                                                 \
103     if((p) != NULL) {                                                   \
104         pclose(p);                                                      \
105     }                                                                   \
106     (p) = NULL;                                                         \
107 } while(0)
108
109
110 /* Calls system open(), but takes care of interrupted system calls and
111  * clears the close-on-exec bit. In the failure case, errno is
112  * retained from the final call to open(). */
113 extern int robust_open(const char * pathname, int flags, mode_t mode);
114
115 /* Same idea but for close. */
116 extern int robust_close(int fd);
117
118 /* Get the original working directory, at application startup
119  *
120  * @returns: pointer to statically allocated string
121  */
122 char *get_original_cwd(void);