Пример #1
0
void SWMgr::loadConfigDir(const char *ipath)
{
	DIR *dir;
	struct dirent *ent;
	SWBuf newmodfile;
 
	if ((dir = opendir(ipath))) {
		rewinddir(dir);
		while ((ent = readdir(dir))) {
			//check whether it ends with .conf, if it doesn't skip it!
			if (!ent->d_name || (strlen(ent->d_name) <= 5) || strncmp(".conf", (ent->d_name + strlen(ent->d_name) - 5), 5 )) {
				continue;
			}
			
			newmodfile = ipath;
			if ((ipath[strlen(ipath)-1] != '\\') && (ipath[strlen(ipath)-1] != '/'))
				newmodfile += "/";
			newmodfile += ent->d_name;
			if (config) {
				SWConfig tmpConfig(newmodfile.c_str());
				*config += tmpConfig;
			}
			else	config = myconfig = new SWConfig(newmodfile.c_str());
		}
		closedir(dir);
		
		if (!config) {	// if no .conf file exist yet, create a default
			newmodfile = ipath;
			if ((ipath[strlen(ipath)-1] != '\\') && (ipath[strlen(ipath)-1] != '/'))
				newmodfile += "/";
			newmodfile += "globals.conf";
			config = myconfig = new SWConfig(newmodfile.c_str());
		}
	}
}
void DeviceManagementNode::update(bool read) 
{
    if (!read && !modified) {
        // no work to be done
        return;
    }

    StringBuffer fileName(currentDir);
    concatDirs(fileName, configFile.c_str());
    const char* fname = fileName.c_str();
    FILE* file = fopen(fname, "r");
    
    if (!file) {
        // Maybe the file or directory does not exist: create it and try again.
        LOG.debug("Could not open file, create it empty: '%s'", fileName.c_str());
        mkdirAll(currentDir);
        file = fopen(fname, "w+");  // "w+" to create the file and be able to read/write
        
        // Anyway, set the last error so Clients can know the file was not found.
        setErrorF(ERR_DM_TREE_NOT_AVAILABLE, "File not found: '%s'", fileName.c_str());
    }

    if (file) {
        // Open a temp file in writing mode if we must update the config
        if (!read) {
            fclose(file);
            fileName.append(".tmp");
            file = fopen(fileName, "w");
            if (!file) {
                setErrorF(ERR_INVALID_CONTEXT, "Error opening file: '%s'", fileName.c_str());
                LOG.error("%s", getLastErrorMsg());
                return;
            }
        }

        if (read) {
            // Note: don't create local buffers too big, the Symbian stack size is limited!
            char buffer[128];

            lines->clear();
            while (fgets(buffer, sizeof(buffer), file)) {
                char *eol = strchr(buffer, '\r');
                if (!eol) {
                    eol = strchr(buffer, '\n');
                }
                if (eol) {
                    *eol = 0;
                }
                line newline(buffer);
                lines->add(newline);
            }
            fclose(file);
        } 
        else {
            int i = 0;
            while (true) {
                line *curr = (line *)lines->get(i);
                if (!curr) {
                    break;
                }
                fprintf(file, "%s\n", curr->getLine());

                i++;
            }
            fflush(file);
            if (!ferror(file)) {
                StringBuffer tmpConfig(configFile);
                tmpConfig += ".tmp";

                // Both files must be closed for the rename.
                int ret = fclose(file);
                if (ret) {
                    setErrorF(ret, "Error (%d) closing file: '%s'", ret, fileName.c_str());
                    return;
                }

                renameFileInCwd( tmpConfig.c_str(), configFile.c_str());
            }
            else {
                fclose(file);
            }
        }
    }
    else {
        setErrorF(ERR_DM_TREE_NOT_AVAILABLE, "Error opening file: '%s'", fileName.c_str());
        LOG.error("%s", getLastErrorMsg());
        return;
    }
}