2 Unix SMB/CIFS implementation.
3 Filename utility functions.
4 Copyright (C) Tim Prouty 2009
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.
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.
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/>.
21 static NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
22 const struct smb_filename *smb_fname_in,
23 struct smb_filename **smb_fname_out);
26 * XXX: This is temporary and there should be no callers of this outside of
27 * this file once smb_filename is plumbed through all path based operations.
28 * The one legitimate caller currently is smb_fname_str_dbg(), which this
29 * could be made static for.
31 NTSTATUS get_full_smb_filename(TALLOC_CTX *ctx,
32 const struct smb_filename *smb_fname,
35 if (smb_fname->stream_name) {
36 /* stream_name must always be NULL if there is no stream. */
37 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
39 *full_name = talloc_asprintf(ctx, "%s%s", smb_fname->base_name,
40 smb_fname->stream_name);
42 *full_name = talloc_strdup(ctx, smb_fname->base_name);
46 return NT_STATUS_NO_MEMORY;
53 * There are actually legitimate callers of this such as functions that
54 * enumerate streams using the vfs_streaminfo interface and then want to
55 * operate on each stream.
57 NTSTATUS create_synthetic_smb_fname(TALLOC_CTX *ctx, const char *base_name,
58 const char *stream_name,
59 const SMB_STRUCT_STAT *psbuf,
60 struct smb_filename **smb_fname_out)
62 *smb_fname_out = synthetic_smb_fname(ctx, base_name, stream_name,
64 if (*smb_fname_out == NULL) {
65 return NT_STATUS_NO_MEMORY;
70 struct smb_filename *synthetic_smb_fname(TALLOC_CTX *mem_ctx,
71 const char *base_name,
72 const char *stream_name,
73 const SMB_STRUCT_STAT *psbuf)
75 struct smb_filename smb_fname_loc = { 0, };
77 /* Setup the base_name/stream_name. */
78 smb_fname_loc.base_name = discard_const_p(char, base_name);
79 smb_fname_loc.stream_name = discard_const_p(char, stream_name);
81 /* Copy the psbuf if one was given. */
83 smb_fname_loc.st = *psbuf;
85 /* Let cp_smb_filename() do the heavy lifting. */
86 return cp_smb_filename(mem_ctx, &smb_fname_loc);
90 * XXX: This is temporary and there should be no callers of this once
91 * smb_filename is plumbed through all path based operations.
93 NTSTATUS create_synthetic_smb_fname_split(TALLOC_CTX *ctx,
95 const SMB_STRUCT_STAT *psbuf,
96 struct smb_filename **smb_fname_out)
99 const char *stream_name = NULL;
100 char *base_name = NULL;
102 if (!lp_posix_pathnames()) {
103 stream_name = strchr_m(fname, ':');
106 /* Setup the base_name/stream_name. */
108 base_name = talloc_strndup(ctx, fname,
109 PTR_DIFF(stream_name, fname));
111 base_name = talloc_strdup(ctx, fname);
115 return NT_STATUS_NO_MEMORY;
118 status = create_synthetic_smb_fname(ctx, base_name, stream_name, psbuf,
120 TALLOC_FREE(base_name);
125 * Return a string using the talloc_tos()
127 const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
132 if (smb_fname == NULL) {
135 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
136 if (!NT_STATUS_IS_OK(status)) {
143 * Return a debug string of the path name of an fsp using the talloc_tos().
145 const char *fsp_str_dbg(const struct files_struct *fsp)
147 return smb_fname_str_dbg(fsp->fsp_name);
151 * Create a debug string for the fnum of an fsp.
153 * This is allocated to talloc_tos() or a string constant
154 * in certain corner cases. The returned string should
155 * hence not be free'd directly but only via the talloc stack.
157 const char *fsp_fnum_dbg(const struct files_struct *fsp)
162 return "fnum [fsp is NULL]";
165 if (fsp->fnum == FNUM_FIELD_INVALID) {
166 return "fnum [invalid value]";
169 str = talloc_asprintf(talloc_tos(), "fnum %llu",
170 (unsigned long long)fsp->fnum);
172 DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
173 return "fnum [talloc failed!]";
179 struct smb_filename *cp_smb_filename(TALLOC_CTX *mem_ctx,
180 const struct smb_filename *in)
182 struct smb_filename *out;
184 /* stream_name must always be NULL if there is no stream. */
185 if (in->stream_name) {
186 SMB_ASSERT(in->stream_name[0] != '\0');
189 out = talloc_zero(mem_ctx, struct smb_filename);
193 if (in->base_name != NULL) {
194 out->base_name = talloc_strdup(out, in->base_name);
195 if (out->base_name == NULL) {
199 if (in->stream_name != NULL) {
200 out->stream_name = talloc_strdup(out, in->stream_name);
201 if (out->stream_name == NULL) {
205 if (in->original_lcomp != NULL) {
206 out->original_lcomp = talloc_strdup(out, in->original_lcomp);
207 if (out->original_lcomp == NULL) {
218 static NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
219 const struct smb_filename *smb_fname_in,
220 struct smb_filename **smb_fname_out)
222 *smb_fname_out = cp_smb_filename(ctx, smb_fname_in);
223 if (*smb_fname_out == NULL) {
224 return NT_STATUS_NO_MEMORY;
229 /****************************************************************************
230 Simple check to determine if the filename is a stream.
231 ***************************************************************************/
232 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
234 /* stream_name must always be NULL if there is no stream. */
235 if (smb_fname->stream_name) {
236 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
239 if (lp_posix_pathnames()) {
243 return smb_fname->stream_name != NULL;
246 /****************************************************************************
247 Returns true if the filename's stream == "::$DATA"
248 ***************************************************************************/
249 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
251 if (!is_ntfs_stream_smb_fname(smb_fname)) {
255 return strcasecmp_m(smb_fname->stream_name, "::$DATA") == 0;