off_t SMBC_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence) { off_t size; TALLOC_CTX *frame = talloc_stackframe(); if (!context || !context->internal->initialized) { errno = EINVAL; TALLOC_FREE(frame); return -1; } if (!file || !SMBC_dlist_contains(context->internal->files, file)) { errno = EBADF; TALLOC_FREE(frame); return -1; } if (!file->file) { errno = EINVAL; TALLOC_FREE(frame); return -1; /* Can't lseek a dir ... */ } switch (whence) { case SEEK_SET: file->offset = offset; break; case SEEK_CUR: file->offset += offset; break; case SEEK_END: if (!NT_STATUS_IS_OK(cli_qfileinfo_basic( file->targetcli, file->cli_fd, NULL, &size, NULL, NULL, NULL, NULL, NULL))) { off_t b_size = size; if (!NT_STATUS_IS_OK(cli_getattrE(file->targetcli, file->cli_fd, NULL, &b_size, NULL, NULL, NULL))) { errno = EINVAL; TALLOC_FREE(frame); return -1; } else size = b_size; } file->offset = size + offset; break; default: errno = EINVAL; break; } TALLOC_FREE(frame); return file->offset; }
void nb_open(char *fname, int handle, int size) { int fd, i; int flags = O_RDWR|O_CREAT; size_t st_size; static int count; strupper(fname); if (size == 0) flags |= O_TRUNC; fd = cli_open(c, fname, flags, DENY_NONE); if (fd == -1) { #if NBDEBUG printf("(%d) open %s failed for handle %d (%s)\n", line_count, fname, handle, cli_errstr(c)); #endif return; } cli_getattrE(c, fd, NULL, &st_size, NULL, NULL, NULL); if (size > st_size) { #if NBDEBUG printf("(%d) needs expanding %s to %d from %d\n", line_count, fname, size, (int)st_size); #endif } else if (size < st_size) { #if NBDEBUG printf("(%d) needs truncating %s to %d from %d\n", line_count, fname, size, (int)st_size); #endif } for (i=0;i<MAX_FILES;i++) { if (ftable[i].handle == 0) break; } if (i == MAX_FILES) { printf("file table full for %s\n", fname); exit(1); } ftable[i].handle = handle; ftable[i].fd = fd; if (count++ % 100 == 0) { printf("."); } }
/***************************************************** a wrapper for fstat() *******************************************************/ int smbw_fstat(int fd, struct stat *st) { struct smbw_file *file; time_t c_time, a_time, m_time; size_t size; uint16 mode; SMB_INO_T ino = 0; smbw_busy++; ZERO_STRUCTP(st); file = smbw_file(fd); if (!file) { int ret = smbw_dir_fstat(fd, st); smbw_busy--; return ret; } if (!cli_qfileinfo(&file->srv->cli, file->f->cli_fd, &mode, &size, &c_time, &a_time, &m_time, NULL, &ino) && !cli_getattrE(&file->srv->cli, file->f->cli_fd, &mode, &size, &c_time, &a_time, &m_time)) { errno = EINVAL; smbw_busy--; return -1; } st->st_ino = ino; smbw_setup_stat(st, file->f->fname, size, mode); st->st_atime = a_time; st->st_ctime = c_time; st->st_mtime = m_time; st->st_dev = file->srv->dev; smbw_busy--; return 0; }
/***************************************************** a wrapper for lseek() *******************************************************/ off_t smbw_lseek(int fd, off_t offset, int whence) { struct smbw_file *file; size_t size; smbw_busy++; file = smbw_file(fd); if (!file) { off_t ret = smbw_dir_lseek(fd, offset, whence); smbw_busy--; return ret; } switch (whence) { case SEEK_SET: file->f->offset = offset; break; case SEEK_CUR: file->f->offset += offset; break; case SEEK_END: if (!cli_qfileinfo(&file->srv->cli, file->f->cli_fd, NULL, &size, NULL, NULL, NULL, NULL, NULL) && !cli_getattrE(&file->srv->cli, file->f->cli_fd, NULL, &size, NULL, NULL, NULL)) { errno = EINVAL; smbw_busy--; return -1; } file->f->offset = size + offset; break; } smbw_busy--; return file->f->offset; }
int SMBC_fstat_ctx(SMBCCTX *context, SMBCFILE *file, struct stat *st) { struct timespec change_time_ts; struct timespec access_time_ts; struct timespec write_time_ts; SMB_OFF_T size; uint16 mode; char *server = NULL; char *share = NULL; char *user = NULL; char *password = NULL; char *path = NULL; char *targetpath = NULL; struct cli_state *targetcli = NULL; SMB_INO_T ino = 0; TALLOC_CTX *frame = talloc_stackframe(); NTSTATUS status; if (!context || !context->internal->initialized) { errno = EINVAL; TALLOC_FREE(frame); return -1; } if (!file || !SMBC_dlist_contains(context->internal->files, file)) { errno = EBADF; TALLOC_FREE(frame); return -1; } if (!file->file) { TALLOC_FREE(frame); return smbc_getFunctionFstatdir(context)(context, file, st); } /*d_printf(">>>fstat: parsing %s\n", file->fname);*/ if (SMBC_parse_path(frame, context, file->fname, NULL, &server, &share, &path, &user, &password, NULL)) { errno = EINVAL; TALLOC_FREE(frame); return -1; } /*d_printf(">>>fstat: resolving %s\n", path);*/ status = cli_resolve_path(frame, "", context->internal->auth_info, file->srv->cli, path, &targetcli, &targetpath); if (!NT_STATUS_IS_OK(status)) { d_printf("Could not resolve %s\n", path); errno = ENOENT; TALLOC_FREE(frame); return -1; } /*d_printf(">>>fstat: resolved path as %s\n", targetpath);*/ if (!NT_STATUS_IS_OK(cli_qfileinfo_basic( targetcli, file->cli_fd, &mode, &size, NULL, &access_time_ts, &write_time_ts, &change_time_ts, &ino))) { time_t change_time, access_time, write_time; if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd, &mode, &size, &change_time, &access_time, &write_time))) { errno = EINVAL; TALLOC_FREE(frame); return -1; } change_time_ts = convert_time_t_to_timespec(change_time); access_time_ts = convert_time_t_to_timespec(access_time); write_time_ts = convert_time_t_to_timespec(write_time); } st->st_ino = ino; setup_stat(context, st, file->fname, size, mode); st->st_atime = convert_timespec_to_time_t(access_time_ts); st->st_ctime = convert_timespec_to_time_t(change_time_ts); st->st_mtime = convert_timespec_to_time_t(write_time_ts); st->st_dev = file->srv->dev; TALLOC_FREE(frame); return 0; }
off_t SMBC_lseek_ctx(SMBCCTX *context, SMBCFILE *file, off_t offset, int whence) { off_t size; char *server = NULL, *share = NULL, *user = NULL, *password = NULL; char *path = NULL; char *targetpath = NULL; struct cli_state *targetcli = NULL; TALLOC_CTX *frame = talloc_stackframe(); NTSTATUS status; if (!context || !context->internal->initialized) { errno = EINVAL; TALLOC_FREE(frame); return -1; } if (!file || !SMBC_dlist_contains(context->internal->files, file)) { errno = EBADF; TALLOC_FREE(frame); return -1; } if (!file->file) { errno = EINVAL; TALLOC_FREE(frame); return -1; /* Can't lseek a dir ... */ } switch (whence) { case SEEK_SET: file->offset = offset; break; case SEEK_CUR: file->offset += offset; break; case SEEK_END: /*d_printf(">>>lseek: parsing %s\n", file->fname);*/ if (SMBC_parse_path(frame, context, file->fname, NULL, &server, &share, &path, &user, &password, NULL)) { errno = EINVAL; TALLOC_FREE(frame); return -1; } /*d_printf(">>>lseek: resolving %s\n", path);*/ status = cli_resolve_path( frame, "", context->internal->auth_info, file->srv->cli, path, &targetcli, &targetpath); if (!NT_STATUS_IS_OK(status)) { d_printf("Could not resolve %s\n", path); errno = ENOENT; TALLOC_FREE(frame); return -1; } /*d_printf(">>>lseek: resolved path as %s\n", targetpath);*/ if (!NT_STATUS_IS_OK(cli_qfileinfo_basic( targetcli, file->cli_fd, NULL, &size, NULL, NULL, NULL, NULL, NULL))) { off_t b_size = size; if (!NT_STATUS_IS_OK(cli_getattrE(targetcli, file->cli_fd, NULL, &b_size, NULL, NULL, NULL))) { errno = EINVAL; TALLOC_FREE(frame); return -1; } else size = b_size; } file->offset = size + offset; break; default: errno = EINVAL; break; } TALLOC_FREE(frame); return file->offset; }
bool run_posix_append(int dummy) { struct cli_state *cli; const char *fname = "append"; NTSTATUS status; uint16_t fnum; SMB_OFF_T size; uint8_t c = '\0'; bool ret = false; printf("Starting POSIX_APPEND\n"); if (!torture_open_connection(&cli, 0)) { return false; } status = torture_setup_unix_extensions(cli); if (!NT_STATUS_IS_OK(status)) { printf("torture_setup_unix_extensions failed: %s\n", nt_errstr(status)); goto fail; } status = cli_ntcreate( cli, fname, 0, GENERIC_WRITE_ACCESS|GENERIC_READ_ACCESS|DELETE_ACCESS, FILE_ATTRIBUTE_NORMAL|FILE_FLAG_POSIX_SEMANTICS, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, FILE_OVERWRITE_IF, FILE_NON_DIRECTORY_FILE|FILE_DELETE_ON_CLOSE, 0, &fnum); if (!NT_STATUS_IS_OK(status)) { printf("cli_ntcreate failed: %s\n", nt_errstr(status)); goto fail; } /* * Write two bytes at offset 0. With bug 6898 we would end up * with a file of 2 byte length. */ status = cli_writeall(cli, fnum, 0, &c, 0, sizeof(c), NULL); if (!NT_STATUS_IS_OK(status)) { printf("cli_write failed: %s\n", nt_errstr(status)); goto fail; } status = cli_writeall(cli, fnum, 0, &c, 0, sizeof(c), NULL); if (!NT_STATUS_IS_OK(status)) { printf("cli_write failed: %s\n", nt_errstr(status)); goto fail; } status = cli_getattrE(cli, fnum, NULL, &size, NULL, NULL, NULL); if (!NT_STATUS_IS_OK(status)) { printf("cli_getatrE failed: %s\n", nt_errstr(status)); goto fail; } if (size != sizeof(c)) { printf("BUG: Writing with O_APPEND!!\n"); goto fail; } ret = true; fail: torture_close_connection(cli); return ret; }
static void do_atar(char *rname,char *lname,file_info *finfo1) { int fnum; SMB_BIG_UINT nread=0; char ftype; file_info2 finfo; BOOL close_done = False; BOOL shallitime=True; char data[65520]; int read_size = 65520; int datalen=0; struct timeval tp_start; GetTimeOfDay(&tp_start); ftype = '0'; /* An ordinary file ... */ if (finfo1) { finfo.size = finfo1 -> size; finfo.mode = finfo1 -> mode; finfo.uid = finfo1 -> uid; finfo.gid = finfo1 -> gid; finfo.mtime = finfo1 -> mtime; finfo.atime = finfo1 -> atime; finfo.ctime = finfo1 -> ctime; finfo.name = finfo1 -> name; } else { finfo.size = def_finfo.size; finfo.mode = def_finfo.mode; finfo.uid = def_finfo.uid; finfo.gid = def_finfo.gid; finfo.mtime = def_finfo.mtime; finfo.atime = def_finfo.atime; finfo.ctime = def_finfo.ctime; finfo.name = def_finfo.name; } if (dry_run) { DEBUG(3,("skipping file %s of size %12.0f bytes\n", finfo.name, (double)finfo.size)); shallitime=0; ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK); ntarf++; return; } fnum = cli_open(cli, rname, O_RDONLY, DENY_NONE); dos_clean_name(rname); if (fnum == -1) { DEBUG(0,("%s opening remote file %s (%s)\n", cli_errstr(cli),rname, cur_dir)); return; } finfo.name = string_create_s(strlen(rname)); if (finfo.name == NULL) { DEBUG(0, ("Unable to allocate space for finfo.name in do_atar\n")); return; } safe_strcpy(finfo.name,rname, strlen(rname)); if (!finfo1) { if (!cli_getattrE(cli, fnum, &finfo.mode, &finfo.size, NULL, &finfo.atime, &finfo.mtime)) { DEBUG(0, ("getattrE: %s\n", cli_errstr(cli))); return; } finfo.ctime = finfo.mtime; } DEBUG(3,("file %s attrib 0x%X\n",finfo.name,finfo.mode)); if (tar_inc && !(finfo.mode & aARCH)) { DEBUG(4, ("skipping %s - archive bit not set\n", finfo.name)); shallitime=0; } else if (!tar_system && (finfo.mode & aSYSTEM)) { DEBUG(4, ("skipping %s - system bit is set\n", finfo.name)); shallitime=0; } else if (!tar_hidden && (finfo.mode & aHIDDEN)) { DEBUG(4, ("skipping %s - hidden bit is set\n", finfo.name)); shallitime=0; } else { DEBUG(3,("getting file %s of size %.0f bytes as a tar file %s", finfo.name, (double)finfo.size, lname)); /* write a tar header, don't bother with mode - just set to 100644 */ writetarheader(tarhandle, rname, finfo.size, finfo.mtime, "100644 \0", ftype); while (nread < finfo.size && !close_done) { DEBUG(3,("nread=%.0f\n",(double)nread)); datalen = cli_read(cli, fnum, data, nread, read_size); if (datalen == -1) { DEBUG(0,("Error reading file %s : %s\n", rname, cli_errstr(cli))); break; } nread += datalen; /* if file size has increased since we made file size query, truncate read so tar header for this file will be correct. */ if (nread > finfo.size) { datalen -= nread - finfo.size; DEBUG(0,("File size change - truncating %s to %.0f bytes\n", finfo.name, (double)finfo.size)); } /* add received bits of file to buffer - dotarbuf will * write out in 512 byte intervals */ if (dotarbuf(tarhandle,data,datalen) != datalen) { DEBUG(0,("Error writing to tar file - %s\n", strerror(errno))); break; } if (datalen == 0) { DEBUG(0,("Error reading file %s. Got 0 bytes\n", rname)); break; } datalen=0; } /* pad tar file with zero's if we couldn't get entire file */ if (nread < finfo.size) { DEBUG(0, ("Didn't get entire file. size=%.0f, nread=%d\n", (double)finfo.size, (int)nread)); if (padit(data, sizeof(data), finfo.size - nread)) DEBUG(0,("Error writing tar file - %s\n", strerror(errno))); } /* round tar file to nearest block */ if (finfo.size % TBLOCK) dozerobuf(tarhandle, TBLOCK - (finfo.size % TBLOCK)); ttarf+=finfo.size + TBLOCK - (finfo.size % TBLOCK); ntarf++; } cli_close(cli, fnum); if (shallitime) { struct timeval tp_end; int this_time; /* if shallitime is true then we didn't skip */ if (tar_reset && !dry_run) (void) do_setrattr(finfo.name, aARCH, ATTRRESET); GetTimeOfDay(&tp_end); this_time = (tp_end.tv_sec - tp_start.tv_sec)*1000 + (tp_end.tv_usec - tp_start.tv_usec)/1000; get_total_time_ms += this_time; get_total_size += finfo.size; if (tar_noisy) { DEBUG(0, ("%12.0f (%7.1f kb/s) %s\n", (double)finfo.size, finfo.size / MAX(0.001, (1.024*this_time)), finfo.name)); } /* Thanks to Carel-Jan Engel ([email protected]) for this one */ DEBUG(3,("(%g kb/s) (average %g kb/s)\n", finfo.size / MAX(0.001, (1.024*this_time)), get_total_size / MAX(0.001, (1.024*get_total_time_ms)))); } }