static TACommandVerdict __lxstat64_cmd(TAThread thread,TAInputStream stream) { struct stat64 buffer; int res; char* path; // Prepare path = readString(&stream); START_TARGET_OPERATION(thread); errno=0; #if ( __i386__ || __powerpc__ || (__s390__ && !__s390x__)) res = __lxstat64(3, path, &buffer); #else res = __lxstat64(0, path, &buffer); #endif END_TARGET_OPERATION(thread); // Response writeInt(thread, res); writeInt(thread, errno); writeFileStatus64(thread, &buffer); sendResponse(thread); return taDefaultVerdict; }
static int try_nocreate (char *tmpl, void *flags) { struct_stat64 st; if (__lxstat64 (_STAT_VER, tmpl, &st) == 0) __set_errno (EEXIST); return errno == ENOENT ? 0 : -1; }
char * __realpath (const char *name, char *resolved) { char *rpath, *dest, *extra_buf = NULL; const char *start, *end, *rpath_limit; long int path_max; #ifdef S_ISLNK int num_links = 0; #endif if (name == NULL) { /* As per Single Unix Specification V2 we must return an error if either parameter is a null pointer. We extend this to allow the RESOLVED parameter to be NULL in case the we are expected to allocate the room for the return value. */ __set_errno (EINVAL); return NULL; } if (name[0] == '\0') { /* As per Single Unix Specification V2 we must return an error if the name argument points to an empty string. */ __set_errno (ENOENT); return NULL; } #ifdef PATH_MAX path_max = PATH_MAX; #else path_max = pathconf (name, _PC_PATH_MAX); if (path_max <= 0) path_max = 1024; #endif if (resolved == NULL) { rpath = malloc (path_max); if (rpath == NULL) return NULL; } else rpath = resolved; rpath_limit = rpath + path_max; if (name[0] != '/') { if (!__getcwd (rpath, path_max)) { rpath[0] = '\0'; goto error; } dest = strchr (rpath, '\0'); } else { rpath[0] = '/'; dest = rpath + 1; } for (start = end = name; *start; start = end) { #ifdef _LIBC struct stat64 st; #else struct stat st; #endif /* Skip sequence of multiple path-separators. */ while (*start == '/') ++start; /* Find end of path component. */ for (end = start; *end && *end != '/'; ++end) /* Nothing. */; if (end - start == 0) break; else if (end - start == 1 && start[0] == '.') /* nothing */; else if (end - start == 2 && start[0] == '.' && start[1] == '.') { /* Back up to previous component, ignore if at root already. */ if (dest > rpath + 1) while ((--dest)[-1] != '/'); } else { size_t new_size; if (dest[-1] != '/') *dest++ = '/'; if (dest + (end - start) >= rpath_limit) { ptrdiff_t dest_offset = dest - rpath; char *new_rpath; if (resolved) { __set_errno (ENAMETOOLONG); if (dest > rpath + 1) dest--; *dest = '\0'; goto error; } new_size = rpath_limit - rpath; if (end - start + 1 > path_max) new_size += end - start + 1; else new_size += path_max; new_rpath = (char *) realloc (rpath, new_size); if (new_rpath == NULL) goto error; rpath = new_rpath; rpath_limit = rpath + new_size; dest = rpath + dest_offset; } #ifdef _LIBC dest = __mempcpy (dest, start, end - start); #else memcpy (dest, start, end - start); dest += end - start; #endif *dest = '\0'; #ifdef _LIBC if (__lxstat64 (_STAT_VER, rpath, &st) < 0) #else if (lstat (rpath, &st) < 0) #endif goto error; #ifdef S_ISLNK if (S_ISLNK (st.st_mode)) { char *buf; size_t len; int n; if (++num_links > MAXSYMLINKS) { __set_errno (ELOOP); goto error; } buf = allocsa (path_max); if (!buf) { errno = ENOMEM; goto error; } n = __readlink (rpath, buf, path_max); if (n < 0) { int saved_errno = errno; freesa (buf); errno = saved_errno; goto error; } buf[n] = '\0'; if (!extra_buf) { extra_buf = allocsa (path_max); if (!extra_buf) { freesa (buf); errno = ENOMEM; goto error; } } len = strlen (end); if ((long int) (n + len) >= path_max) { freesa (buf); __set_errno (ENAMETOOLONG); goto error; } /* Careful here, end may be a pointer into extra_buf... */ memmove (&extra_buf[n], end, len + 1); name = end = memcpy (extra_buf, buf, n); if (buf[0] == '/') dest = rpath + 1; /* It's an absolute symlink */ else /* Back up to previous component, ignore if at root already: */ if (dest > rpath + 1) while ((--dest)[-1] != '/'); } #endif } } if (dest > rpath + 1 && dest[-1] == '/') --dest; *dest = '\0'; if (extra_buf) freesa (extra_buf); return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath; error: { int saved_errno = errno; if (extra_buf) freesa (extra_buf); if (resolved) strcpy (resolved, rpath); else free (rpath); errno = saved_errno; } return NULL; }
char * __realpath (const char *name, char *resolved) { char *rpath, *dest, *extra_buf = NULL; const char *start, *end, *rpath_limit; long int path_max; int num_links = 0; size_t prefix_len; if (name == NULL) { /* As per Single Unix Specification V2 we must return an error if either parameter is a null pointer. We extend this to allow the RESOLVED parameter to be NULL in case the we are expected to allocate the room for the return value. */ __set_errno (EINVAL); return NULL; } if (name[0] == '\0') { /* As per Single Unix Specification V2 we must return an error if the name argument points to an empty string. */ __set_errno (ENOENT); return NULL; } #ifdef PATH_MAX path_max = PATH_MAX; #else path_max = pathconf (name, _PC_PATH_MAX); if (path_max <= 0) path_max = 8192; #endif if (resolved == NULL) { rpath = malloc (path_max); if (rpath == NULL) { /* It's easier to set errno to ENOMEM than to rely on the 'malloc-posix' gnulib module. */ errno = ENOMEM; return NULL; } } else rpath = resolved; rpath_limit = rpath + path_max; /* This is always zero for Posix hosts, but can be 2 for MS-Windows and MS-DOS X:/foo/bar file names. */ prefix_len = FILE_SYSTEM_PREFIX_LEN (name); if (!IS_ABSOLUTE_FILE_NAME (name)) { if (!__getcwd (rpath, path_max)) { rpath[0] = '\0'; goto error; } dest = strchr (rpath, '\0'); start = name; prefix_len = FILE_SYSTEM_PREFIX_LEN (rpath); } else { dest = rpath; if (prefix_len) { memcpy (rpath, name, prefix_len); dest += prefix_len; } *dest++ = '/'; if (DOUBLE_SLASH_IS_DISTINCT_ROOT) { if (ISSLASH (name[1]) && !ISSLASH (name[2]) && !prefix_len) *dest++ = '/'; *dest = '\0'; } start = name + prefix_len; } for (end = start; *start; start = end) { #ifdef _LIBC struct stat64 st; #else struct stat st; #endif int n; /* Skip sequence of multiple path-separators. */ while (ISSLASH (*start)) ++start; /* Find end of path component. */ for (end = start; *end && !ISSLASH (*end); ++end) /* Nothing. */; if (end - start == 0) break; else if (end - start == 1 && start[0] == '.') /* nothing */; else if (end - start == 2 && start[0] == '.' && start[1] == '.') { /* Back up to previous component, ignore if at root already. */ if (dest > rpath + prefix_len + 1) for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest) continue; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && !prefix_len && ISSLASH (*dest) && !ISSLASH (dest[1])) dest++; } else { size_t new_size; if (!ISSLASH (dest[-1])) *dest++ = '/'; if (dest + (end - start) >= rpath_limit) { ptrdiff_t dest_offset = dest - rpath; char *new_rpath; if (resolved) { __set_errno (ENAMETOOLONG); if (dest > rpath + prefix_len + 1) dest--; *dest = '\0'; goto error; } new_size = rpath_limit - rpath; if (end - start + 1 > path_max) new_size += end - start + 1; else new_size += path_max; new_rpath = (char *) realloc (rpath, new_size); if (new_rpath == NULL) { /* It's easier to set errno to ENOMEM than to rely on the 'realloc-posix' gnulib module. */ errno = ENOMEM; goto error; } rpath = new_rpath; rpath_limit = rpath + new_size; dest = rpath + dest_offset; } #ifdef _LIBC dest = __mempcpy (dest, start, end - start); #else memcpy (dest, start, end - start); dest += end - start; #endif *dest = '\0'; #ifdef _LIBC if (__lxstat64 (_STAT_VER, rpath, &st) < 0) #else if (lstat (rpath, &st) < 0) #endif goto error; if (S_ISLNK (st.st_mode)) { char *buf; size_t len; if (++num_links > MAXSYMLINKS) { __set_errno (ELOOP); goto error; } buf = malloca (path_max); if (!buf) { errno = ENOMEM; goto error; } n = __readlink (rpath, buf, path_max - 1); if (n < 0) { int saved_errno = errno; freea (buf); errno = saved_errno; goto error; } buf[n] = '\0'; if (!extra_buf) { extra_buf = malloca (path_max); if (!extra_buf) { freea (buf); errno = ENOMEM; goto error; } } len = strlen (end); if ((long int) (n + len) >= path_max) { freea (buf); __set_errno (ENAMETOOLONG); goto error; } /* Careful here, end may be a pointer into extra_buf... */ memmove (&extra_buf[n], end, len + 1); name = end = memcpy (extra_buf, buf, n); if (IS_ABSOLUTE_FILE_NAME (buf)) { size_t pfxlen = FILE_SYSTEM_PREFIX_LEN (buf); if (pfxlen) memcpy (rpath, buf, pfxlen); dest = rpath + pfxlen; *dest++ = '/'; /* It's an absolute symlink */ if (DOUBLE_SLASH_IS_DISTINCT_ROOT) { if (ISSLASH (buf[1]) && !ISSLASH (buf[2]) && !pfxlen) *dest++ = '/'; *dest = '\0'; } /* Install the new prefix to be in effect hereafter. */ prefix_len = pfxlen; } else { /* Back up to previous component, ignore if at root already: */ if (dest > rpath + prefix_len + 1) for (--dest; dest > rpath && !ISSLASH (dest[-1]); --dest) continue; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && ISSLASH (*dest) && !ISSLASH (dest[1]) && !prefix_len) dest++; } } else if (!S_ISDIR (st.st_mode) && *end != '\0') { __set_errno (ENOTDIR); goto error; } } } if (dest > rpath + prefix_len + 1 && ISSLASH (dest[-1])) --dest; if (DOUBLE_SLASH_IS_DISTINCT_ROOT && dest == rpath + 1 && !prefix_len && ISSLASH (*dest) && !ISSLASH (dest[1])) dest++; *dest = '\0'; if (extra_buf) freea (extra_buf); return rpath; error: { int saved_errno = errno; if (extra_buf) freea (extra_buf); if (resolved == NULL) free (rpath); errno = saved_errno; } return NULL; }
/* Generate a temporary file name based on TMPL. TMPL must match the rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed does not exist at the time of the call to __gen_tempname. TMPL is overwritten with the result. KIND may be one of: __GT_NOCREATE: simply verify that the name does not exist at the time of the call. __GT_FILE: create the file using open(O_CREAT|O_EXCL) and return a read-write fd. The file is mode 0600. __GT_BIGFILE: same as __GT_FILE but use open64(). __GT_DIR: create a directory, which will be mode 0700. We use a clever algorithm to get hard-to-predict names. */ int __gen_tempname (char *tmpl, int kind) { int len; char *XXXXXX; static uint64_t value; uint64_t random_time_bits; unsigned int count; int fd = -1; int save_errno = errno; struct_stat64 st; /* A lower bound on the number of temporary files to attempt to generate. The maximum total number of temporary file names that can exist for a given template is 62**6. It should never be necessary to try all these combinations. Instead if a reasonable number of names is tried (we define reasonable as 62**3) fail to give the system administrator the chance to remove the problems. */ unsigned int attempts_min = 62 * 62 * 62; /* The number of times to attempt to generate a temporary file. To conform to POSIX, this must be no smaller than TMP_MAX. */ unsigned int attempts = attempts_min < TMP_MAX ? TMP_MAX : attempts_min; len = strlen (tmpl); if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) { __set_errno (EINVAL); return -1; } /* This is where the Xs start. */ XXXXXX = &tmpl[len - 6]; /* Get some more or less random data. */ #ifdef RANDOM_BITS RANDOM_BITS (random_time_bits); #else # if HAVE_GETTIMEOFDAY || _LIBC { struct timeval tv; __gettimeofday (&tv, NULL); random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; } # else random_time_bits = time (NULL); # endif #endif value += random_time_bits ^ __getpid (); for (count = 0; count < attempts; value += 7777, ++count) { uint64_t v = value; /* Fill in the random bits. */ XXXXXX[0] = letters[v % 62]; v /= 62; XXXXXX[1] = letters[v % 62]; v /= 62; XXXXXX[2] = letters[v % 62]; v /= 62; XXXXXX[3] = letters[v % 62]; v /= 62; XXXXXX[4] = letters[v % 62]; v /= 62; XXXXXX[5] = letters[v % 62]; switch (kind) { case __GT_FILE: fd = __open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); break; case __GT_BIGFILE: fd = __open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); break; case __GT_DIR: fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR); break; case __GT_NOCREATE: /* This case is backward from the other three. __gen_tempname succeeds if __xstat fails because the name does not exist. Note the continue to bypass the common logic at the bottom of the loop. */ if (__lxstat64 (_STAT_VER, tmpl, &st) < 0) { if (errno == ENOENT) { __set_errno (save_errno); return 0; } else /* Give up now. */ return -1; } continue; default: assert (! "invalid KIND in __gen_tempname"); } if (fd >= 0) { __set_errno (save_errno); return fd; } else if (errno != EEXIST) return -1; } /* We got out of the loop because we ran out of combinations to try. */ __set_errno (EEXIST); return -1; }
/* Generate a temporary file name based on TMPL. TMPL must match the rules for mk[s]temp (i.e. end in "XXXXXX"). The name constructed does not exist at the time of the call to __gen_tempname. TMPL is overwritten with the result. KIND may be one of: __GT_NOCREATE: simply verify that the name does not exist at the time of the call. __GT_FILE: create the file using open(O_CREAT|O_EXCL) and return a read-write fd. The file is mode 0600. __GT_BIGFILE: same as __GT_FILE but use open64(). __GT_DIR: create a directory, which will be mode 0700. We use a clever algorithm to get hard-to-predict names. */ int __gen_tempname (char *tmpl, int kind) { int len; char *XXXXXX; static uint64_t value; uint64_t random_time_bits; int count, fd = -1; int save_errno = errno; struct_stat64 st; len = strlen (tmpl); if (len < 6 || strcmp (&tmpl[len - 6], "XXXXXX")) { __set_errno (EINVAL); return -1; } /* This is where the Xs start. */ XXXXXX = &tmpl[len - 6]; /* Get some more or less random data. */ #ifdef RANDOM_BITS RANDOM_BITS (random_time_bits); #else # if HAVE_GETTIMEOFDAY || _LIBC { struct timeval tv; __gettimeofday (&tv, NULL); random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; } # else random_time_bits = time (NULL); # endif #endif value += random_time_bits ^ __getpid (); for (count = 0; count < TMP_MAX; value += 7777, ++count) { uint64_t v = value; /* Fill in the random bits. */ XXXXXX[0] = letters[v % 62]; v /= 62; XXXXXX[1] = letters[v % 62]; v /= 62; XXXXXX[2] = letters[v % 62]; v /= 62; XXXXXX[3] = letters[v % 62]; v /= 62; XXXXXX[4] = letters[v % 62]; v /= 62; XXXXXX[5] = letters[v % 62]; switch (kind) { case __GT_FILE: fd = __open (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); break; case __GT_BIGFILE: fd = __open64 (tmpl, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); break; case __GT_DIR: fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR); break; case __GT_NOCREATE: /* This case is backward from the other three. __gen_tempname succeeds if __xstat fails because the name does not exist. Note the continue to bypass the common logic at the bottom of the loop. */ if (__lxstat64 (_STAT_VER, tmpl, &st) < 0) { if (errno == ENOENT) { __set_errno (save_errno); return 0; } else /* Give up now. */ return -1; } continue; default: assert (! "invalid KIND in __gen_tempname"); } if (fd >= 0) { __set_errno (save_errno); return fd; } else if (errno != EEXIST) return -1; } /* We got out of the loop because we ran out of combinations to try. */ __set_errno (EEXIST); return -1; }
void attribute_hidden __atfct_seterrno_2 (int errval, int fd1, const char *buf1, int fd2, const char *buf2) { if (buf1 != NULL || buf2 != NULL) { struct stat64 st; if (errval == ENOTDIR) { /* This can mean either the file descriptor is invalid or /proc is not mounted. */ if (buf1 != NULL) { if (__fxstat64 (_STAT_VER, fd1, &st) != 0) /* errno is already set correctly. */ return; /* If /proc is not mounted there is nothing we can do. */ if (S_ISDIR (st.st_mode) && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0 || !S_ISDIR (st.st_mode))) { errval = ENOSYS; goto out; } } if (buf2 != NULL) { if (__fxstat64 (_STAT_VER, fd2, &st) != 0) /* errno is already set correctly. */ return; /* If /proc is not mounted there is nothing we can do. */ if (S_ISDIR (st.st_mode) && (__xstat64 (_STAT_VER, "/proc/self/fd", &st) != 0 || !S_ISDIR (st.st_mode))) errval = ENOSYS; } } else if (errval == ENOENT) { /* This could mean the file descriptor is not valid. We reuse BUF for the stat call. Find the slash after the file descriptor number. */ if (buf1 != NULL) { *(char *) strchr (buf1 + sizeof "/proc/self/fd", '/') = '\0'; int e = __lxstat64 (_STAT_VER, buf1, &st); if ((e == -1 && errno == ENOENT) ||(e == 0 && !S_ISLNK (st.st_mode))) { errval = EBADF; goto out; } } if (buf2 != NULL) { *(char *) strchr (buf2 + sizeof "/proc/self/fd", '/') = '\0'; int e = __lxstat64 (_STAT_VER, buf2, &st); if ((e == -1 && errno == ENOENT) ||(e == 0 && !S_ISLNK (st.st_mode))) errval = EBADF; } } } out: __set_errno (errval); }
int lstat64 (const char *file, struct stat64 *buf) { return __lxstat64 (_STAT_VER, file, buf); }
static char * canonicalize (const char *name, char *resolved) { char *rpath, *dest, *extra_buf = NULL; const char *start, *end, *rpath_limit; long int path_max; int num_links = 0, old_errno; if (name == NULL) { /* As per Single Unix Specification V2 we must return an error if either parameter is a null pointer. We extend this to allow the RESOLVED parameter to be NULL in case the we are expected to allocate the room for the return value. */ __set_errno (EINVAL); return NULL; } if (name[0] == '\0') { /* As per Single Unix Specification V2 we must return an error if the name argument points to an empty string. */ __set_errno (ENOENT); return NULL; } #ifdef __WIN32__ { char *lpFilePart; int len; // fprintf(stderr, "name: %s\n", name); rpath = resolved ? __builtin_alloca (MAX_PATH) : malloc (MAX_PATH); // unix2winpath (name); // fprintf(stderr, "name: %s\n", name); len = GetFullPathName(name, MAX_PATH, rpath, &lpFilePart); /* GetFullPathName returns bogus paths for *nix-style paths, like * /foo/bar - it just prepends current drive to them. Keep them * intact (they need to be for relocation to work!). */ if (name[0] == '/') { strncpy (rpath, name, MAX_PATH - 1); rpath[MAX_PATH - 1] = '\0'; len = strlen (rpath); } // fprintf(stderr, "rpath: %s\n", rpath); if (len == 0) { //set_werrno; return NULL; } if (len > MAX_PATH) { if (resolved) __set_errno(ENAMETOOLONG); else { rpath = realloc(rpath, len + 2); GetFullPathName(name, len, rpath, &lpFilePart); // fprintf(stderr, "rpath: %s\n", rpath); } } // if ( ISDIRSEP(name[strlen(name)]) && !ISDIRSEP(rpath[len]) ) { // rpath[len] = '\\'; // rpath[len + 1] = 0; // } old_errno = errno; //if (!access (rpath, D_OK) && !ISDIRSEP(rpath[len - 1]) ){ if (!access (rpath, R_OK) && !ISDIRSEP(rpath[len - 1]) ){ rpath[len] = '\\'; rpath[len + 1] = 0; } errno = old_errno; win2unixpath (rpath); // fprintf(stderr, "rpath: %s\n", rpath); return resolved ? strcpy(resolved, rpath) : rpath ; } #else /* __WIN32__ */ #ifdef PATH_MAX path_max = PATH_MAX; #else path_max = pathconf (name, _PC_PATH_MAX); if (path_max <= 0) path_max = 1024; #endif rpath = resolved ? __builtin_alloca (path_max) : malloc (path_max); rpath_limit = rpath + path_max; if (name[0] != '/') { if (!__getcwd (rpath, path_max)) { rpath[0] = '\0'; goto error; } dest = strchr (rpath, '\0'); } else { rpath[0] = '/'; dest = rpath + 1; } for (start = end = name; *start; start = end) { #ifdef _LIBC struct stat64 st; #else struct stat st; #endif int n; /* Skip sequence of multiple path-separators. */ while (*start == '/') ++start; /* Find end of path component. */ for (end = start; *end && *end != '/'; ++end) /* Nothing. */; if (end - start == 0) break; else if (end - start == 1 && start[0] == '.') /* nothing */; else if (end - start == 2 && start[0] == '.' && start[1] == '.') { /* Back up to previous component, ignore if at root already. */ if (dest > rpath + 1) while ((--dest)[-1] != '/'); } else { size_t new_size; if (dest[-1] != '/') *dest++ = '/'; if (dest + (end - start) >= rpath_limit) { ptrdiff_t dest_offset = dest - rpath; if (resolved) { __set_errno (ENAMETOOLONG); if (dest > rpath + 1) dest--; *dest = '\0'; goto error; } new_size = rpath_limit - rpath; if (end - start + 1 > path_max) new_size += end - start + 1; else new_size += path_max; rpath = realloc (rpath, new_size); rpath_limit = rpath + new_size; if (rpath == NULL) return NULL; dest = rpath + dest_offset; } #ifdef _LIBC dest = __mempcpy (dest, start, end - start); #else memcpy (dest, start, end - start); dest += end - start; #endif *dest = '\0'; #ifdef _LIBC if (__lxstat64 (_STAT_VER, rpath, &st) < 0) #else if (lstat (rpath, &st) < 0) #endif goto error; #if HAVE_READLINK if (S_ISLNK (st.st_mode)) { char *buf = __builtin_alloca (path_max); size_t len; if (++num_links > MAXSYMLINKS) { __set_errno (ELOOP); goto error; } n = __readlink (rpath, buf, path_max); if (n < 0) goto error; buf[n] = '\0'; if (!extra_buf) extra_buf = __builtin_alloca (path_max); len = strlen (end); if ((long int) (n + len) >= path_max) { __set_errno (ENAMETOOLONG); goto error; } /* Careful here, end may be a pointer into extra_buf... */ memmove (&extra_buf[n], end, len + 1); name = end = memcpy (extra_buf, buf, n); if (buf[0] == '/') dest = rpath + 1; /* It's an absolute symlink */ else /* Back up to previous component, ignore if at root already: */ if (dest > rpath + 1) while ((--dest)[-1] != '/'); } #endif } } if (dest > rpath + 1 && dest[-1] == '/') --dest; *dest = '\0'; return resolved ? memcpy (resolved, rpath, dest - rpath + 1) : rpath; error: if (resolved) strcpy (resolved, rpath); else free (rpath); return NULL; #endif /* __WIN32__ */ }
/* Generate a temporary file name based on TMPL. TMPL must match the rules for mk[s]temp (i.e. end in at least X_SUFFIX_LEN "X"s, possibly with a suffix). The name constructed does not exist at the time of the call to this function. TMPL is overwritten with the result. KIND may be one of: __GT_NOCREATE: simply verify that the name does not exist at the time of the call. __GT_FILE: create the file using open(O_CREAT|O_EXCL) and return a read-write fd. The file is mode 0600. __GT_DIR: create a directory, which will be mode 0700. We use a clever algorithm to get hard-to-predict names. */ int gen_tempname_len (char *tmpl, int suffixlen, int flags, int kind, size_t x_suffix_len) { size_t len; char *XXXXXX; unsigned int count; int fd = -1; int save_errno = errno; struct_stat64 st; struct randint_source *rand_src; /* A lower bound on the number of temporary files to attempt to generate. The maximum total number of temporary file names that can exist for a given template is 62**6. It should never be necessary to try all these combinations. Instead if a reasonable number of names is tried (we define reasonable as 62**3) fail to give the system administrator the chance to remove the problems. This value requires that X_SUFFIX_LEN be at least 3. */ #define ATTEMPTS_MIN (62 * 62 * 62) /* The number of times to attempt to generate a temporary file. To conform to POSIX, this must be no smaller than TMP_MAX. */ #if ATTEMPTS_MIN < TMP_MAX unsigned int attempts = TMP_MAX; #else unsigned int attempts = ATTEMPTS_MIN; #endif len = strlen (tmpl); if (len < x_suffix_len + suffixlen || ! check_x_suffix (&tmpl[len - x_suffix_len - suffixlen], x_suffix_len)) { __set_errno (EINVAL); return -1; } /* This is where the Xs start. */ XXXXXX = &tmpl[len - x_suffix_len - suffixlen]; /* Get some more or less random data. */ rand_src = randint_all_new (NULL, x_suffix_len); if (! rand_src) return -1; for (count = 0; count < attempts; ++count) { size_t i; for (i = 0; i < x_suffix_len; i++) XXXXXX[i] = letters[randint_genmax (rand_src, sizeof letters - 2)]; switch (kind) { case __GT_FILE: fd = __open (tmpl, (flags & ~O_ACCMODE) | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR); break; case __GT_DIR: fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR); break; case __GT_NOCREATE: /* This case is backward from the other three. This function succeeds if __xstat fails because the name does not exist. Note the continue to bypass the common logic at the bottom of the loop. */ if (__lxstat64 (_STAT_VER, tmpl, &st) < 0) { if (errno == ENOENT) { __set_errno (save_errno); fd = 0; goto done; } else { /* Give up now. */ fd = -1; goto done; } } continue; default: assert (! "invalid KIND in __gen_tempname"); abort (); } if (fd >= 0) { __set_errno (save_errno); goto done; } else if (errno != EEXIST) { fd = -1; goto done; } } randint_all_free (rand_src); /* We got out of the loop because we ran out of combinations to try. */ __set_errno (EEXIST); return -1; done: { int saved_errno = errno; randint_all_free (rand_src); __set_errno (saved_errno); } return fd; }
int __gen_tempname (char *tmpl, int kind) { int len; char *XXXXXX; static uint64_t value; uint64_t random_time_bits; unsigned long count; int fd = -1; int save_errno = __libc_errno; struct stat64 st; unsigned long attempts_min = 62L * 62L * 62L; unsigned long attempts = attempts_min < 238328 ? 238328 : attempts_min; len = strlen (tmpl); if (len < 6 || strcmp(&tmpl[len - 6], "XXXXXX")) { (__libc_errno = (22)); return -1; } XXXXXX = &tmpl[len - 6]; for (count = 0; count < attempts; value += 7777, ++count) { uint64_t v = value; XXXXXX[0] = letters[v % 62]; v /= 62; XXXXXX[1] = letters[v % 62]; v /= 62; XXXXXX[2] = letters[v % 62]; v /= 62; XXXXXX[3] = letters[v % 62]; v /= 62; XXXXXX[4] = letters[v % 62]; v /= 62; XXXXXX[5] = letters[v % 62]; switch (kind) { case 0: fd = __open (tmpl, 02 | 01000 | 04000, 0400 | 0200); break; case 1: fd = __open64 (tmpl, 02 | 01000 | 04000, 0400 | 0200); break; case 2: fd = __mkdir (tmpl, 0400 | 0200 | 0100); break; case 3: if (__lxstat64 (2, tmpl, &st) < 0) { if (__libc_errno == 2) { (__libc_errno = (save_errno)); return 0; } else return -1; } continue; } if (fd >= 0) { (__libc_errno = (save_errno)); return fd; } else if (__libc_errno != 17) return -1; } (__libc_errno = (17)); return -1; }
__attribute__((weak)) int lstat64(const char *path, struct stat64 *buf) { return __lxstat64(1, path, buf); }
int __xstat64(int ver, const char *path, struct stat64 *buf) { return __lxstat64(ver, path, buf); }