1 /*
2  * lftp - file transfer program
3  *
4  * Copyright (c) 1996-2012 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 FINDJOB_H
21 #define FINDJOB_H
22 
23 #include "Job.h"
24 #include "buffer.h"
25 #include "ArgV.h"
26 #include "GetFileInfo.h"
27 #include "PatternSet.h"
28 
29 class FinderJob : public SessionJob
30 {
31    FileAccessRef my_session;
32    FileAccess::Path orig_init_dir;
33 
34    xstring_c dir;
35    int errors;
36    SMTaskRef<GetFileInfo> li;
37 
38    class place
39       {
40 	 friend class FinderJob;
41 
42 	 xstring_c path;
43 	 Ref<FileSet> fset;
44 
place(const char * p,FileSet * f)45 	 place(const char *p,FileSet *f) : path(p), fset(f) {}
46       };
47 
48    RefArray<place> stack;
49 
50    void Up();
51    void Down(const char *d);
52    void Push(FileSet *f);
53 
Enter(const char * d)54    virtual void Enter(const char *d) { }
Exit()55    virtual void Exit() { }
56 
57    bool depth_done;
58    unsigned file_info_need;
59    /* In certain circumstances, we can skip a LIST altogether and just
60     * pass argument names on: we don't need anything other than the name
61     * (no other file_info_needs) and we're not recursing (which would imply
62     * needing the type.)  This means arguments that don't actually exist
63     * get passed on; if this is inappropriate (ie for a simple Find),
64     * call ValidateArgs(). */
65    bool validate_args;
66 
67    Ref<PatternSet> exclude;
68 
69 protected:
70    enum state_t { START_INFO, INFO, LOOP, PROCESSING, WAIT, DONE };
71    state_t state;
72 
73    const char *op;
74    FileAccessRefC session;
75    FileAccess::Path init_dir;
76 
77    enum prf_res { PRF_FATAL, PRF_ERR, PRF_OK, PRF_WAIT, PRF_LATER };
78    virtual prf_res ProcessFile(const char *d,const FileInfo *fi);
ProcessList(FileSet * f)79    virtual void ProcessList(FileSet *f) { }
Finish()80    virtual void Finish() {};
81 
82    bool show_sl;
83 
84    bool depth_first;
85    bool use_cache;
86    bool quiet;
87    int maxdepth;
88 
89    void NextDir(const char *d);
GetCur()90    const char *GetCur() const { return dir; }
Need(unsigned need)91    void Need(unsigned need) { file_info_need=need; }
ValidateArgs()92    void ValidateArgs() { validate_args=true; }
93 
ProcessingURL()94    bool ProcessingURL() { return session!=SessionJob::session; }
95 
96 public:
97    int Do();
Done()98    int Done() { return state==DONE; }
ExitCode()99    int ExitCode() { return state!=DONE || (errors && !quiet); }
100 
101    void Init();
102    FinderJob(FileAccess *s);
PrepareToDie()103    void PrepareToDie() { session->Close(); SessionJob::PrepareToDie(); }
104    ~FinderJob();
105 
106    void ShowRunStatus(const SMTaskRef<StatusLine>&);
107    xstring& FormatStatus(xstring&,int,const char *);
108 
BeQuiet()109    void BeQuiet() { quiet=true; }
SetExclude(PatternSet * p)110    void SetExclude(PatternSet *p) { exclude = p; }
set_maxdepth(int _maxdepth)111    void set_maxdepth(int _maxdepth) { maxdepth = _maxdepth; }
112 
113    void Fg();
114    void Bg();
115 };
116 
117 class FinderJob_List : public FinderJob
118 {
119    SMTaskRef<IOBuffer> buf;
120    Ref<ArgV> args;
121    bool long_listing;
122 protected:
123    prf_res ProcessFile(const char *d,const FileInfo *fi);
124    void Finish();
125 
126 public:
127    FinderJob_List(FileAccess *s,ArgV *a,FDStream *o);
128    void DoLongListing(bool yes=true) { long_listing=yes; }
129 
Done()130    int Done() { return FinderJob::Done() && buf->Done(); }
131 };
132 
133 #endif //FINDJOB_H
134