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 struct smb_filename smb_fname_loc;
64 ZERO_STRUCT(smb_fname_loc);
66 /* Setup the base_name/stream_name. */
67 smb_fname_loc.base_name = discard_const_p(char, base_name);
68 smb_fname_loc.stream_name = discard_const_p(char, stream_name);
70 /* Copy the psbuf if one was given. */
72 smb_fname_loc.st = *psbuf;
74 /* Let copy_smb_filename() do the heavy lifting. */
75 return copy_smb_filename(ctx, &smb_fname_loc, smb_fname_out);
78 struct smb_filename *synthetic_smb_fname(TALLOC_CTX *mem_ctx,
79 const char *base_name,
80 const char *stream_name,
81 const SMB_STRUCT_STAT *psbuf)
83 struct smb_filename smb_fname_loc = { 0, };
85 /* Setup the base_name/stream_name. */
86 smb_fname_loc.base_name = discard_const_p(char, base_name);
87 smb_fname_loc.stream_name = discard_const_p(char, stream_name);
89 /* Copy the psbuf if one was given. */
91 smb_fname_loc.st = *psbuf;
93 /* Let copy_smb_filename() do the heavy lifting. */
94 return cp_smb_filename(mem_ctx, &smb_fname_loc);
98 * XXX: This is temporary and there should be no callers of this once
99 * smb_filename is plumbed through all path based operations.
101 NTSTATUS create_synthetic_smb_fname_split(TALLOC_CTX *ctx,
103 const SMB_STRUCT_STAT *psbuf,
104 struct smb_filename **smb_fname_out)
107 const char *stream_name = NULL;
108 char *base_name = NULL;
110 if (!lp_posix_pathnames()) {
111 stream_name = strchr_m(fname, ':');
114 /* Setup the base_name/stream_name. */
116 base_name = talloc_strndup(ctx, fname,
117 PTR_DIFF(stream_name, fname));
119 base_name = talloc_strdup(ctx, fname);
123 return NT_STATUS_NO_MEMORY;
126 status = create_synthetic_smb_fname(ctx, base_name, stream_name, psbuf,
128 TALLOC_FREE(base_name);
133 * Return a string using the talloc_tos()
135 const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
140 if (smb_fname == NULL) {
143 status = get_full_smb_filename(talloc_tos(), smb_fname, &fname);
144 if (!NT_STATUS_IS_OK(status)) {
151 * Return a debug string of the path name of an fsp using the talloc_tos().
153 const char *fsp_str_dbg(const struct files_struct *fsp)
155 return smb_fname_str_dbg(fsp->fsp_name);
159 * Create a debug string for the fnum of an fsp.
161 * This is allocated to talloc_tos() or a string constant
162 * in certain corner cases. The returned string should
163 * hence not be free'd directly but only via the talloc stack.
165 const char *fsp_fnum_dbg(const struct files_struct *fsp)
170 return "fnum [fsp is NULL]";
173 if (fsp->fnum == FNUM_FIELD_INVALID) {
174 return "fnum [invalid value]";
177 str = talloc_asprintf(talloc_tos(), "fnum %llu",
178 (unsigned long long)fsp->fnum);
180 DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
181 return "fnum [talloc failed!]";
187 struct smb_filename *cp_smb_filename(TALLOC_CTX *mem_ctx,
188 const struct smb_filename *in)
190 struct smb_filename *out;
192 /* stream_name must always be NULL if there is no stream. */
193 if (in->stream_name) {
194 SMB_ASSERT(in->stream_name[0] != '\0');
197 out = talloc_zero(mem_ctx, struct smb_filename);
201 if (in->base_name != NULL) {
202 out->base_name = talloc_strdup(out, in->base_name);
203 if (out->base_name == NULL) {
207 if (in->stream_name != NULL) {
208 out->stream_name = talloc_strdup(out, in->stream_name);
209 if (out->stream_name == NULL) {
213 if (in->original_lcomp != NULL) {
214 out->original_lcomp = talloc_strdup(out, in->original_lcomp);
215 if (out->original_lcomp == NULL) {
226 static NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
227 const struct smb_filename *smb_fname_in,
228 struct smb_filename **smb_fname_out)
230 *smb_fname_out = cp_smb_filename(ctx, smb_fname_in);
231 if (*smb_fname_out == NULL) {
232 return NT_STATUS_NO_MEMORY;
237 /****************************************************************************
238 Simple check to determine if the filename is a stream.
239 ***************************************************************************/
240 bool is_ntfs_stream_smb_fname(const struct smb_filename *smb_fname)
242 /* stream_name must always be NULL if there is no stream. */
243 if (smb_fname->stream_name) {
244 SMB_ASSERT(smb_fname->stream_name[0] != '\0');
247 if (lp_posix_pathnames()) {
251 return smb_fname->stream_name != NULL;
254 /****************************************************************************
255 Returns true if the filename's stream == "::$DATA"
256 ***************************************************************************/
257 bool is_ntfs_default_stream_smb_fname(const struct smb_filename *smb_fname)
259 if (!is_ntfs_stream_smb_fname(smb_fname)) {
263 return strcasecmp_m(smb_fname->stream_name, "::$DATA") == 0;