| 72 | } |
| 73 | |
| 74 | String ConfigPackageUtility::CreateStage(const String& packageName, const Dictionary::Ptr& files) |
| 75 | { |
| 76 | String stageName = Utility::NewUniqueID(); |
| 77 | |
| 78 | String path = GetPackageDir() + "/" + packageName; |
| 79 | |
| 80 | if (!Utility::PathExists(path)) |
| 81 | BOOST_THROW_EXCEPTION(std::invalid_argument("Package does not exist.")); |
| 82 | |
| 83 | path += "/" + stageName; |
| 84 | |
| 85 | Utility::MkDirP(path, 0700); |
| 86 | Utility::MkDirP(path + "/conf.d", 0700); |
| 87 | Utility::MkDirP(path + "/zones.d", 0700); |
| 88 | WriteStageConfig(packageName, stageName); |
| 89 | |
| 90 | bool foundDotDot = false; |
| 91 | |
| 92 | if (files) { |
| 93 | ObjectLock olock(files); |
| 94 | for (const Dictionary::Pair& kv : files) { |
| 95 | if (ContainsDotDot(kv.first)) { |
| 96 | foundDotDot = true; |
| 97 | break; |
| 98 | } |
| 99 | |
| 100 | String filePath = path + "/" + kv.first; |
| 101 | |
| 102 | Log(LogInformation, "ConfigPackageUtility") |
| 103 | << "Updating configuration file: " << filePath; |
| 104 | |
| 105 | // Pass the directory and generate a dir tree, if it does not already exist |
| 106 | Utility::MkDirP(Utility::DirName(filePath), 0750); |
| 107 | std::ofstream fp(filePath.CStr(), std::ofstream::out | std::ostream::binary | std::ostream::trunc); |
| 108 | fp << kv.second; |
| 109 | fp.close(); |
| 110 | } |
| 111 | } |
| 112 | |
| 113 | if (foundDotDot) { |
| 114 | Utility::RemoveDirRecursive(path); |
| 115 | BOOST_THROW_EXCEPTION(std::invalid_argument("Path must not contain '..'.")); |
| 116 | } |
| 117 | |
| 118 | return stageName; |
| 119 | } |
| 120 | |
| 121 | void ConfigPackageUtility::WritePackageConfig(const String& packageName) |
| 122 | { |