📄 client.c
字号:
if (!p) return; p[1] = 0; pstrcat(mask2, f->name); pstrcat(mask2,"\\*"); add_to_do_list_queue(mask2); } return; } if (do_this_one(f)) { do_list_fn(f); }}/**************************************************************************** A wrapper around cli_list that adds recursion.****************************************************************************/void do_list(const char *mask,uint16 attribute,void (*fn)(file_info *),BOOL rec, BOOL dirs){ static int in_do_list = 0; struct cli_state *targetcli; pstring targetpath; if (in_do_list && rec) { fprintf(stderr, "INTERNAL ERROR: do_list called recursively when the recursive flag is true\n"); exit(1); } in_do_list = 1; do_list_recurse = rec; do_list_dirs = dirs; do_list_fn = fn; if (rec) { init_do_list_queue(); add_to_do_list_queue(mask); while (! do_list_queue_empty()) { /* * Need to copy head so that it doesn't become * invalid inside the call to cli_list. This * would happen if the list were expanded * during the call. * Fix from E. Jay Berkenbilt (ejb@ql.org) */ pstring head; pstrcpy(head, do_list_queue_head()); /* check for dfs */ if ( !cli_resolve_path( "", cli, head, &targetcli, targetpath ) ) { d_printf("do_list: [%s] %s\n", head, cli_errstr(cli)); remove_do_list_queue_head(); continue; } cli_list(targetcli, targetpath, attribute, do_list_helper, NULL); remove_do_list_queue_head(); if ((! do_list_queue_empty()) && (fn == display_finfo)) { char* next_file = do_list_queue_head(); char* save_ch = 0; if ((strlen(next_file) >= 2) && (next_file[strlen(next_file) - 1] == '*') && (next_file[strlen(next_file) - 2] == '\\')) { save_ch = next_file + strlen(next_file) - 2; *save_ch = '\0'; } d_printf("\n%s\n",next_file); if (save_ch) { *save_ch = '\\'; } } } } else { /* check for dfs */ if ( cli_resolve_path( "", cli, mask, &targetcli, targetpath ) ) { if (cli_list(targetcli, targetpath, attribute, do_list_helper, NULL) == -1) d_printf("%s listing %s\n", cli_errstr(targetcli), targetpath); } else d_printf("do_list: [%s] %s\n", mask, cli_errstr(cli)); } in_do_list = 0; reset_do_list_queue();}/**************************************************************************** Get a directory listing.****************************************************************************/static int cmd_dir(void){ uint16 attribute = aDIR | aSYSTEM | aHIDDEN; pstring mask; pstring buf; char *p=buf; int rc; dir_total = 0; if (strcmp(cur_dir, "\\") != 0) { pstrcpy(mask,cur_dir); if(mask[strlen(mask)-1]!='\\') pstrcat(mask,"\\"); } else { pstrcpy(mask, "\\"); } if (next_token_nr(NULL,buf,NULL,sizeof(buf))) { dos_format(p); if (*p == '\\') pstrcpy(mask,p + 1); else pstrcat(mask,p); } else { pstrcat(mask,"*"); } do_list(mask, attribute, display_finfo, recurse, True); rc = do_dskattr(); DEBUG(3, ("Total bytes listed: %.0f\n", dir_total)); return rc;}/**************************************************************************** Get a directory listing.****************************************************************************/static int cmd_du(void){ uint16 attribute = aDIR | aSYSTEM | aHIDDEN; pstring mask; pstring buf; char *p=buf; int rc; dir_total = 0; pstrcpy(mask,cur_dir); if(mask[strlen(mask)-1]!='\\') pstrcat(mask,"\\"); if (next_token_nr(NULL,buf,NULL,sizeof(buf))) { dos_format(p); if (*p == '\\') pstrcpy(mask,p); else pstrcat(mask,p); } else { pstrcat(mask,"*"); } do_list(mask, attribute, do_du, recurse, True); rc = do_dskattr(); d_printf("Total number of bytes: %.0f\n", dir_total); return rc;}/**************************************************************************** Get a file from rname to lname****************************************************************************/static int do_get(char *rname, char *lname, BOOL reget){ int handle = 0, fnum; BOOL newhandle = False; char *data; struct timeval tp_start; int read_size = io_bufsize; uint16 attr; SMB_OFF_T size; off_t start = 0; off_t nread = 0; int rc = 0; struct cli_state *targetcli; pstring targetname; if (lowercase) { strlower_m(lname); } if ( !cli_resolve_path( "", cli, rname, &targetcli, targetname ) ) { d_printf("Failed to open %s: %s\n", rname, cli_errstr(cli)); return 1; } GetTimeOfDay(&tp_start); if ( targetcli->dfsroot ) { pstring path; /* we need to refer to the full \server\share\path format for dfs shares */ pstrcpy( path, targetname ); cli_dfs_make_full_path( targetname, targetcli->desthost, targetcli->share, path); } fnum = cli_open(targetcli, targetname, O_RDONLY, DENY_NONE); if (fnum == -1) { d_printf("%s opening remote file %s\n",cli_errstr(cli),rname); return 1; } if(!strcmp(lname,"-")) { handle = fileno(stdout); } else { if (reget) { handle = sys_open(lname, O_WRONLY|O_CREAT, 0644); if (handle >= 0) { start = sys_lseek(handle, 0, SEEK_END); if (start == -1) { d_printf("Error seeking local file\n"); return 1; } } } else { handle = sys_open(lname, O_WRONLY|O_CREAT|O_TRUNC, 0644); } newhandle = True; } if (handle < 0) { d_printf("Error opening local file %s\n",lname); return 1; } if (!cli_qfileinfo(targetcli, fnum, &attr, &size, NULL, NULL, NULL, NULL, NULL) && !cli_getattrE(targetcli, fnum, &attr, &size, NULL, NULL, NULL)) { d_printf("getattrib: %s\n",cli_errstr(targetcli)); return 1; } DEBUG(1,("getting file %s of size %.0f as %s ", rname, (double)size, lname)); if(!(data = (char *)SMB_MALLOC(read_size))) { d_printf("malloc fail for size %d\n", read_size); cli_close(targetcli, fnum); return 1; } while (1) { int n = cli_read(targetcli, fnum, data, nread + start, read_size); if (n <= 0) break; if (writefile(handle,data, n) != n) { d_printf("Error writing local file\n"); rc = 1; break; } nread += n; } if (nread + start < size) { DEBUG (0, ("Short read when getting file %s. Only got %ld bytes.\n", rname, (long)nread)); rc = 1; } SAFE_FREE(data); if (!cli_close(targetcli, fnum)) { d_printf("Error %s closing remote file\n",cli_errstr(cli)); rc = 1; } if (newhandle) { close(handle); } if (archive_level >= 2 && (attr & aARCH)) { cli_setatr(cli, rname, attr & ~(uint16)aARCH, 0); } { struct timeval tp_end; int this_time; 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 += nread; DEBUG(1,("(%3.1f kb/s) (average %3.1f kb/s)\n", nread / (1.024*this_time + 1.0e-4), get_total_size / (1.024*get_total_time_ms))); } return rc;}/**************************************************************************** Get a file.****************************************************************************/static int cmd_get(void){ pstring lname; pstring rname; char *p; pstrcpy(rname,cur_dir); pstrcat(rname,"\\"); p = rname + strlen(rname); if (!next_token_nr(NULL,p,NULL,sizeof(rname)-strlen(rname))) { d_printf("get <filename>\n"); return 1; } pstrcpy(lname,p); dos_clean_name(rname); next_token_nr(NULL,lname,NULL,sizeof(lname)); return do_get(rname, lname, False);}/**************************************************************************** Do an mget operation on one file.****************************************************************************/static void do_mget(file_info *finfo){ pstring rname; pstring quest; pstring saved_curdir; pstring mget_mask; if (strequal(finfo->name,".") || strequal(finfo->name,"..")) return; if (abort_mget) { d_printf("mget aborted\n"); return; } if (finfo->mode & aDIR) slprintf(quest,sizeof(pstring)-1, "Get directory %s? ",finfo->name); else slprintf(quest,sizeof(pstring)-1, "Get file %s? ",finfo->name); if (prompt && !yesno(quest)) return; if (!(finfo->mode & aDIR)) { pstrcpy(rname,cur_dir); pstrcat(rname,finfo->name); do_get(rname, finfo->name, False); return; } /* handle directories */ pstrcpy(saved_curdir,cur_dir); pstrcat(cur_dir,finfo->name); pstrcat(cur_dir,"\\"); unix_format(finfo->name); if (lowercase) strlower_m(finfo->name); if (!directory_exist(finfo->name,NULL) && mkdir(finfo->name,0777) != 0) { d_printf("failed to create directory %s\n",finfo->name); pstrcpy(cur_dir,saved_curdir); return; } if (chdir(finfo->name) != 0) { d_printf("failed to chdir to directory %s\n",finfo->name); pstrcpy(cur_dir,saved_curdir); return; } pstrcpy(mget_mask,cur_dir); pstrcat(mget_mask,"*"); do_list(mget_mask, aSYSTEM | aHIDDEN | aDIR,do_mget,False, True); chdir(".."); pstrcpy(cur_dir,saved_curdir);}/**************************************************************************** View the file using the pager.****************************************************************************/static int cmd_more(void){ pstring rname,lname,pager_cmd; char *pager; int fd; int rc = 0; pstrcpy(rname,cur_dir); pstrcat(rname,"\\"); slprintf(lname,sizeof(lname)-1, "%s/smbmore.XXXXXX",tmpdir()); fd = smb_mkstemp(lname); if (fd == -1) { d_printf("failed to create temporary file for more\n"); return 1; } close(fd); if (!next_token_nr(NULL,rname+strlen(rname),NULL,sizeof(rname)-strlen(rname))) { d_printf("more <filename>\n"); unlink(lname); return 1; } dos_clean_name(rname); rc = do_get(rname, lname, False); pager=getenv("PAGER"); slprintf(pager_cmd,sizeof(pager_cmd)-1, "%s %s",(pager? pager:PAGER), lname); system(pager_cmd); unlink(lname); return rc;}/**************************************************************************** Do a mget command.****************************************************************************/static int cmd_mget(void){ uint16 attribute = aSYSTEM | aHIDDEN; pstring mget_mask; pstring buf; char *p=buf; *mget_mask = 0; if (recurse) attribute |= aDIR; abort_mget = False; while (next_token_nr(NULL,p,NULL,sizeof(buf))) { pstrcpy(mget_mask,cur_dir); if(mget_mask[strlen(mget_mask)-1]!='\\') pstrcat(mget_mask,"\\"); if (*p == '\\') pstrcpy(mget_mask,p); else pstrcat(mget_mask,p); do_list(mget_mask, attribute,do_mget,False,True); } if (!*mget_mask) { pstrcpy(mget_mask,cur_dir); if(mget_mask[strlen(mget_mask)-1]!='\\') pstrcat(mget_mask,"\\"); pstrcat(mget_mask,"*"); do_list(mget_mask, attribute,do_mget,False,True); } return 0;}/**************************************************************************** Make a directory of name "name".****************************************************************************/static BOOL do_mkdir(char *name){ struct cli_state *targetcli; pstring targetname; if ( !cli_resolve_path( "", cli, name, &targetcli, targetname ) ) { d_printf("mkdir %s: %s\n", name, cli_errstr(cli)); return False; } if (!cli_mkdir(targetcli, targetname)) { d_printf("%s making remote directory %s\n", cli_errstr(targetcli),name); return(False); } return(True);}/**************************************************************************** Show 8.3 name of a file.****************************************************************************/static BOOL do_altname(char *name){ pstring altname; if (!NT_STATUS_IS_OK(cli_qpathinfo_alt_name(cli, name, altname))) { d_printf("%s getting alt name for %s\n", cli_errstr(cli),name); return(False); } d_printf("%s\n", altname); return(True);}/**************************************************************************** Exit client.****************************************************************************/static int cmd_quit(void){ cli_cm_shutdown(); exit(0); /* NOTREACHED */ return 0;}/****************************************************************************
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -