1 /* 2 * lftp - file transfer program 3 * 4 * Copyright (c) 1996-2015 by Alexander V. Lukyanov (lav@yars.free.net) 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * This program is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with this program. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 #ifndef FILTER_H 21 #define FILTER_H 22 23 #include "ProcWait.h" 24 #include "ArgV.h" 25 26 struct FileTimestamp; 27 28 class FDStream 29 { 30 bool close_when_done; 31 32 protected: 33 bool closed; 34 35 void DoCloseFD(); 36 void SetFD(int new_fd,bool c); 37 38 public: 39 int fd; 40 xstring_c name; 41 xstring_c full_name; 42 xstring_c cwd; 43 xstring error_text; 44 const char *status; 45 error()46 bool error() { return error_text!=0; } 47 getfd()48 virtual int getfd() { return fd; } is_closed()49 bool is_closed() const { return closed; } 50 51 FDStream(); 52 FDStream(int new_fd,const char *new_name); 53 virtual ~FDStream(); 54 55 void MakeErrorText(int e=0); 56 bool NonFatalError(int err); set_status(const char * str)57 void set_status(const char *str) { status=str; } clear_status()58 void clear_status() { status=0; } CloseWhenDone()59 void CloseWhenDone() { close_when_done=true; } 60 61 void SetCwd(const char *); GetCwd()62 const char *GetCwd() const { return cwd; } 63 get_size()64 virtual off_t get_size() { return -1; } setmtime(const FileTimestamp &)65 virtual void setmtime(const FileTimestamp &) {} can_setmtime()66 virtual bool can_setmtime() { return false; } remove_if_empty()67 virtual void remove_if_empty() {} remove()68 virtual void remove() {} 69 virtual bool Done(); usesfd(int n_fd)70 virtual bool usesfd(int n_fd) { return fd==n_fd; } 71 virtual void Kill(int=SIGTERM) {} GetProcGroup()72 virtual pid_t GetProcGroup() const { return 0; } broken()73 virtual bool broken() { return false; } can_seek()74 virtual bool can_seek() { return false; } revert_backup()75 virtual void revert_backup() {} remove_backup()76 virtual void remove_backup() {} 77 }; 78 79 class OutputFilter : public FDStream 80 { 81 Ref<ArgV> a; 82 ProcWait *w; 83 pid_t pg; 84 85 Ref<FDStream> my_second; 86 const Ref<FDStream>& second; 87 88 bool stderr_to_stdout; 89 bool stdout_to_null; 90 91 void Init(); 92 93 virtual void Parent(int *p); // what to do with pipe if parent 94 virtual void Child (int *p); // same for child 95 96 protected: 97 int second_fd; 98 99 public: 100 OutputFilter(const char *filter,int second_fd=-1); 101 OutputFilter(const char *filter,FDStream *second); 102 OutputFilter(const char *filter,const Ref<FDStream>& second); 103 OutputFilter(ArgV *a,int second_fd=-1); 104 OutputFilter(ArgV *a,FDStream *second); 105 OutputFilter(ArgV *a,const Ref<FDStream>& second); 106 virtual ~OutputFilter(); 107 StderrToStdout()108 void StderrToStdout() { stderr_to_stdout=true; } StdoutToNull()109 void StdoutToNull() { stdout_to_null=true; } 110 111 int getfd(); 112 bool Done(); 113 114 bool usesfd(int n_fd); 115 void Kill(int sig=SIGTERM); GetProcGroup()116 pid_t GetProcGroup() const { return pg; } SetProcGroup(pid_t new_pg)117 void SetProcGroup(pid_t new_pg) { pg=new_pg; } GetProcState()118 ProcWait::State GetProcState() { return w->GetState(); } GetProcExitCode()119 int GetProcExitCode() { return w->GetInfo()>>8; } 120 121 bool broken(); 122 }; 123 124 class InputFilter : public OutputFilter 125 { 126 virtual void Parent(int *p); 127 virtual void Child (int *p); 128 public: 129 InputFilter(const char *filter,int second_fd=-1) OutputFilter(filter,second_fd)130 : OutputFilter(filter,second_fd) {} InputFilter(const char * filter,FDStream * second)131 InputFilter(const char *filter,FDStream *second) 132 : OutputFilter(filter,second) {} 133 InputFilter(ArgV *a,int second_fd=-1) OutputFilter(a,second_fd)134 : OutputFilter(a,second_fd) {} InputFilter(ArgV * a,FDStream * second)135 InputFilter(ArgV *a,FDStream *second) 136 : OutputFilter(a,second) {} 137 }; 138 139 class FileStream : public FDStream 140 { 141 int mode; 142 mode_t create_mode; 143 bool do_lock; 144 bool no_keep_backup; 145 146 xstring_c backup_file; 147 mode_t old_file_mode; 148 public: 149 FileStream(const char *fname,int open_mode); 150 ~FileStream(); 151 152 void setmtime(const FileTimestamp &); can_setmtime()153 bool can_setmtime() { return true; } 154 void remove_if_empty(); 155 void remove(); 156 int getfd(); 157 bool can_seek(); 158 off_t get_size(); 159 void set_lock(bool flag=true) { do_lock=flag; } set_create_mode(mode_t m)160 void set_create_mode(mode_t m) { create_mode=m; } 161 void revert_backup(); 162 void remove_backup(); dont_keep_backup()163 void dont_keep_backup() { no_keep_backup=true; } 164 }; 165 166 #endif /* FILTER_H */ 167