Fix inode generation so nautilus can count total dir size correctly
[samba.git] / source3 / lib / util.c
index b85f29e1362e9c8a200499aa4a6ee37bd2b2b965..46be349a2c22d0253642eaecc72ea96e35f078af 100644 (file)
@@ -55,7 +55,17 @@ extern unsigned int global_clobber_region_line;
 #endif /* WITH_NISPLUS_HOME */
 #endif /* HAVE_NETGROUP && WITH_AUTOMOUNT */
 
-enum protocol_types Protocol = PROTOCOL_COREPLUS;
+static enum protocol_types Protocol = PROTOCOL_COREPLUS;
+
+enum protocol_types get_Protocol(void)
+{
+       return Protocol;
+}
+
+void set_Protocol(enum protocol_types  p)
+{
+       Protocol = p;
+}
 
 static enum remote_arch_types ra_type = RA_UNKNOWN;
 
@@ -308,6 +318,24 @@ void set_cmdline_auth_info_username(struct user_auth_info *auth_info,
        }
 }
 
+const char *get_cmdline_auth_info_domain(const struct user_auth_info *auth_info)
+{
+       if (!auth_info->domain) {
+               return "";
+       }
+       return auth_info->domain;
+}
+
+void set_cmdline_auth_info_domain(struct user_auth_info *auth_info,
+                                 const char *domain)
+{
+       TALLOC_FREE(auth_info->domain);
+       auth_info->domain = talloc_strdup(auth_info, domain);
+       if (!auth_info->domain) {
+               exit(ENOMEM);
+       }
+}
+
 const char *get_cmdline_auth_info_password(const struct user_auth_info *auth_info)
 {
        if (!auth_info->password) {
@@ -354,6 +382,16 @@ int get_cmdline_auth_info_signing_state(const struct user_auth_info *auth_info)
        return auth_info->signing_state;
 }
 
+void set_cmdline_auth_info_use_ccache(struct user_auth_info *auth_info, bool b)
+{
+        auth_info->use_ccache = b;
+}
+
+bool get_cmdline_auth_info_use_ccache(const struct user_auth_info *auth_info)
+{
+       return auth_info->use_ccache;
+}
+
 void set_cmdline_auth_info_use_kerberos(struct user_auth_info *auth_info,
                                        bool b)
 {
@@ -499,13 +537,14 @@ void set_cmdline_auth_info_getpass(struct user_auth_info *auth_info)
  Check if a file exists - call vfs_file_exist for samba files.
 ********************************************************************/
 
-bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
+bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf,
+                    bool fake_dir_create_times)
 {
        SMB_STRUCT_STAT st;
        if (!sbuf)
                sbuf = &st;
-  
-       if (sys_stat(fname,sbuf) != 0) 
+
+       if (sys_stat(fname, sbuf, fake_dir_create_times) != 0)
                return(False);
 
        return((S_ISREG(sbuf->st_ex_mode)) || (S_ISFIFO(sbuf->st_ex_mode)));
@@ -518,33 +557,12 @@ bool file_exist_stat(const char *fname,SMB_STRUCT_STAT *sbuf)
 bool socket_exist(const char *fname)
 {
        SMB_STRUCT_STAT st;
-       if (sys_stat(fname,&st) != 0) 
+       if (sys_stat(fname, &st, false) != 0)
                return(False);
 
        return S_ISSOCK(st.st_ex_mode);
 }
 
-/*******************************************************************
- Check if a directory exists.
-********************************************************************/
-
-bool directory_exist_stat(char *dname,SMB_STRUCT_STAT *st)
-{
-       SMB_STRUCT_STAT st2;
-       bool ret;
-
-       if (!st)
-               st = &st2;
-
-       if (sys_stat(dname,st) != 0) 
-               return(False);
-
-       ret = S_ISDIR(st->st_ex_mode);
-       if(!ret)
-               errno = ENOTDIR;
-       return ret;
-}
-
 /*******************************************************************
  Returns the size in bytes of the named given the stat struct.
 ********************************************************************/
@@ -562,7 +580,7 @@ SMB_OFF_T get_file_size(char *file_name)
 {
        SMB_STRUCT_STAT buf;
        buf.st_ex_size = 0;
-       if(sys_stat(file_name,&buf) != 0)
+       if (sys_stat(file_name, &buf, false) != 0)
                return (SMB_OFF_T)-1;
        return get_file_size_stat(&buf);
 }
@@ -1389,7 +1407,7 @@ uid_t nametouid(const char *name)
        char *p;
        uid_t u;
 
-       pass = getpwnam_alloc(talloc_autofree_context(), name);
+       pass = Get_Pwnam_alloc(talloc_autofree_context(), name);
        if (pass) {
                u = pass->pw_uid;
                TALLOC_FREE(pass);
@@ -1691,7 +1709,7 @@ bool is_in_path(const char *name, name_compare_entry *namelist, bool case_sensit
 void set_namearray(name_compare_entry **ppname_array, const char *namelist)
 {
        char *name_end;
-       const char *nameptr = namelist;
+       char *nameptr = (char *)namelist;
        int num_entries = 0;
        int i;
 
@@ -1711,12 +1729,14 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist)
                        nameptr++;
                        continue;
                }
-               /* find the next / */
-               name_end = strchr_m(nameptr, '/');
+               /* anything left? */
+               if ( *nameptr == '\0' )
+                       break;
 
-               /* oops - the last check for a / didn't find one. */
+               /* find the next '/' or consume remaining */
+               name_end = strchr_m(nameptr, '/');
                if (name_end == NULL)
-                       break;
+                       name_end = (char *)nameptr + strlen(nameptr);
 
                /* next segment please */
                nameptr = name_end + 1;
@@ -1732,7 +1752,7 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist)
        }
 
        /* Now copy out the names */
-       nameptr = namelist;
+       nameptr = (char *)namelist;
        i = 0;
        while(*nameptr) {
                if ( *nameptr == '/' ) {
@@ -1740,14 +1760,17 @@ void set_namearray(name_compare_entry **ppname_array, const char *namelist)
                        nameptr++;
                        continue;
                }
-               /* find the next / */
-               if ((name_end = strchr_m(nameptr, '/')) != NULL)
-                       *name_end = 0;
-
-               /* oops - the last check for a / didn't find one. */
-               if(name_end == NULL) 
+               /* anything left? */
+               if ( *nameptr == '\0' )
                        break;
 
+               /* find the next '/' or consume remaining */
+               name_end = strchr_m(nameptr, '/');
+               if (name_end)
+                       *name_end = '\0';
+               else
+                       name_end = nameptr + strlen(nameptr);
+
                (*ppname_array)[i].is_wild = ms_has_wild(nameptr);
                if(((*ppname_array)[i].name = SMB_STRDUP(nameptr)) == NULL) {
                        DEBUG(0,("set_namearray: malloc fail (1)\n"));
@@ -1974,17 +1997,8 @@ const char *tab_depth(int level, int depth)
 
 int str_checksum(const char *s)
 {
-       int res = 0;
-       int c;
-       int i=0;
-
-       while(*s) {
-               c = *s;
-               res ^= (c << (i % 15)) ^ (c >> (15-(i%15)));
-               s++;
-               i++;
-       }
-       return(res);
+       TDB_DATA key = string_tdb_data(s);
+       return jenkins_hash(&key);
 }
 
 /*****************************************************************
@@ -2113,10 +2127,10 @@ void *smb_xmalloc_array(size_t size, unsigned int count)
        va_copy(ap2, ap);
 
        n = vasprintf(ptr, format, ap2);
+       va_end(ap2);
        if (n == -1 || ! *ptr) {
                smb_panic("smb_xvasprintf: out of memory");
        }
-       va_end(ap2);
        return n;
 }
 
@@ -2285,7 +2299,7 @@ bool parent_dirname(TALLOC_CTX *mem_ctx, const char *dir, char **parent,
                        return False;
                }
                if (name) {
-                       *name = "";
+                       *name = dir;
                }
                return True;
        }
@@ -2692,14 +2706,15 @@ bool procid_is_me(const struct server_id *pid)
 
 struct server_id interpret_pid(const char *pid_string)
 {
-#ifdef CLUSTER_SUPPORT
-       unsigned int vnn, pid;
        struct server_id result;
-       if (sscanf(pid_string, "%u:%u", &vnn, &pid) == 2) {
+       int pid;
+#ifdef CLUSTER_SUPPORT
+       unsigned int vnn;
+       if (sscanf(pid_string, "%u:%d", &vnn, &pid) == 2) {
                result.vnn = vnn;
                result.pid = pid;
        }
-       else if (sscanf(pid_string, "%u", &pid) == 1) {
+       else if (sscanf(pid_string, "%d", &pid) == 1) {
                result.vnn = get_my_vnn();
                result.pid = pid;
        }
@@ -2707,10 +2722,19 @@ struct server_id interpret_pid(const char *pid_string)
                result.vnn = NONCLUSTER_VNN;
                result.pid = -1;
        }
-       return result;
 #else
-       return pid_to_procid(atoi(pid_string));
+       if (sscanf(pid_string, "%d", &pid) != 1) {
+               result.pid = -1;
+       } else {
+               result.pid = pid;
+       }
 #endif
+       /* Assigning to result.pid may have overflowed
+          Map negative pid to -1: i.e. error */
+       if (result.pid < 0) {
+               result.pid = -1;
+       }
+       return result;
 }
 
 char *procid_str(TALLOC_CTX *mem_ctx, const struct server_id *pid)
@@ -3001,96 +3025,6 @@ void *talloc_zeronull(const void *context, size_t size, const char *name)
 }
 #endif
 
-/* Split a path name into filename and stream name components. Canonicalise
- * such that an implicit $DATA token is always explicit.
- *
- * The "specification" of this function can be found in the
- * run_local_stream_name() function in torture.c, I've tried those
- * combinations against a W2k3 server.
- */
-
-NTSTATUS split_ntfs_stream_name(TALLOC_CTX *mem_ctx, const char *fname,
-                               char **pbase, char **pstream)
-{
-       char *base = NULL;
-       char *stream = NULL;
-       char *sname; /* stream name */
-       const char *stype; /* stream type */
-
-       DEBUG(10, ("split_ntfs_stream_name called for [%s]\n", fname));
-
-       sname = strchr_m(fname, ':');
-
-       if (lp_posix_pathnames() || (sname == NULL)) {
-               if (pbase != NULL) {
-                       base = talloc_strdup(mem_ctx, fname);
-                       NT_STATUS_HAVE_NO_MEMORY(base);
-               }
-               goto done;
-       }
-
-       if (pbase != NULL) {
-               base = talloc_strndup(mem_ctx, fname, PTR_DIFF(sname, fname));
-               NT_STATUS_HAVE_NO_MEMORY(base);
-       }
-
-       sname += 1;
-
-       stype = strchr_m(sname, ':');
-
-       if (stype == NULL) {
-               sname = talloc_strdup(mem_ctx, sname);
-               stype = "$DATA";
-       }
-       else {
-               if (StrCaseCmp(stype, ":$DATA") != 0) {
-                       /*
-                        * If there is an explicit stream type, so far we only
-                        * allow $DATA. Is there anything else allowed? -- vl
-                        */
-                       DEBUG(10, ("[%s] is an invalid stream type\n", stype));
-                       TALLOC_FREE(base);
-                       return NT_STATUS_OBJECT_NAME_INVALID;
-               }
-               sname = talloc_strndup(mem_ctx, sname, PTR_DIFF(stype, sname));
-               stype += 1;
-       }
-
-       if (sname == NULL) {
-               TALLOC_FREE(base);
-               return NT_STATUS_NO_MEMORY;
-       }
-
-       if (sname[0] == '\0') {
-               /*
-                * no stream name, so no stream
-                */
-               goto done;
-       }
-
-       if (pstream != NULL) {
-               stream = talloc_asprintf(mem_ctx, "%s:%s", sname, stype);
-               if (stream == NULL) {
-                       TALLOC_FREE(sname);
-                       TALLOC_FREE(base);
-                       return NT_STATUS_NO_MEMORY;
-               }
-               /*
-                * upper-case the type field
-                */
-               strupper_m(strchr_m(stream, ':')+1);
-       }
-
- done:
-       if (pbase != NULL) {
-               *pbase = base;
-       }
-       if (pstream != NULL) {
-               *pstream = stream;
-       }
-       return NT_STATUS_OK;
-}
-
 bool is_valid_policy_hnd(const struct policy_handle *hnd)
 {
        struct policy_handle tmp;
@@ -3127,3 +3061,14 @@ const char *strip_hostname(const char *s)
 
        return s;
 }
+
+bool tevent_req_poll_ntstatus(struct tevent_req *req,
+                             struct tevent_context *ev,
+                             NTSTATUS *status)
+{
+       bool ret = tevent_req_poll(req, ev);
+       if (!ret) {
+               *status = map_nt_error_from_unix(errno);
+       }
+       return ret;
+}