Skip to content

Commit

Permalink
[ISSUE #11967] Can not aquire the specific config (#12025)
Browse files Browse the repository at this point in the history
* [ISSUE #11967] Can not aquire the specific config

* [ISSUE #11967] Can not aquire the specific config

* [ISSUE #11967] Can not aquire the specific config

* [ISSUE #11967] Can not aquire the specific config

* [ISSUE #11967] Can not aquire the specific config
  • Loading branch information
syshenyao authored May 15, 2024
1 parent 9a6ec0c commit 6fe4363
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.alibaba.nacos.config.server.service.dump.disk;

import com.alibaba.nacos.api.utils.StringUtils;
import com.alibaba.nacos.common.pathencoder.PathEncoderManager;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.sys.env.EnvUtil;
Expand Down Expand Up @@ -65,6 +66,10 @@ public void saveToDisk(String dataId, String group, String tenant, String conten
* Returns the path of the server cache file.
*/
private static File targetFile(String dataId, String group, String tenant) {
// fix https://github.com/alibaba/nacos/issues/10067
dataId = PathEncoderManager.getInstance().encode(dataId);
group = PathEncoderManager.getInstance().encode(group);
tenant = PathEncoderManager.getInstance().encode(tenant);
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(EnvUtil.getNacosHome(), BASE_DIR);
Expand All @@ -81,6 +86,10 @@ private static File targetFile(String dataId, String group, String tenant) {
* Returns the path of cache file in server.
*/
private static File targetBetaFile(String dataId, String group, String tenant) {
// fix https://github.com/alibaba/nacos/issues/10067
dataId = PathEncoderManager.getInstance().encode(dataId);
group = PathEncoderManager.getInstance().encode(group);
tenant = PathEncoderManager.getInstance().encode(tenant);
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(EnvUtil.getNacosHome(), BETA_DIR);
Expand All @@ -97,6 +106,10 @@ private static File targetBetaFile(String dataId, String group, String tenant) {
* Returns the path of the tag cache file in server.
*/
private static File targetTagFile(String dataId, String group, String tenant, String tag) {
// fix https://github.com/alibaba/nacos/issues/10067
dataId = PathEncoderManager.getInstance().encode(dataId);
group = PathEncoderManager.getInstance().encode(group);
tenant = PathEncoderManager.getInstance().encode(tenant);
File file = null;
if (StringUtils.isBlank(tenant)) {
file = new File(EnvUtil.getNacosHome(), TAG_DIR);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 1999-2023 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.alibaba.nacos.config.server.service.dump.disk;

import junit.framework.TestCase;
import org.junit.Before;

import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.file.Path;
import java.nio.file.Paths;

public class ConfigRawDiskServiceTest extends TestCase {

private String cachedOsName;

@Before
public void setUp() throws Exception {
cachedOsName = System.getProperty("os.name");
}

private boolean isWindows() {
return cachedOsName.toLowerCase().startsWith("win");
}

/**
* 测试获取文件路径.
*/
public void testTargetFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = ConfigRawDiskService.class.getDeclaredMethod("targetFile", String.class, String.class, String.class);
method.setAccessible(true);
File result = (File) method.invoke(null, "aaaa?dsaknkf", "aaaa*dsaknkf", "aaaa:dsaknkf");
// 分解路径
Path path = Paths.get(result.getPath());
Path parent = path.getParent();
Path grandParent = parent.getParent();
// 获取最后三段路径
String lastSegment = path.getFileName().toString();
String secondLastSegment = parent.getFileName().toString();
String thirdLastSegment = grandParent.getFileName().toString();
assertEquals(isWindows() ? "aaaa%A3%dsaknkf" : thirdLastSegment, thirdLastSegment);
assertEquals(isWindows() ? "aaaa%A4%dsaknkf" : secondLastSegment, secondLastSegment);
assertEquals(isWindows() ? "aaaa%A5%dsaknkf" : lastSegment, lastSegment);
}

/**
* 测试获取beta文件路径.
*/
public void testTargetBetaFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = ConfigRawDiskService.class.getDeclaredMethod("targetBetaFile", String.class, String.class, String.class);
method.setAccessible(true);
File result = (File) method.invoke(null, "aaaa?dsaknkf", "aaaa*dsaknkf", "aaaa:dsaknkf");
// 分解路径
Path path = Paths.get(result.getPath());
Path parent = path.getParent();
Path grandParent = parent.getParent();
// 获取最后三段路径
String lastSegment = path.getFileName().toString();
String secondLastSegment = parent.getFileName().toString();
String thirdLastSegment = grandParent.getFileName().toString();
assertEquals(isWindows() ? "aaaa%A3%dsaknkf" : thirdLastSegment, thirdLastSegment);
assertEquals(isWindows() ? "aaaa%A4%dsaknkf" : secondLastSegment, secondLastSegment);
assertEquals(isWindows() ? "aaaa%A5%dsaknkf" : lastSegment, lastSegment);

}

/**
* 测试获取tag文件路径.
* @throws NoSuchMethodException 方法不存在异常
* @throws IllegalAccessException 非法访问异常
* @throws InvocationTargetException 目标异常
*/
public void testTargetTagFile() throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = ConfigRawDiskService.class.getDeclaredMethod("targetTagFile", String.class, String.class, String.class, String.class);
method.setAccessible(true);
File result = (File) method.invoke(null, "aaaa?dsaknkf", "aaaa*dsaknkf", "aaaa:dsaknkf", "aaaadsaknkf");
// 分解路径
Path path = Paths.get(result.getPath());
Path parent = path.getParent();
Path grandParent = parent.getParent();
Path greatGrandParent = grandParent.getParent();
// 获取最后四段路径
String secondLastSegment = parent.getFileName().toString();
String thirdLastSegment = grandParent.getFileName().toString();
String fourthLastSegment = greatGrandParent.getFileName().toString();
assertEquals(isWindows() ? "aaaa%A3%dsaknkf" : fourthLastSegment, fourthLastSegment);
assertEquals(isWindows() ? "aaaa%A4%dsaknkf" : thirdLastSegment, thirdLastSegment);
assertEquals(isWindows() ? "aaaa%A5%dsaknkf" : secondLastSegment, secondLastSegment);
String lastSegment = path.getFileName().toString();
assertEquals("aaaadsaknkf", lastSegment);
}
}

0 comments on commit 6fe4363

Please sign in to comment.