Second part of fix for bug #8679 - recvfile code path using splice() on Linux leaves...
[samba.git] / source3 / lib / system.c
index 74a8971c5386421f84a7818c62a7956f58f7d4e1..85988417a65ed434403b3f7bbc0779fa1a0622e6 100644 (file)
 
 
 
-/*******************************************************************
- A wrapper for memalign
-********************************************************************/
-
-void *sys_memalign( size_t align, size_t size )
-{
-#if defined(HAVE_POSIX_MEMALIGN)
-       void *p = NULL;
-       int ret = posix_memalign( &p, align, size );
-       if ( ret == 0 )
-               return p;
-
-       return NULL;
-#elif defined(HAVE_MEMALIGN)
-       return memalign( align, size );
-#else
-       /* On *BSD systems memaligns doesn't exist, but memory will
-        * be aligned on allocations of > pagesize. */
-#if defined(SYSCONF_SC_PAGESIZE)
-       size_t pagesize = (size_t)sysconf(_SC_PAGESIZE);
-#elif defined(HAVE_GETPAGESIZE)
-       size_t pagesize = (size_t)getpagesize();
-#else
-       size_t pagesize = (size_t)-1;
-#endif
-       if (pagesize == (size_t)-1) {
-               DEBUG(0,("memalign functionalaity not available on this platform!\n"));
-               return NULL;
-       }
-       if (size < pagesize) {
-               size = pagesize;
-       }
-       return SMB_MALLOC(size);
-#endif
-}
-
 /*******************************************************************
  A wrapper for usleep in case we don't have one.
 ********************************************************************/
@@ -545,13 +509,13 @@ void update_stat_ex_create_time(struct stat_ex *dst,
 }
 
 #if defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(HAVE_OFF64_T) && defined(HAVE_STAT64)
-static void init_stat_ex_from_stat (struct stat_ex *dst,
-                                   const struct stat64 *src,
-                                   bool fake_dir_create_times)
+void init_stat_ex_from_stat (struct stat_ex *dst,
+                           const struct stat64 *src,
+                           bool fake_dir_create_times)
 #else
-static void init_stat_ex_from_stat (struct stat_ex *dst,
-                                   const struct stat *src,
-                                   bool fake_dir_create_times)
+void init_stat_ex_from_stat (struct stat_ex *dst,
+                           const struct stat *src,
+                           bool fake_dir_create_times)
 #endif
 {
        dst->st_ex_dev = src->st_dev;
@@ -769,6 +733,8 @@ int sys_fseek(FILE *fp, SMB_OFF_T offset, int whence)
        return fseek64(fp, offset, whence);
 #elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO64)
        return fseeko64(fp, offset, whence);
+#elif defined(HAVE_EXPLICIT_LARGEFILE_SUPPORT) && defined(LARGE_SMB_OFF_T) && defined(HAVE_FSEEKO)
+       return fseeko(fp, offset, whence);
 #else
        return fseek(fp, offset, whence);
 #endif
@@ -995,18 +961,45 @@ int sys_waitpid(pid_t pid,int *status,int options)
 }
 
 /*******************************************************************
- System wrapper for getwd
+ System wrapper for getwd. Always returns MALLOC'ed memory, or NULL
+ on error (malloc fail usually).
 ********************************************************************/
 
-char *sys_getwd(char *s)
+char *sys_getwd(void)
 {
-       char *wd;
-#ifdef HAVE_GETCWD
-       wd = (char *)getcwd(s, PATH_MAX);
+#ifdef GETCWD_TAKES_NULL
+       return getcwd(NULL, 0);
+#elif HAVE_GETCWD
+       char *wd = NULL, *s = NULL;
+       size_t allocated = PATH_MAX;
+
+       while (1) {
+               s = SMB_REALLOC_ARRAY(s, char, allocated);
+               if (s == NULL) {
+                       return NULL;
+               }
+               wd = getcwd(s, allocated);
+               if (wd) {
+                       break;
+               }
+               if (errno != ERANGE) {
+                       SAFE_FREE(s);
+                       break;
+               }
+               allocated *= 2;
+               if (allocated < PATH_MAX) {
+                       SAFE_FREE(s);
+                       break;
+               }
+       }
+       return wd;
 #else
-       wd = (char *)getwd(s);
+       char *s = SMB_MALLOC_ARRAY(char, PATH_MAX);
+       if (s == NULL) {
+               return NULL;
+       }
+       return getwd(s);
 #endif
-       return wd;
 }
 
 #if defined(HAVE_POSIX_CAPABILITIES)
@@ -1391,7 +1384,7 @@ static char **extract_args(TALLOC_CTX *mem_ctx, const char *command)
 
        TALLOC_FREE(trunc_cmd);
 
-       if (!(argl = TALLOC_ARRAY(mem_ctx, char *, argcl + 1))) {
+       if (!(argl = talloc_array(mem_ctx, char *, argcl + 1))) {
                goto nomem;
        }
 
@@ -2376,7 +2369,7 @@ static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
        dirp = fdopendir(newfd);
 
        while ((de = readdir(dirp))) {
-               size_t listlen = strlen(de->d_name);
+               size_t listlen = strlen(de->d_name) + 1;
                if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) {
                        /* we don't want "." and ".." here: */
                        DEBUG(10,("skipped EA %s\n",de->d_name));
@@ -2385,18 +2378,16 @@ static ssize_t solaris_list_xattr(int attrdirfd, char *list, size_t size)
 
                if (size == 0) {
                        /* return the current size of the list of extended attribute names*/
-                       len += listlen + 1;
+                       len += listlen;
                } else {
                        /* check size and copy entrieŃ• + nul into list. */
-                       if ((len + listlen + 1) > size) {
+                       if ((len + listlen) > size) {
                                errno = ERANGE;
                                len = -1;
                                break;
                        } else {
-                               safe_strcpy(list + len, de->d_name, listlen);
+                               strlcpy(list + len, de->d_name, listlen);
                                len += listlen;
-                               list[len] = '\0';
-                               ++len;
                        }
                }
        }