Commit 7b2e9491 authored by Kazunori Yamamoto's avatar Kazunori Yamamoto
Browse files

Set EFS flag to indicate that filenames and comments are UTF-8 encoded

parent 67a9ab49
......@@ -64,6 +64,8 @@ func createZipEntry(archive *zip.Writer, fileName string) error {
}
fh.Name = fileName
fh.Extra = createZipExtra(fi)
// Set EFS flag to indicate that filenames and comments are UTF-8 encoded
fh.Flags = 0x800
switch fi.Mode() & os.ModeType {
case os.ModeDir:
......
......@@ -15,38 +15,73 @@ import (
var testZipFileContent = []byte("test content")
func createTestFile(t *testing.T) string {
err := ioutil.WriteFile("test_file.txt", testZipFileContent, 0640)
type CharsetByte int
const (
SingleByte CharsetByte = iota
MultiBytes
)
func createTestFile(t *testing.T, csb CharsetByte) string {
name := "test_file.txt"
if csb == MultiBytes {
name = "テストファイル.txt"
}
err := ioutil.WriteFile(name, testZipFileContent, 0640)
assert.NoError(t, err)
return "test_file.txt"
return name
}
func createSymlinkFile(t *testing.T) string {
err := os.Symlink("old_symlink", "new_symlink")
func createSymlinkFile(t *testing.T, csb CharsetByte) string {
name := "new_symlink"
if csb == MultiBytes {
name = "新しいシンボリックリンク"
}
err := os.Symlink("old_symlink", name)
assert.NoError(t, err)
return "new_symlink"
return name
}
func createTestDirectory(t *testing.T) string {
err := os.Mkdir("test_directory", 0711)
func createTestDirectory(t *testing.T, csb CharsetByte) string {
name := "test_directory"
if csb == MultiBytes {
name = "テストディレクトリ"
}
err := os.Mkdir(name, 0711)
assert.NoError(t, err)
return "test_directory"
return name
}
func createTestPipe(t *testing.T) string {
err := syscall.Mkfifo("test_pipe", 0600)
func createTestPipe(t *testing.T, csb CharsetByte) string {
name := "test_pipe"
if csb == MultiBytes {
name = "テストパイプ"
}
err := syscall.Mkfifo(name, 0600)
assert.NoError(t, err)
return "test_pipe"
return name
}
func createTestGitPathFile(t *testing.T) string {
err := os.Mkdir(".git", 0711)
assert.NoError(t, err)
func createTestGitPathFile(t *testing.T, csb CharsetByte) string {
_, err := os.Stat(".git")
if err != nil {
err = os.Mkdir(".git", 0711)
assert.NoError(t, err)
}
name := ".git/test_file"
if csb == MultiBytes {
name = ".git/テストファイル"
}
err = ioutil.WriteFile(".git/test_file", testZipFileContent, 0640)
err = ioutil.WriteFile(name, testZipFileContent, 0640)
assert.NoError(t, err)
return ".git/test_file"
return name
}
func testInWorkDir(t *testing.T, testCase func(t *testing.T, fileName string)) {
......@@ -71,10 +106,14 @@ func testInWorkDir(t *testing.T, testCase func(t *testing.T, fileName string)) {
func TestZipCreate(t *testing.T) {
testInWorkDir(t, func(t *testing.T, fileName string) {
paths := []string{
createTestFile(t),
createSymlinkFile(t),
createTestDirectory(t),
createTestPipe(t),
createTestFile(t, SingleByte),
createSymlinkFile(t, SingleByte),
createTestDirectory(t, SingleByte),
createTestPipe(t, SingleByte),
createTestFile(t, MultiBytes),
createSymlinkFile(t, MultiBytes),
createTestDirectory(t, MultiBytes),
createTestPipe(t, MultiBytes),
"non_existing_file.txt",
}
err := CreateZipFile(fileName, paths)
......@@ -84,7 +123,7 @@ func TestZipCreate(t *testing.T) {
require.NoError(t, err)
defer archive.Close()
assert.Len(t, archive.File, 3)
assert.Len(t, archive.File, 6)
assert.Equal(t, "test_file.txt", archive.File[0].Name)
assert.Equal(t, os.FileMode(0640), archive.File[0].Mode().Perm())
......@@ -95,6 +134,16 @@ func TestZipCreate(t *testing.T) {
assert.Equal(t, "test_directory/", archive.File[2].Name)
assert.NotEmpty(t, archive.File[2].Extra)
assert.True(t, archive.File[2].Mode().IsDir())
assert.Equal(t, "テストファイル.txt", archive.File[3].Name)
assert.Equal(t, os.FileMode(0640), archive.File[3].Mode().Perm())
assert.NotEmpty(t, archive.File[3].Extra)
assert.Equal(t, "新しいシンボリックリンク", archive.File[4].Name)
assert.Equal(t, "テストディレクトリ/", archive.File[5].Name)
assert.NotEmpty(t, archive.File[5].Extra)
assert.True(t, archive.File[5].Mode().IsDir())
})
}
......@@ -106,7 +155,8 @@ func TestZipCreateWithGitPath(t *testing.T) {
defer logrus.SetOutput(output)
paths := []string{
createTestGitPathFile(t),
createTestGitPathFile(t, SingleByte),
createTestGitPathFile(t, MultiBytes),
}
err := CreateZipFile(fileName, paths)
require.NoError(t, err)
......@@ -117,10 +167,14 @@ func TestZipCreateWithGitPath(t *testing.T) {
require.NoError(t, err)
defer archive.Close()
assert.Len(t, archive.File, 1)
assert.Len(t, archive.File, 2)
assert.Equal(t, ".git/test_file", archive.File[0].Name)
assert.Equal(t, os.FileMode(0640), archive.File[0].Mode().Perm())
assert.NotEmpty(t, archive.File[0].Extra)
assert.Equal(t, ".git/テストファイル", archive.File[1].Name)
assert.Equal(t, os.FileMode(0640), archive.File[1].Mode().Perm())
assert.NotEmpty(t, archive.File[1].Extra)
})
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment