lib: Make create_synthetic_smb_fname use synthetic_smb_fname
[amitay/samba.git] / source3 / lib / filename_util.c
index 85d9fb5a868299490566c6a6756089efd3196317..e4900eb10d155d5ccd306b1b7101298ee380bd04 100644 (file)
 */
 #include "includes.h"
 
+static NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
+                                 const struct smb_filename *smb_fname_in,
+                                 struct smb_filename **smb_fname_out);
+
 /**
  * XXX: This is temporary and there should be no callers of this outside of
  * this file once smb_filename is plumbed through all path based operations.
@@ -55,9 +59,20 @@ NTSTATUS create_synthetic_smb_fname(TALLOC_CTX *ctx, const char *base_name,
                                    const SMB_STRUCT_STAT *psbuf,
                                    struct smb_filename **smb_fname_out)
 {
-       struct smb_filename smb_fname_loc;
+       *smb_fname_out = synthetic_smb_fname(ctx, base_name, stream_name,
+                                            psbuf);
+       if (*smb_fname_out == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
+       return NT_STATUS_OK;
+}
 
-       ZERO_STRUCT(smb_fname_loc);
+struct smb_filename *synthetic_smb_fname(TALLOC_CTX *mem_ctx,
+                                        const char *base_name,
+                                        const char *stream_name,
+                                        const SMB_STRUCT_STAT *psbuf)
+{
+       struct smb_filename smb_fname_loc = { 0, };
 
        /* Setup the base_name/stream_name. */
        smb_fname_loc.base_name = discard_const_p(char, base_name);
@@ -67,8 +82,8 @@ NTSTATUS create_synthetic_smb_fname(TALLOC_CTX *ctx, const char *base_name,
        if (psbuf)
                smb_fname_loc.st = *psbuf;
 
-       /* Let copy_smb_filename() do the heavy lifting. */
-       return copy_smb_filename(ctx, &smb_fname_loc, smb_fname_out);
+       /* Let cp_smb_filename() do the heavy lifting. */
+       return cp_smb_filename(mem_ctx, &smb_fname_loc);
 }
 
 /**
@@ -125,55 +140,90 @@ const char *smb_fname_str_dbg(const struct smb_filename *smb_fname)
 }
 
 /**
- * Return a debug string using the talloc_tos().  This can only be called from
- * DEBUG() macros due to the debut_ctx().
+ * Return a debug string of the path name of an fsp using the talloc_tos().
  */
 const char *fsp_str_dbg(const struct files_struct *fsp)
 {
        return smb_fname_str_dbg(fsp->fsp_name);
 }
 
-NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
-                          const struct smb_filename *smb_fname_in,
-                          struct smb_filename **smb_fname_out)
+/**
+ * Create a debug string for the fnum of an fsp.
+ *
+ * This is allocated to talloc_tos() or a string constant
+ * in certain corner cases. The returned string should
+ * hence not be free'd directly but only via the talloc stack.
+ */
+const char *fsp_fnum_dbg(const struct files_struct *fsp)
 {
-       /* stream_name must always be NULL if there is no stream. */
-       if (smb_fname_in->stream_name) {
-               SMB_ASSERT(smb_fname_in->stream_name[0] != '\0');
+       char *str;
+
+       if (fsp == NULL) {
+               return "fnum [fsp is NULL]";
        }
 
-       *smb_fname_out = talloc_zero(ctx, struct smb_filename);
-       if (*smb_fname_out == NULL) {
-               return NT_STATUS_NO_MEMORY;
+       if (fsp->fnum == FNUM_FIELD_INVALID) {
+               return "fnum [invalid value]";
        }
 
-       if (smb_fname_in->base_name) {
-               (*smb_fname_out)->base_name =
-                   talloc_strdup(*smb_fname_out, smb_fname_in->base_name);
-               if (!(*smb_fname_out)->base_name)
-                       goto no_mem_err;
+       str = talloc_asprintf(talloc_tos(), "fnum %llu",
+                             (unsigned long long)fsp->fnum);
+       if (str == NULL) {
+               DEBUG(1, ("%s: talloc_asprintf failed\n", __FUNCTION__));
+               return "fnum [talloc failed!]";
        }
 
-       if (smb_fname_in->stream_name) {
-               (*smb_fname_out)->stream_name =
-                   talloc_strdup(*smb_fname_out, smb_fname_in->stream_name);
-               if (!(*smb_fname_out)->stream_name)
-                       goto no_mem_err;
+       return str;
+}
+
+struct smb_filename *cp_smb_filename(TALLOC_CTX *mem_ctx,
+                                    const struct smb_filename *in)
+{
+       struct smb_filename *out;
+
+       /* stream_name must always be NULL if there is no stream. */
+       if (in->stream_name) {
+               SMB_ASSERT(in->stream_name[0] != '\0');
        }
 
-       if (smb_fname_in->original_lcomp) {
-               (*smb_fname_out)->original_lcomp =
-                   talloc_strdup(*smb_fname_out, smb_fname_in->original_lcomp);
-               if (!(*smb_fname_out)->original_lcomp)
-                       goto no_mem_err;
+       out = talloc_zero(mem_ctx, struct smb_filename);
+       if (out == NULL) {
+               return NULL;
+       }
+       if (in->base_name != NULL) {
+               out->base_name = talloc_strdup(out, in->base_name);
+               if (out->base_name == NULL) {
+                       goto fail;
+               }
+       }
+       if (in->stream_name != NULL) {
+               out->stream_name = talloc_strdup(out, in->stream_name);
+               if (out->stream_name == NULL) {
+                       goto fail;
+               }
+       }
+       if (in->original_lcomp != NULL) {
+               out->original_lcomp = talloc_strdup(out, in->original_lcomp);
+               if (out->original_lcomp == NULL) {
+                       goto fail;
+               }
        }
+       out->st = in->st;
+       return out;
+fail:
+       TALLOC_FREE(out);
+       return NULL;
+}
 
-       (*smb_fname_out)->st = smb_fname_in->st;
+static NTSTATUS copy_smb_filename(TALLOC_CTX *ctx,
+                                 const struct smb_filename *smb_fname_in,
+                                 struct smb_filename **smb_fname_out)
+{
+       *smb_fname_out = cp_smb_filename(ctx, smb_fname_in);
+       if (*smb_fname_out == NULL) {
+               return NT_STATUS_NO_MEMORY;
+       }
        return NT_STATUS_OK;
-
- no_mem_err:
-       TALLOC_FREE(*smb_fname_out);
-       return NT_STATUS_NO_MEMORY;
 }
 
 /****************************************************************************