xref: /trafficserver/include/tscore/ink_file.h (revision 774f55f1)
1 /** @file
2 
3   A brief file description
4 
5   @section license License
6 
7   Licensed to the Apache Software Foundation (ASF) under one
8   or more contributor license agreements.  See the NOTICE file
9   distributed with this work for additional information
10   regarding copyright ownership.  The ASF licenses this file
11   to you under the Apache License, Version 2.0 (the
12   "License"); you may not use this file except in compliance
13   with the License.  You may obtain a copy of the License at
14 
15       http://www.apache.org/licenses/LICENSE-2.0
16 
17   Unless required by applicable law or agreed to in writing, software
18   distributed under the License is distributed on an "AS IS" BASIS,
19   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20   See the License for the specific language governing permissions and
21   limitations under the License.
22  */
23 
24 /****************************************************************************
25 
26   ink_file.h
27 
28   File manipulation routines for libts
29 
30  ****************************************************************************/
31 
32 #pragma once
33 
34 #include "tscore/ink_config.h"
35 
36 #include <cstdio>
37 #include <sys/types.h>
38 #include <dirent.h>
39 
40 #if HAVE_SYS_STATFS_H
41 #include <sys/statfs.h>
42 #endif
43 
44 #if HAVE_SYS_STATVFS_H
45 #include <sys/statvfs.h>
46 #endif
47 
48 // Darwin keeps statfs(2) in <sys/mount.h> ...
49 #if HAVE_SYS_MOUNT_H
50 #include <sys/mount.h>
51 #endif
52 
53 /*===========================================================================*
54 
55                             Function Prototypes
56 
57  *===========================================================================*/
58 
59 // Cause ink_filepath_merge to fail if addpath is above rootpath
60 //
61 #define INK_FILEPATH_NOTABOVEROOT 0x01
62 // internal: Only meaningful with INK_FILEPATH_NOTABOVEROOT
63 #define INK_FILEPATH_SECUREROOTTEST 0x02
64 // Cause ink_filepath_merge to fail if addpath is above rootpath,
65 // even given a rootpath /foo/bar and an addpath ../bar/bash
66 //
67 #define INK_FILEPATH_SECUREROOT 0x03
68 // Fail ink_filepath_merge if the merged path is relative
69 #define INK_FILEPATH_NOTRELATIVE 0x04
70 // Fail ink_filepath_merge if the merged path is absolute
71 #define INK_FILEPATH_NOTABSOLUTE 0x08
72 // Return the file system's native path format (e.g. path delimiters
73 // of ':' on MacOS9, '\' on Win32, etc.)
74 #define INK_FILEPATH_NATIVE 0x10
75 // Resolve the true case of existing directories and file elements
76 // of addpath, (resolving any aliases on Win32) and append a proper
77 // trailing slash if a directory
78 //
79 #define INK_FILEPATH_TRUENAME 0x20
80 
81 int ink_fputln(FILE *stream, const char *s);
82 int ink_file_fd_readline(int fd, int bufsize, char *buf);
83 int ink_file_fd_writestring(int fd, const char *buf);
84 int ink_filepath_merge(char *buf, int bufsz, const char *rootpath, const char *addpath, int flags = INK_FILEPATH_TRUENAME);
85 /**
86  Add addpath to the rootpath prepending slash if rootpath
87  is not nullptr and doesn't end with the slash already and put the
88  result into path buffer. If the buffer is too small to hold the
89  resulting string, required size is returned. On success zero is returned
90  */
91 int ink_filepath_make(char *path, int pathsz, const char *rootpath, const char *addpath);
92 
93 /**
94  Resize and zero-fill the given file.
95  Returns 0 on success, errno on failure.
96  */
97 int ink_file_fd_zerofill(int fd, off_t size);
98 
99 /**
100  Return true if the path is a directory.
101  */
102 bool ink_file_is_directory(const char *path);
103 
104 /**
105  Return true if this file type can be mmap(2)'ed.
106  */
107 bool ink_file_is_mmappable(mode_t st_mode);
108 
109 struct ink_device_geometry {
110   uint64_t totalsz; // Total device size in bytes.
111   unsigned blocksz; // Preferred I/O block size.
112   unsigned alignsz; // Block device alignment in bytes. Only relevant with stacked block devices.
113 };
114 
115 bool ink_file_get_geometry(int fd, ink_device_geometry &geometry);
116 
117 // Return the value of pathconf(path, _PC_NAME_MAX), or the closest approximation.
118 size_t ink_file_namemax(const char *path);
119 
120 /**
121  Parses the given string for file permissions
122  Ownership of string parameter is NOT passed
123  Returns an integer bit field on success
124  Returns -1 on failure
125  */
126 int ink_fileperm_parse(const char *perms);
127 
128 // Is the given path "."?
129 static inline bool
isdot(const char * path)130 isdot(const char *path)
131 {
132   return path[0] == '.' && path[1] == '\0';
133 }
134 
135 // Is the given path ".."?
136 static inline bool
isdotdot(const char * path)137 isdotdot(const char *path)
138 {
139   return path[0] == '.' && path[1] == '.' && path[2] == '\0';
140 }
141