static int atalk_build_paths(TALLOC_CTX *ctx, const char *path, const char *fname, char **adbl_path, char **orig_path, SMB_STRUCT_STAT *adbl_info, SMB_STRUCT_STAT *orig_info, bool fake_dir_create_times) { int ptr0 = 0; int ptr1 = 0; char *dname = 0; char *name = 0; if (!ctx || !path || !fname || !adbl_path || !orig_path || !adbl_info || !orig_info) return -1; #if 0 DEBUG(3, ("ATALK: PATH: %s[%s]\n", path, fname)); #endif if (strstr(path, APPLEDOUBLE) || strstr(fname, APPLEDOUBLE)) { DEBUG(3, ("ATALK: path %s[%s] already contains %s\n", path, fname, APPLEDOUBLE)); return -1; } if (fname[0] == '.') ptr0 ++; if (fname[1] == '/') ptr0 ++; *orig_path = talloc_asprintf(ctx, "%s/%s", path, &fname[ptr0]); /* get pointer to last '/' */ ptr1 = atalk_get_path_ptr(*orig_path); sys_lstat(*orig_path, orig_info, fake_dir_create_times); if (S_ISDIR(orig_info->st_ex_mode)) { *adbl_path = talloc_asprintf(ctx, "%s/%s/%s/", path, &fname[ptr0], APPLEDOUBLE); } else { dname = talloc_strdup(ctx, *orig_path); dname[ptr1] = '\0'; name = *orig_path; *adbl_path = talloc_asprintf(ctx, "%s/%s/%s", dname, APPLEDOUBLE, &name[ptr1 + 1]); } #if 0 DEBUG(3, ("ATALK: DEBUG:\n%s\n%s\n", *orig_path, *adbl_path)); #endif sys_lstat(*adbl_path, adbl_info, fake_dir_create_times); return 0; }
static FILE * get_volfp (glusterfs_ctx_t *ctx) { int ret = 0; cmd_args_t *cmd_args = NULL; FILE *specfp = NULL; struct stat statbuf; cmd_args = &ctx->cmd_args; ret = sys_lstat (cmd_args->volfile, &statbuf); if (ret == -1) { gf_log ("glusterfsd", GF_LOG_ERROR, "%s: %s", cmd_args->volfile, strerror (errno)); return NULL; } if ((specfp = fopen (cmd_args->volfile, "r")) == NULL) { gf_log ("glusterfsd", GF_LOG_ERROR, "volume file %s: %s", cmd_args->volfile, strerror (errno)); return NULL; } gf_log ("glusterfsd", GF_LOG_DEBUG, "loading volume file %s", cmd_args->volfile); return specfp; }
static char * parse_and_validate_args (int argc, char *argv[]) { char *basedir = NULL; struct stat d = {0, }; int ret = -1; #ifndef __FreeBSD__ unsigned char volume_id[16]; #endif /* __FreeBSD__ */ char *slv_mnt = NULL; if (argc != 4) { err ("Usage: %s <DIR> <SLAVE-VOL-MOUNT> <CRAWL-THREAD-COUNT>\n", argv[0]); return NULL; } basedir = argv[1]; ret = sys_lstat (basedir, &d); if (ret) { err ("%s: %s\n", basedir, strerror (errno)); return NULL; } #ifndef __FreeBSD__ ret = sys_lgetxattr (basedir, "trusted.glusterfs.volume-id", volume_id, 16); if (ret != 16) { err ("%s:Not a valid brick path.\n", basedir); return NULL; } #endif /* __FreeBSD__ */ slv_mnt = argv[2]; ret = sys_lstat (slv_mnt, &d); if (ret) { err ("%s: %s\n", slv_mnt, strerror (errno)); return NULL; } slavemnt = argv[2]; workers = atoi(argv[3]); if (workers <= 0) workers = DEFAULT_WORKERS; return basedir; }
static int umountd_async (char *path, dev_t dev, int frmdir, int timeout) { int ret = -1; struct stat stbuf = {0, }; int unmount_ret = 0; do { unmount_ret = unmount (path, 0); if (unmount_ret == 0) gf_log ("umountd", GF_LOG_INFO, "Unmounted %s", path); if (unmount_ret != 0 && errno != EBUSY) { gf_log ("umountd", GF_LOG_WARNING, "umount %s failed: %s", path, strerror (errno)); } ret = sys_lstat (path, &stbuf); if (ret != 0) { gf_log ("umountd", GF_LOG_WARNING, "Cannot stat device from %s", path, strerror (errno)); break; } if (stbuf.st_dev != dev) { if (unmount_ret != 0) gf_log ("umountd", GF_LOG_INFO, "device mismatch " "(expect %lld, found %lld), " "someone else unmounted %s", dev, stbuf.st_dev, path); ret = 0; break; } sleep (timeout); } while (1/*CONSTCOND*/); if (ret) { gf_log ("umountd", GF_LOG_ERROR, "Asynchronous unmount of %s failed: %s", path, strerror (errno)); } else { if (frmdir) { ret = rmdir (path); if (ret) gf_log ("umountd", GF_LOG_WARNING, "rmdir %s failed: %s", path, strerror (errno)); else gf_log ("umountd", GF_LOG_INFO, "Removed %s", path); } } return ret; }
int vfswrap_lstat(vfs_handle_struct *handle, connection_struct *conn, const char *path, SMB_STRUCT_STAT *sbuf) { int result; START_PROFILE(syscall_lstat); result = sys_lstat(path, sbuf); END_PROFILE(syscall_lstat); return result; }
enum fmaster_pre_execute_result fmaster_lstat_pre_execute(struct thread *td, struct fmaster_lstat_args *uap, int *error) { if (fmaster_is_master_file(td, uap->path)) { *error = sys_lstat(td, (struct lstat_args *)uap); return (PRE_EXEC_END); } return (PRE_EXEC_CONT); }
/********************** ntalk_mkresdir: creates a new .AppleDouble directory (if necessary) for the resource fork of a specified file **********************/ int ntalk_mkresdir(const char *fname) { char fdir[255]; int i; int lastslash; SMB_STRUCT_STAT dirstats; char appledouble[] = APPLEDOUBLE; /* find directory containing fname */ for (i = 0; (fname[i] != 0) && (i <= 254); i++) { fdir[i] = fname[i]; if (fdir[i] == '/') { lastslash = i; } } lastslash++; fdir[lastslash] = 0; sys_lstat(fdir, &dirstats); /* append .AppleDouble */ for (i = 0; (appledouble[i] != 0) && (lastslash <= 254); i++) { fdir[lastslash] = appledouble[i]; lastslash++; } fdir[lastslash] = 0; /* create this directory */ /* silently ignore EEXIST error */ if ((mkdir(fdir, dirstats.st_mode) < 0) && (errno != EEXIST)) { return errno; } /* set ownership of this dir to the same as its parent */ /* holy race condition, batman! */ /* warning: this doesn't check for errors */ chown(fdir, dirstats.st_uid, dirstats.st_gid); printf("%s\n", fdir); return 1; }
int dos_lstat(char *fname,SMB_STRUCT_STAT *sbuf) { return(sys_lstat(dos_to_unix(fname,False),sbuf)); }
static int copy_reg(char *source, const char *dest) { SMB_STRUCT_STAT source_stats; int ifd; int ofd; char *buf; int len; /* Number of bytes read into `buf'. */ sys_lstat (source, &source_stats); if (!S_ISREG (source_stats.st_mode)) return 1; if (unlink (dest) && errno != ENOENT) return 1; if((ifd = sys_open (source, O_RDONLY, 0)) < 0) return 1; if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0 ) { close (ifd); return 1; } if((buf = malloc( COPYBUF_SIZE )) == NULL) { close (ifd); close (ofd); unlink (dest); return 1; } while ((len = read(ifd, buf, COPYBUF_SIZE)) > 0) { if (write_data(ofd, buf, len) < 0) { close (ifd); close (ofd); unlink (dest); free(buf); return 1; } } free(buf); if (len < 0) { close (ifd); close (ofd); unlink (dest); return 1; } if (close (ifd) < 0) { close (ofd); return 1; } if (close (ofd) < 0) return 1; /* chown turns off set[ug]id bits for non-root, so do the chmod last. */ /* Try to copy the old file's modtime and access time. */ { struct utimbuf tv; tv.actime = source_stats.st_atime; tv.modtime = source_stats.st_mtime; if (utime (dest, &tv)) return 1; } /* Try to preserve ownership. For non-root it might fail, but that's ok. But root probably wants to know, e.g. if NFS disallows it. */ if (chown (dest, source_stats.st_uid, source_stats.st_gid) && (errno != EPERM)) return 1; if (chmod (dest, source_stats.st_mode & 07777)) return 1; unlink (source); return 0; }
static int copy_reg(const char *source, const char *dest) { SMB_STRUCT_STAT source_stats; int saved_errno; int ifd = -1; int ofd = -1; if (sys_lstat (source, &source_stats) == -1) return -1; if (!S_ISREG (source_stats.st_mode)) return -1; if((ifd = sys_open (source, O_RDONLY, 0)) < 0) return -1; if (unlink (dest) && errno != ENOENT) return -1; #ifdef O_NOFOLLOW if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600)) < 0 ) #else if((ofd = sys_open (dest, O_WRONLY | O_CREAT | O_TRUNC , 0600)) < 0 ) #endif goto err; if (transfer_file(ifd, ofd, (size_t)-1) == -1) goto err; /* * Try to preserve ownership. For non-root it might fail, but that's ok. * But root probably wants to know, e.g. if NFS disallows it. */ #ifdef HAVE_FCHOWN if ((fchown(ofd, source_stats.st_uid, source_stats.st_gid) == -1) && (errno != EPERM)) #else if ((chown(dest, source_stats.st_uid, source_stats.st_gid) == -1) && (errno != EPERM)) #endif goto err; /* * fchown turns off set[ug]id bits for non-root, * so do the chmod last. */ #if defined(HAVE_FCHMOD) if (fchmod (ofd, source_stats.st_mode & 07777)) #else if (chmod (dest, source_stats.st_mode & 07777)) #endif goto err; if (close (ifd) == -1) goto err; if (close (ofd) == -1) return -1; /* Try to copy the old file's modtime and access time. */ { struct utimbuf tv; tv.actime = source_stats.st_atime; tv.modtime = source_stats.st_mtime; utime(dest, &tv); } if (unlink (source) == -1) return -1; return 0; err: saved_errno = errno; if (ifd != -1) close(ifd); if (ofd != -1) close(ofd); errno = saved_errno; return -1; }
int main(int argc, char **argv) { int ret = 0; struct stat st; char *dname = NULL; char *bname = NULL; ssize_t ret_size = 0; uuid_t pgfid_raw = { 0, }; char pgfid[36] = ""; char xxh64[GF_XXH64_DIGEST_LENGTH * 2 + 1] = { 0, }; char pgfid_bname[1024] = { 0, }; char *key = NULL; char *val = NULL; size_t key_size = 0; size_t val_size = 0; const char *file_path = NULL; char *file_path1 = NULL; char *file_path2 = NULL; if (argc != 2) { fprintf(stderr, "Usage: setgfid2path <file-path>\n"); return -1; } ret = sys_lstat(argv[1], &st); if (ret != 0) { fprintf(stderr, "Invalid File Path\n"); return -1; } if (st.st_nlink >= MAX_GFID2PATH_LINK_SUP) { fprintf(stderr, "Number of Hardlink support exceeded. " "max=%d\n", MAX_GFID2PATH_LINK_SUP); return -1; } file_path = argv[1]; file_path1 = strdup(file_path); file_path2 = strdup(file_path); dname = dirname(file_path1); bname = basename(file_path2); /* Get GFID of Parent directory */ ret_size = sys_lgetxattr(dname, GFID_XATTR_KEY, pgfid_raw, GFID_SIZE); if (ret_size != GFID_SIZE) { fprintf(stderr, "Failed to get GFID of parent directory. dir=%s\n", dname); ret = -1; goto out; } /* Convert to UUID format */ if (uuid_utoa_r(pgfid_raw, pgfid) == NULL) { fprintf(stderr, "Failed to format GFID of parent directory. " "dir=%s GFID=%s\n", dname, pgfid_raw); ret = -1; goto out; } /* Find xxhash for PGFID/BaseName */ snprintf(pgfid_bname, sizeof(pgfid_bname), "%s/%s", pgfid, bname); gf_xxh64_wrapper((unsigned char *)pgfid_bname, strlen(pgfid_bname), GF_XXHSUM64_DEFAULT_SEED, xxh64); key_size = SLEN(GFID2PATH_XATTR_KEY_PREFIX) + GF_XXH64_DIGEST_LENGTH * 2 + 1; key = alloca(key_size); snprintf(key, key_size, GFID2PATH_XATTR_KEY_PREFIX "%s", xxh64); val_size = UUID_CANONICAL_FORM_LEN + NAME_MAX + 2; val = alloca(val_size); snprintf(val, val_size, "%s/%s", pgfid, bname); /* Set the Xattr, ignore if same key xattr already exists */ ret = sys_lsetxattr(file_path, key, val, strlen(val), XATTR_CREATE); if (ret == -1) { if (errno == EEXIST) { printf("Xattr already exists, ignoring..\n"); ret = 0; goto out; } fprintf(stderr, "Failed to set gfid2path xattr. errno=%d\n error=%s", errno, strerror(errno)); ret = -1; goto out; } printf("Success. file=%s key=%s value=%s\n", file_path, key, val); out: if (file_path1 != NULL) free(file_path1); if (file_path2 != NULL) free(file_path2); return ret; }
int wsys_lstat(const smb_ucs2_t *wfname,SMB_STRUCT_STAT *sbuf) { pstring fname; return sys_lstat(unicode_to_unix(fname,wfname,sizeof(fname)), sbuf); }
int lstat(const char *file, struct stat *st) { return sys_lstat(file, st); }
int parse_cmdline (int argc, char *argv[], glusterfs_ctx_t *ctx) { int process_mode = 0; int ret = 0; struct stat stbuf = {0, }; struct tm *tm = NULL; time_t utime; char timestr[256]; char tmp_logfile[1024] = { 0 }; char *tmp_logfile_dyn = NULL; char *tmp_logfilebase = NULL; cmd_args_t *cmd_args = NULL; cmd_args = &ctx->cmd_args; argp_parse (&argp, argc, argv, ARGP_IN_ORDER, NULL, cmd_args); if (ENABLE_DEBUG_MODE == cmd_args->debug_mode) { cmd_args->log_level = GF_LOG_DEBUG; cmd_args->log_file = "/dev/stderr"; cmd_args->no_daemon_mode = ENABLE_NO_DAEMON_MODE; } process_mode = gf_get_process_mode (argv[0]); if ((cmd_args->volfile_server == NULL) && (cmd_args->volfile == NULL)) { if (process_mode == GF_SERVER_PROCESS) cmd_args->volfile = gf_strdup (DEFAULT_SERVER_VOLFILE); else if (process_mode == GF_GLUSTERD_PROCESS) cmd_args->volfile = gf_strdup (DEFAULT_GLUSTERD_VOLFILE); else cmd_args->volfile = gf_strdup (DEFAULT_CLIENT_VOLFILE); } if (cmd_args->run_id) { ret = sys_lstat (cmd_args->log_file, &stbuf); /* If its /dev/null, or /dev/stdout, /dev/stderr, * let it use the same, no need to alter */ if (((ret == 0) && (S_ISREG (stbuf.st_mode) || S_ISLNK (stbuf.st_mode))) || (ret == -1)) { /* Have seperate logfile per run */ tm = localtime (&utime); strftime (timestr, 256, "%Y%m%d.%H%M%S", tm); sprintf (tmp_logfile, "%s.%s.%d", cmd_args->log_file, timestr, getpid ()); /* Create symlink to actual log file */ sys_unlink (cmd_args->log_file); tmp_logfile_dyn = gf_strdup (tmp_logfile); tmp_logfilebase = basename (tmp_logfile_dyn); ret = sys_symlink (tmp_logfilebase, cmd_args->log_file); if (ret == -1) { fprintf (stderr, "symlink of logfile failed"); } else { GF_FREE (cmd_args->log_file); cmd_args->log_file = gf_strdup (tmp_logfile); } GF_FREE (tmp_logfile_dyn); } } return ret; }
static int copy_reg(const char *source, const char *dest) { SMB_STRUCT_STAT source_stats; int saved_errno; int ifd = -1; int ofd = -1; if (sys_lstat(source, &source_stats, false) == -1) return -1; if (!S_ISREG (source_stats.st_ex_mode)) return -1; if (source_stats.st_ex_size > module_sizelimit) { DEBUG(5, ("%s: size of %s larger than sizelimit (%lld > %lld), rename prohititted\n", MODULE, source, (long long)source_stats.st_ex_size, (long long)module_sizelimit)); return -1; } if((ifd = open (source, O_RDONLY, 0)) < 0) return -1; if (unlink (dest) && errno != ENOENT) { close(ifd); return -1; } #ifdef O_NOFOLLOW if((ofd = open (dest, O_WRONLY | O_CREAT | O_TRUNC | O_NOFOLLOW, 0600)) < 0 ) #else if((ofd = open (dest, O_WRONLY | O_CREAT | O_TRUNC , 0600)) < 0 ) #endif goto err; if (transfer_file(ifd, ofd, source_stats.st_ex_size) == -1) goto err; /* * Try to preserve ownership. For non-root it might fail, but that's ok. * But root probably wants to know, e.g. if NFS disallows it. */ #ifdef HAVE_FCHOWN if ((fchown(ofd, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM)) #else if ((chown(dest, source_stats.st_ex_uid, source_stats.st_ex_gid) == -1) && (errno != EPERM)) #endif goto err; /* * fchown turns off set[ug]id bits for non-root, * so do the chmod last. */ #if defined(HAVE_FCHMOD) if ((fchmod (ofd, source_stats.st_ex_mode & 07777) == -1) && (errno != EPERM)) #else if ((chmod (dest, source_stats.st_ex_mode & 07777) == -1) && (errno != EPERM)) #endif goto err; if (close (ifd) == -1) goto err; if (close (ofd) == -1) return -1; /* Try to copy the old file's modtime and access time. */ #if defined(HAVE_UTIMENSAT) { struct timespec ts[2]; ts[0] = source_stats.st_ex_atime; ts[1] = source_stats.st_ex_mtime; utimensat(AT_FDCWD, dest, ts, AT_SYMLINK_NOFOLLOW); } #elif defined(HAVE_UTIMES) { struct timeval tv[2]; tv[0] = convert_timespec_to_timeval(source_stats.st_ex_atime); tv[1] = convert_timespec_to_timeval(source_stats.st_ex_mtime); #ifdef HAVE_LUTIMES lutimes(dest, tv); #else utimes(dest, tv); #endif } #elif defined(HAVE_UTIME) { struct utimbuf tv; tv.actime = convert_timespec_to_time_t(source_stats.st_ex_atime); tv.modtime = convert_timespec_to_time_t(source_stats.st_ex_mtime); utime(dest, &tv); } #endif if (unlink (source) == -1) return -1; return 0; err: saved_errno = errno; if (ifd != -1) close(ifd); if (ofd != -1) close(ofd); errno = saved_errno; return -1; }
int xworker_do_crawl (struct xwork *xwork, struct dirjob *job) { DIR *dirp = NULL; int ret = -1; int boff; int plen; struct dirent *result; char dbuf[512]; char *path = NULL; struct dirjob *cjob = NULL; struct stat statbuf = {0,}; char gfid_path[4096] = {0,}; plen = strlen (job->dirname) + 256 + 2; path = alloca (plen); tdbg ("Entering: %s\n", job->dirname); dirp = sys_opendir (job->dirname); if (!dirp) { terr ("opendir failed on %s (%s)\n", job->dirname, strerror (errno)); goto out; } boff = sprintf (path, "%s/", job->dirname); for (;;) { ret = readdir_r (dirp, (struct dirent *)dbuf, &result); if (ret) { err ("readdir_r(%s): %s\n", job->dirname, strerror (errno)); goto out; } if (!result) /* EOF */ break; if (result->d_ino == 0) continue; if (skip_name (job->dirname, result->d_name)) continue; /* It is sure that, children and grandchildren of .glusterfs * are directories, just add them to global queue. */ if (skip_stat (job, result->d_name)) { strncpy (path + boff, result->d_name, (plen-boff)); cjob = dirjob_new (path, job); if (!cjob) { err ("dirjob_new(%s): %s\n", path, strerror (errno)); ret = -1; goto out; } xwork_addcrawl (xwork, cjob); continue; } strcpy (gfid_path, slavemnt); strcat (gfid_path, "/.gfid/"); strcat (gfid_path, result->d_name); ret = sys_lstat (gfid_path, &statbuf); if (ret && errno == ENOENT) { out ("%s\n", result->d_name); BUMP (skipped_gfids); } if (ret && errno != ENOENT) { err ("stat on slave failed(%s): %s\n", gfid_path, strerror (errno)); goto out; } } ret = 0; out: if (dirp) sys_closedir (dirp); return ret; }