Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GcsFs: incorrect handling of OpenFile of flags #423

Open
meandnano opened this issue Apr 12, 2024 · 0 comments
Open

GcsFs: incorrect handling of OpenFile of flags #423

meandnano opened this issue Apr 12, 2024 · 0 comments

Comments

@meandnano
Copy link

First, thanks for the great product!

Second, a bug report:
In the occasion of "overwriting file" with GCS FS using afero.WriteFile (or fs.OpenFile), the way in which flags are processed seems to be incorrect.

afero.WriteFile calls fs.OpenFile with flags being os.O_WRONLY|os.O_CREATE|os.O_TRUNC which works like a charm for creating new files or truncating existing ones with local FS. However, with GCS FS, when the file does not exist, such call fails with storage: object doesn't exist.

Example code:

package main

import (
	"context"
	"fmt"
	"github.com/spf13/afero"
	"github.com/spf13/afero/gcsfs"
	"os"
)

func main() {
	filename := "mybucket/myfile.txt" // bucket exists, but not the file
	fs, err := gcsfs.NewGcsFS(context.Background())
	if err != nil {
		panic(err)
	}

	if err := afero.WriteFile(fs, filename, []byte("Hello"), os.ModePerm); err != nil {
		fmt.Printf("%s\n", err) // will print "storage: object doesn't exist"
	}
}

Looking at the gcsfs/fs.go it's clear that os.O_TRUNC tries to delete non-existent file without checking for os.O_CREATE flag to be present.

I'm capable of providing a PR with the solution to the issue, but just wanted to make sure this is not something already being addressed or was considered an expected behaviour.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant