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/>.
20 #include "smbd/smbd.h"
23 * XXX: This is temporary and there should be no callers of this outside of
24 * this file once smb_filename is plumbed through all path based operations.
25 * The one legitimate caller currently is smb_fname_str_dbg(), which this
26 * could be made static for.
28 NTSTATUS
get_full_smb_filename(TALLOC_CTX
*ctx
,
29 const struct smb_filename
*smb_fname
,
32 if (smb_fname
->stream_name
) {
33 /* stream_name must always be NULL if there is no stream. */
34 SMB_ASSERT(smb_fname
->stream_name
[0] != '\0');
36 *full_name
= talloc_asprintf(ctx
, "%s%s", smb_fname
->base_name
,
37 smb_fname
->stream_name
);
39 *full_name
= talloc_strdup(ctx
, smb_fname
->base_name
);
43 return NT_STATUS_NO_MEMORY
;
50 * There are actually legitimate callers of this such as functions that
51 * enumerate streams using the SMB_VFS_STREAMINFO interface and then want to
52 * operate on each stream.
54 NTSTATUS
create_synthetic_smb_fname(TALLOC_CTX
*ctx
, const char *base_name
,
55 const char *stream_name
,
56 const SMB_STRUCT_STAT
*psbuf
,
57 struct smb_filename
**smb_fname_out
)
59 struct smb_filename smb_fname_loc
;
61 ZERO_STRUCT(smb_fname_loc
);
63 /* Setup the base_name/stream_name. */
64 smb_fname_loc
.base_name
= CONST_DISCARD(char *, base_name
);
65 smb_fname_loc
.stream_name
= CONST_DISCARD(char *, stream_name
);
67 /* Copy the psbuf if one was given. */
69 smb_fname_loc
.st
= *psbuf
;
71 /* Let copy_smb_filename() do the heavy lifting. */
72 return copy_smb_filename(ctx
, &smb_fname_loc
, smb_fname_out
);
76 * XXX: This is temporary and there should be no callers of this once
77 * smb_filename is plumbed through all path based operations.
79 NTSTATUS
create_synthetic_smb_fname_split(TALLOC_CTX
*ctx
,
81 const SMB_STRUCT_STAT
*psbuf
,
82 struct smb_filename
**smb_fname_out
)
85 const char *stream_name
= NULL
;
86 char *base_name
= NULL
;
88 if (!lp_posix_pathnames()) {
89 stream_name
= strchr_m(fname
, ':');
92 /* Setup the base_name/stream_name. */
94 base_name
= talloc_strndup(ctx
, fname
,
95 PTR_DIFF(stream_name
, fname
));
97 base_name
= talloc_strdup(ctx
, fname
);
101 return NT_STATUS_NO_MEMORY
;
104 status
= create_synthetic_smb_fname(ctx
, base_name
, stream_name
, psbuf
,
106 TALLOC_FREE(base_name
);
111 * Return a string using the talloc_tos()
113 const char *smb_fname_str_dbg(const struct smb_filename
*smb_fname
)
118 if (smb_fname
== NULL
) {
121 status
= get_full_smb_filename(talloc_tos(), smb_fname
, &fname
);
122 if (!NT_STATUS_IS_OK(status
)) {
129 * Return a debug string using the talloc_tos(). This can only be called from
130 * DEBUG() macros due to the debut_ctx().
132 const char *fsp_str_dbg(const struct files_struct
*fsp
)
134 return smb_fname_str_dbg(fsp
->fsp_name
);
137 NTSTATUS
copy_smb_filename(TALLOC_CTX
*ctx
,
138 const struct smb_filename
*smb_fname_in
,
139 struct smb_filename
**smb_fname_out
)
141 /* stream_name must always be NULL if there is no stream. */
142 if (smb_fname_in
->stream_name
) {
143 SMB_ASSERT(smb_fname_in
->stream_name
[0] != '\0');
146 *smb_fname_out
= talloc_zero(ctx
, struct smb_filename
);
147 if (*smb_fname_out
== NULL
) {
148 return NT_STATUS_NO_MEMORY
;
151 if (smb_fname_in
->base_name
) {
152 (*smb_fname_out
)->base_name
=
153 talloc_strdup(*smb_fname_out
, smb_fname_in
->base_name
);
154 if (!(*smb_fname_out
)->base_name
)
158 if (smb_fname_in
->stream_name
) {
159 (*smb_fname_out
)->stream_name
=
160 talloc_strdup(*smb_fname_out
, smb_fname_in
->stream_name
);
161 if (!(*smb_fname_out
)->stream_name
)
165 if (smb_fname_in
->original_lcomp
) {
166 (*smb_fname_out
)->original_lcomp
=
167 talloc_strdup(*smb_fname_out
, smb_fname_in
->original_lcomp
);
168 if (!(*smb_fname_out
)->original_lcomp
)
172 (*smb_fname_out
)->st
= smb_fname_in
->st
;
176 TALLOC_FREE(*smb_fname_out
);
177 return NT_STATUS_NO_MEMORY
;
180 /****************************************************************************
181 Simple check to determine if the filename is a stream.
182 ***************************************************************************/
183 bool is_ntfs_stream_smb_fname(const struct smb_filename
*smb_fname
)
185 /* stream_name must always be NULL if there is no stream. */
186 if (smb_fname
->stream_name
) {
187 SMB_ASSERT(smb_fname
->stream_name
[0] != '\0');
190 if (lp_posix_pathnames()) {
194 return smb_fname
->stream_name
!= NULL
;
197 /****************************************************************************
198 Returns true if the filename's stream == "::$DATA"
199 ***************************************************************************/
200 bool is_ntfs_default_stream_smb_fname(const struct smb_filename
*smb_fname
)
202 if (!is_ntfs_stream_smb_fname(smb_fname
)) {
206 return StrCaseCmp(smb_fname
->stream_name
, "::$DATA") == 0;