s3:lib: add a utility function "fsp_fnum_dbg" for logging the fnum of an fsp
[kai/samba.git] / source3 / lib / filename_util.c
1 /*
2    Unix SMB/CIFS implementation.
3    Filename utility functions.
4    Copyright (C) Tim Prouty 2009
5
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 3 of the License, or
9    (at your option) any later version.
10
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 */
19 #include "includes.h"
20
21 /**
22  * XXX: This is temporary and there should be no callers of this outside of
23  * this file once smb_filename is plumbed through all path based operations.
24  * The one legitimate caller currently is smb_fname_str_dbg(), which this
25  * could be made static for.
26  */
27 NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx,
28                                const struct smb_filename *smb_fname,
29                                char **full_name)
30 {
31         if (smb_fname->stream_name) {
32                 /* stream_name must always be NULL if there is no stream. */
33                 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
34
35                 *full_name = talloc_asprintf(ctx, "%s%s", smb_fname->base_name,
36                                              smb_fname->stream_name);
37         } else {
38                 *full_name = talloc_strdup(ctx, smb_fname->base_name);
39         }
40
41         if (!*full_name) {
42                 return NT_STATUS_NO_MEMORY;
43         }
44
45         return NT_STATUS_OK;
46 }
47
48 /**
49  * There are actually legitimate callers of this such as functions that
50  * enumerate streams using the vfs_streaminfo interface and then want to
51  * operate on each stream.
52  */
53 NTSTATUS create_synthetic_smb_fname(TALLOC_CTX *ctx, const char *base_name,
54                                     const char *stream_name,
55                                     const SMB_STRUCT_STAT *psbuf,
56                                     struct smb_filename **smb_fname_out)
57 {
58         struct smb_filename smb_fname_loc;
59
60         ZERO_STRUCT(smb_fname_loc);
61
62         /* Setup the base_name/stream_name. */
63         smb_fname_loc.base_name = discard_const_p(char, base_name);
64         smb_fname_loc.stream_name = discard_const_p(char, stream_name);
65
66         /* Copy the psbuf if one was given. */
67         if (psbuf)
68                 smb_fname_loc.st = *psbuf;
69
70         /* Let copy_smb_filename() do the heavy lifting. */
71         return copy_smb_filename(ctx, &smb_fname_loc, smb_fname_out);
72 }
73
74 /**
75  * XXX: This is temporary and there should be no callers of this once
76  * smb_filename is plumbed through all path based operations.
77  */
78 NTSTATUS create_synthetic_smb_fname_split(TALLOC_CTX *ctx,
79                                           const char *fname,
80                                           const SMB_STRUCT_STAT *psbuf,
81                                           struct smb_filename **smb_fname_out)
82 {
83         NTSTATUS status;
84         const char *stream_name = NULL;
85         char *base_name = NULL;
86
87         if (!lp_posix_pathnames()) {
88                 stream_name = strchr_m(fname, ':');
89         }
90
91         /* Setup the base_name/stream_name. */
92         if (stream_name) {
93                 base_name = talloc_strndup(ctx, fname,
94                                            PTR_DIFF(stream_name, fname));
95         } else {
96                 base_name = talloc_strdup(ctx, fname);
97         }
98
99         if (!base_name) {
100                 return NT_STATUS_NO_MEMORY;
101         }
102
103         status = create_synthetic_smb_fname(ctx, base_name, stream_name, psbuf,
104                                             smb_fname_out);
105         TALLOC_FREE(base_name);
106         return status;
107 }
108
109 /**
110  * Return a string using the talloc_tos()
111  */
112 const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
113 {
114         char *fname = NULL;
115         NTSTATUS status;
116
117         if (smb_fname == NULL) {
118                 return "";
119         }
120         status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
121         if (!NT_STATUS_IS_OK(status)) {
122                 return "";
123         }
124         return fname;
125 }
126
127 /**
128  * Return a debug string of the path name of an fsp using the talloc_tos().
129  */
130 const char *fsp_str_dbg(const struct files_struct *fsp)
131 {
132         return smb_fname_str_dbg(fsp->fsp_name);
133 }
134
135 /**
136  * Create a debug string for the fnum of an fsp.
137  *
138  * This is allocated to talloc_tos() or a string constant
139  * in certain corner cases. The returned string should
140  * hence not be free'd directly but only via the talloc stack.
141  */
142 const char *fsp_fnum_dbg(const struct files_struct *fsp)
143 {
144         char *str;
145
146         if (fsp == NULL) {
147                 return "fnum [fsp is NULL]";
148         }
149
150         if (fsp->fnum == FNUM_FIELD_INVALID) {
151                 return "fnum [invalid value]";
152         }
153
154         str = talloc_asprintf(talloc_tos(), "fnum %d", fsp->fnum);
155         if (str == NULL) {
156                 DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
157                 return "fnum [talloc failed!]";
158         }
159
160         return str;
161 }
162
163 NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
164                            const struct smb_filename *smb_fname_in,
165                            struct smb_filename **smb_fname_out)
166 {
167         /* stream_name must always be NULL if there is no stream. */
168         if (smb_fname_in->stream_name) {
169                 SMB_ASSERT(smb_fname_in->stream_name[0] != '\0');
170         }
171
172         *smb_fname_out = talloc_zero(ctx, struct smb_filename);
173         if (*smb_fname_out == NULL) {
174                 return NT_STATUS_NO_MEMORY;
175         }
176
177         if (smb_fname_in->base_name) {
178                 (*smb_fname_out)->base_name =
179                     talloc_strdup(*smb_fname_out, smb_fname_in->base_name);
180                 if (!(*smb_fname_out)->base_name)
181                         goto no_mem_err;
182         }
183
184         if (smb_fname_in->stream_name) {
185                 (*smb_fname_out)->stream_name =
186                     talloc_strdup(*smb_fname_out, smb_fname_in->stream_name);
187                 if (!(*smb_fname_out)->stream_name)
188                         goto no_mem_err;
189         }
190
191         if (smb_fname_in->original_lcomp) {
192                 (*smb_fname_out)->original_lcomp =
193                     talloc_strdup(*smb_fname_out, smb_fname_in->original_lcomp);
194                 if (!(*smb_fname_out)->original_lcomp)
195                         goto no_mem_err;
196         }
197
198         (*smb_fname_out)->st = smb_fname_in->st;
199         return NT_STATUS_OK;
200
201  no_mem_err:
202         TALLOC_FREE(*smb_fname_out);
203         return NT_STATUS_NO_MEMORY;
204 }
205
206 /****************************************************************************
207  Simple check to determine if the filename is a stream.
208  ***************************************************************************/
209 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
210 {
211         /* stream_name must always be NULL if there is no stream. */
212         if (smb_fname->stream_name) {
213                 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
214         }
215
216         if (lp_posix_pathnames()) {
217                 return false;
218         }
219
220         return smb_fname->stream_name != NULL;
221 }
222
223 /****************************************************************************
224  Returns true if the filename's stream == "::$DATA"
225  ***************************************************************************/
226 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
227 {
228         if (!is_ntfs_stream_smb_fname(smb_fname)) {
229                 return false;
230         }
231
232         return strcasecmp_m(smb_fname->stream_name, "::$DATA") == 0;
233 }