Skip to content

Commit

Permalink
Add readWriteFileMode constant
Browse files Browse the repository at this point in the history
  • Loading branch information
ramikg authored and sfc-gh-pfus committed Aug 2, 2023
1 parent cd08ebd commit d9cae96
Show file tree
Hide file tree
Showing 15 changed files with 38 additions and 37 deletions.
4 changes: 2 additions & 2 deletions azure_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func (util *snowflakeAzureClient) uploadFile(
})
} else {
var f *os.File
f, err = os.OpenFile(dataFile, os.O_RDONLY, 0666)
f, err = os.OpenFile(dataFile, os.O_RDONLY, readWriteFileMode)
if err != nil {
return err
}
Expand Down Expand Up @@ -273,7 +273,7 @@ func (util *snowflakeAzureClient) nativeDownloadFile(
if meta.mockAzureClient != nil {
blobClient = meta.mockAzureClient
}
f, err := os.OpenFile(fullDstFileName, os.O_CREATE|os.O_WRONLY, 0666)
f, err := os.OpenFile(fullDstFileName, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions encrypt_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func encryptFile(
if err != nil {
return nil, "", err
}
infile, err := os.OpenFile(filename, os.O_CREATE|os.O_RDONLY, 0666)
infile, err := os.OpenFile(filename, os.O_CREATE|os.O_RDONLY, readWriteFileMode)
if err != nil {
return nil, "", err
}
Expand Down Expand Up @@ -232,7 +232,7 @@ func decryptFile(
return "", err
}
defer tmpOutputFile.Close()
infile, err := os.OpenFile(filename, os.O_RDONLY, 0666)
infile, err := os.OpenFile(filename, os.O_RDONLY, readWriteFileMode)
if err != nil {
return "", err
}
Expand Down
14 changes: 7 additions & 7 deletions encrypt_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func TestEncryptDecryptFile(t *testing.T) {
data := "test data"
inputFile := "test_encrypt_decrypt_file"

fd, err := os.OpenFile(inputFile, os.O_CREATE|os.O_WRONLY, 0666)
fd, err := os.OpenFile(inputFile, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand All @@ -54,7 +54,7 @@ func TestEncryptDecryptFile(t *testing.T) {
}
defer os.Remove(decryptedFile)

fd, err = os.OpenFile(decryptedFile, os.O_RDONLY, 0666)
fd, err = os.OpenFile(decryptedFile, os.O_RDONLY, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -144,7 +144,7 @@ func encryptDecryptFile(t *testing.T, encMat snowflakeFileEncryption, expected i
defer os.Remove(decryptedFile)

cnt := 0
fd, err := os.OpenFile(decryptedFile, os.O_RDONLY, 0666)
fd, err := os.OpenFile(decryptedFile, os.O_RDONLY, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand All @@ -168,7 +168,7 @@ func generateKLinesOfNByteRows(numLines int, numBytes int, tmpDir string) (strin
}
}
fname := path.Join(tmpDir, "file"+strconv.FormatInt(int64(numLines*numBytes), 10))
f, err := os.OpenFile(fname, os.O_CREATE|os.O_WRONLY, 0666)
f, err := os.OpenFile(fname, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
return "", err
}
Expand All @@ -191,7 +191,7 @@ func generateKLinesOfNFiles(k int, n int, compress bool, tmpDir string) (string,
}
for i := 0; i < n; i++ {
fname := path.Join(tmpDir, "file"+strconv.FormatInt(int64(i), 10))
f, err := os.OpenFile(fname, os.O_CREATE|os.O_WRONLY, 0666)
f, err := os.OpenFile(fname, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
return "", err
}
Expand Down Expand Up @@ -233,12 +233,12 @@ func generateKLinesOfNFiles(k int, n int, compress bool, tmpDir string) (string,
io.ReadAll(gzipErr)
gzipCmd.Wait()
} else {
fOut, err := os.OpenFile(fname+".gz", os.O_CREATE|os.O_WRONLY, 0666)
fOut, err := os.OpenFile(fname+".gz", os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
return "", err
}
w := gzip.NewWriter(fOut)
fIn, err := os.OpenFile(fname, os.O_RDONLY, 0666)
fIn, err := os.OpenFile(fname, os.O_RDONLY, readWriteFileMode)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion file_transfer_agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func TestUnitDownloadWithInvalidLocalPath(t *testing.T) {
}
defer os.RemoveAll(tmpDir)
testData := filepath.Join(tmpDir, "data.txt")
f, err := os.OpenFile(testData, os.O_CREATE|os.O_RDWR, 0666)
f, err := os.OpenFile(testData, os.O_CREATE|os.O_RDWR, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand Down
7 changes: 4 additions & 3 deletions file_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ type snowflakeFileUtil struct {
}

const (
fileChunkSize = 16 * 4 * 1024
fileChunkSize = 16 * 4 * 1024
readWriteFileMode os.FileMode = 0666
)

func (util *snowflakeFileUtil) compressFileWithGzipFromStream(srcStream **bytes.Buffer) (*bytes.Buffer, int, error) {
Expand All @@ -39,12 +40,12 @@ func (util *snowflakeFileUtil) compressFileWithGzip(fileName string, tmpDir stri
basename := baseName(fileName)
gzipFileName := filepath.Join(tmpDir, basename+"_c.gz")

fr, err := os.OpenFile(fileName, os.O_RDONLY, 0666)
fr, err := os.OpenFile(fileName, os.O_RDONLY, readWriteFileMode)
if err != nil {
return "", -1, err
}
defer fr.Close()
fw, err := os.OpenFile(gzipFileName, os.O_WRONLY|os.O_CREATE, 0666)
fw, err := os.OpenFile(gzipFileName, os.O_WRONLY|os.O_CREATE, readWriteFileMode)
if err != nil {
return "", -1, err
}
Expand Down
4 changes: 2 additions & 2 deletions gcs_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ func (util *snowflakeGcsClient) uploadFile(
uploadSrc = meta.realSrcStream
}
} else {
uploadSrc, err = os.OpenFile(dataFile, os.O_RDONLY, 0666)
uploadSrc, err = os.OpenFile(dataFile, os.O_RDONLY, readWriteFileMode)
if err != nil {
return err
}
Expand Down Expand Up @@ -314,7 +314,7 @@ func (util *snowflakeGcsClient) nativeDownloadFile(
return meta.lastError
}

f, err := os.OpenFile(fullDstFileName, os.O_CREATE|os.O_WRONLY, 0666)
f, err := os.OpenFile(fullDstFileName, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions local_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (util *localUtil) uploadOneFileWithRetry(meta *fileMetadata) error {
return nil
}
}
output, err := os.OpenFile(filepath.Join(user, meta.dstFileName), os.O_CREATE|os.O_WRONLY, 0666)
output, err := os.OpenFile(filepath.Join(user, meta.dstFileName), os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
return err
}
Expand Down Expand Up @@ -100,7 +100,7 @@ func (util *localUtil) downloadOneFile(meta *fileMetadata) error {
if err != nil {
return err
}
if err = os.WriteFile(fullDstFileName, data, 0666); err != nil {
if err = os.WriteFile(fullDstFileName, data, readWriteFileMode); err != nil {
return err
}
fi, err := os.Stat(fullDstFileName)
Expand Down
6 changes: 3 additions & 3 deletions local_storage_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func TestLocalUpload(t *testing.T) {
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err := os.WriteFile(fname, b.Bytes(), 0666); err != nil {
if err := os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}
putDir, err := os.MkdirTemp("", "put")
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestLocalUpload(t *testing.T) {
if uploadMeta.resStatus != skipped {
t.Fatal("overwrite is false. should have skipped")
}
fileStream, _ := os.OpenFile(fname, os.O_RDONLY, 0666)
fileStream, _ := os.OpenFile(fname, os.O_RDONLY, readWriteFileMode)
ctx := WithFileStream(context.Background(), fileStream)
uploadMeta.srcStream = getFileStream(ctx)

Expand Down Expand Up @@ -116,7 +116,7 @@ func TestDownloadLocalFile(t *testing.T) {
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err := os.WriteFile(fname, b.Bytes(), 0666); err != nil {
if err := os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}
putDir, err := os.MkdirTemp("", "put")
Expand Down
2 changes: 1 addition & 1 deletion ocsp.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func initOCSPCache() {
ocspResponseCacheLock = &sync.RWMutex{}

logger.Infof("reading OCSP Response cache file. %v\n", cacheFileName)
f, err := os.OpenFile(cacheFileName, os.O_CREATE|os.O_RDONLY, 0666)
f, err := os.OpenFile(cacheFileName, os.O_CREATE|os.O_RDONLY, readWriteFileMode)
if err != nil {
logger.Debugf("failed to open. Ignored. %v\n", err)
return
Expand Down
2 changes: 1 addition & 1 deletion ocsp_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,7 @@ func TestInitOCSPCacheFileCreation(t *testing.T) {
defer func() {
src, _ = os.Open(tmpFileName)
defer src.Close()
dst, _ = os.OpenFile(srcFileName, os.O_WRONLY, 0666)
dst, _ = os.OpenFile(srcFileName, os.O_WRONLY, readWriteFileMode)
defer dst.Close()
// copy temporary file contents back to original file
if _, err = io.Copy(dst, src); err != nil {
Expand Down
14 changes: 7 additions & 7 deletions put_get_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestPutError(t *testing.T) {
defer os.RemoveAll(tmpDir)
file1 := filepath.Join(tmpDir, "file1")
remoteLocation := filepath.Join(tmpDir, "remote_loc")
f, err := os.OpenFile(file1, os.O_CREATE|os.O_WRONLY, 0666)
f, err := os.OpenFile(file1, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -250,7 +250,7 @@ func TestPutWithAutoCompressFalse(t *testing.T) {
}
defer os.RemoveAll(tmpDir)
testData := filepath.Join(tmpDir, "data.txt")
f, err := os.OpenFile(testData, os.O_CREATE|os.O_WRONLY, 0666)
f, err := os.OpenFile(testData, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -291,7 +291,7 @@ func TestPutOverwrite(t *testing.T) {
}
defer os.RemoveAll(tmpDir)
testData := filepath.Join(tmpDir, "data.txt")
f, err := os.OpenFile(testData, os.O_CREATE|os.O_RDWR, 0666)
f, err := os.OpenFile(testData, os.O_CREATE|os.O_RDWR, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -392,14 +392,14 @@ func testPutGet(t *testing.T, isStream bool) {
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err = os.WriteFile(fname, b.Bytes(), 0666); err != nil {
if err = os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}

runTests(t, dsn, func(dbt *DBTest) {
dbt.mustExec("create or replace table " + tableName +
" (a int, b string)")
fileStream, err := os.OpenFile(fname, os.O_RDONLY, 0666)
fileStream, err := os.OpenFile(fname, os.O_RDONLY, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand Down Expand Up @@ -517,15 +517,15 @@ func TestPutGetGcsDownscopedCredential(t *testing.T) {
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err = os.WriteFile(fname, b.Bytes(), 0666); err != nil {
if err = os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}

dsn = dsn + "&GCS_USE_DOWNSCOPED_CREDENTIAL=true"
runTests(t, dsn, func(dbt *DBTest) {
dbt.mustExec("create or replace table " + tableName +
" (a int, b string)")
fileStream, err := os.OpenFile(fname, os.O_RDONLY, 0666)
fileStream, err := os.OpenFile(fname, os.O_RDONLY, readWriteFileMode)
if err != nil {
t.Error(err)
}
Expand Down
2 changes: 1 addition & 1 deletion put_get_user_stage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ func putGetUserStage(t *testing.T, tmpDir string, numberOfFiles int, numberOfLin
dbt.mustExec("rm @" + stageName)
var fs *os.File
if isStream {
fs, _ = os.OpenFile(files, os.O_RDONLY, 0666)
fs, _ = os.OpenFile(files, os.O_RDONLY, readWriteFileMode)
dbt.mustExecContext(WithFileStream(context.Background(), fs),
fmt.Sprintf("put 'file://%v' @%v", strings.ReplaceAll(
files, "\\", "\\\\"), stageName))
Expand Down
6 changes: 3 additions & 3 deletions put_get_with_aws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ func TestPutWithInvalidToken(t *testing.T) {
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err := os.WriteFile(fname, b.Bytes(), 0666); err != nil {
if err := os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}

Expand Down Expand Up @@ -214,7 +214,7 @@ func TestPretendToPutButList(t *testing.T) {
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err := os.WriteFile(fname, b.Bytes(), 0666); err != nil {
if err := os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}

Expand Down Expand Up @@ -283,7 +283,7 @@ func TestPutGetAWSStage(t *testing.T) {
gzw := gzip.NewWriter(&b)
gzw.Write([]byte(originalContents))
gzw.Close()
if err = os.WriteFile(fname, b.Bytes(), 0666); err != nil {
if err = os.WriteFile(fname, b.Bytes(), readWriteFileMode); err != nil {
t.Fatal("could not write to gzip file")
}

Expand Down
2 changes: 1 addition & 1 deletion s3_storage_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ func (util *snowflakeS3Client) nativeDownloadFile(
}
}

f, err := os.OpenFile(fullDstFileName, os.O_CREATE|os.O_WRONLY, 0666)
f, err := os.OpenFile(fullDstFileName, os.O_CREATE|os.O_WRONLY, readWriteFileMode)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion secure_storage_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func TestCreateCredentialCache(t *testing.T) {
// cleanup
src, _ = os.Open(tmpFileName)
defer src.Close()
dst, _ = os.OpenFile(srcFileName, os.O_WRONLY, 0666)
dst, _ = os.OpenFile(srcFileName, os.O_WRONLY, readWriteFileMode)
defer dst.Close()
// copy temporary file contents back to original file
if _, err = io.Copy(dst, src); err != nil {
Expand Down

0 comments on commit d9cae96

Please sign in to comment.