Next Previous Contents

7. Archiving dynamic CONFIG_FILE

#Specification: CONFIG_FILE_LISTER / principles ([configf.cc,62])

Some configuration file are dynamic. For example the various DNS zone file are known only while reading /etc/named.conf. When linuxconf needs to operate on all known configuration file it uses the CONFIG_FILE_LISTER objects to trigger the definition of all dynamic CONFIG_FILE object. A CONFIG_FILE_LISTER is a static object. The constructor is used to register a void (*f)() function pointer. When linuxconf needs wants to enumerate all the CONFIG_FILE, it walks all the CONFIG_FILE_LISTER objects and trigger the function. All those function enumerate all the dynamic CONFIG_FILE they know and create a new CONFIG_FILE object for each. Linuxconf will delete the newly created CONFIG_FILE object later. So the function only care about creating the objects.

#Specbeg: CONFIG_FILE_LISTER / sample ([samples.cc,9])

        /*
            This sample code illustrate how dynamic (configuration dependant)
            configuration file are handled for system profile versioning.
            The trick is to use a CONFIG_FILE_LISTER object and to override
            the CONFIG_FILE::extract() member function of the configuration
            file providing the list of dynamic configuration file.
        
            For this sample, we create a CONFIG_FILE /tmp/file.list, which
            contains one file path per line.
        
            Note that the f_list CONFIG_FILE belongs to sub-system sample like
            all the dynamic configuraion file, so they are archived/extracted
            together.
        
            Note also that the sample_list() function create new CONFIG_FILE object.
            The CONFIG_FILE_LISTER framework will delete them later.
        
            For extraction to work, we must define a new CONFIG_FILE classe
            which overrire the CONFIG_FILE::extract(SSTREAM &) function.
        */
        
        static const char subsys_sample[]="sample";
        static LINUXCONF_SUBSYS sub (subsys_sample,"This is a sample sub-system");
        class CONFIG_FILE_SAMPLE: public CONFIG_FILE{
        public:
            CONFIG_FILE_SAMPLE(const char *_path, HELP_FILE &_help, int _opt, const char *_subsys)
                : CONFIG_FILE (_path,_help,_opt,_subsys)
            {
            }
            int extract (SSTREAM &ss);
        };
        static CONFIG_FILE_SAMPLE f_list ("/tmp/file.list",help_nil,CONFIGF_OPTIONAL,subsys_sample);
        static void sample_list()
        {
            FILE_CFG *fin = f_list.fopen ("r");
            if (fin != NULL){
                char path[PATH_MAX];
                while (fgets_strip(path,sizeof(path)-1,fin,NULL)!=NULL){
                    if (path[0] != '\0'){
                        new CONFIG_FILE (path,help_nil,CONFIGF_OPTIONAL,subsys_sample);
                    }
                }
                fclose (fin);
            }
        }
        
        int CONFIG_FILE_SAMPLE::extract (SSTREAM &ss)
        {
            CONFIG_FILE::extract (ss);
            sample_list();
            return 0;
        }
        
        
        static CONFIG_FILE_LISTER lister(sample_list);
        
        static void sample_listconfig()
        {
            xconf_notice (
                "We will present the list of all configuration file\n"
                "and we will see /tmp/file.list and its children in the list\n"
                "\n"
                "Edit this file and add various file path to it\n"
                );
            configf_list();
            xconf_notice ("Now, we will archive the sample sub-system");
            SSTRINGS tb;
            tb.add(new SSTRING(subsys_sample));
            SSTREAM_FILE ssout(stdout);
            configf_archive (tb,"/tmp/cfgarchive","--arch",ssout,true);
            xconf_notice ("Now we will delete /tmp/file.list\n"
                "and trigger an extraction");
            f_list.unlink();
            xconf_notice ("/tmp/file.list is deleted");
            configf_extract (tb,"/tmp/cfgarchive","--extr");
        }

CONFIG_FILE_LISTER::CONFIG_FILE_LISTER()

        PUBLIC CONFIG_FILE_LISTER::CONFIG_FILE_LISTER(
                void(*f)())
        

Next Previous Contents