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

updater.go - misuse of os.WriteFile ? #667

Open
udf2457 opened this issue Jan 20, 2025 · 2 comments
Open

updater.go - misuse of os.WriteFile ? #667

udf2457 opened this issue Jan 20, 2025 · 2 comments
Assignees

Comments

@udf2457
Copy link
Contributor

udf2457 commented Jan 20, 2025

Given you have:

file, err := os.CreateTemp(update.cfg.LocalMetadataDir, "tuf_tmp")

Its not clear why you are doing this:

err = os.WriteFile(file.Name(), data, 0644)

_, err = file.Write(data) would surely be a far more sensible construct ?

I'm not going to open a PR on it, I'll leave that to your own coding preferences ...

@rdimitrov
Copy link
Contributor

hey, @udf2457, thanks for reaching out.

I agree both can be used with the difference that one allows you to specify the file permissions explicitly so it might be the reason why we chose it.

In any case as you mentioned it's a matter of preference and it doesn't affect performance nor behaviour in any way. The amount of imported packages would be the same too. That said, I think we can close the issue for now unless we see more reasons to change it. Wdyt?

@rdimitrov rdimitrov self-assigned this Jan 20, 2025
@udf2457
Copy link
Contributor Author

udf2457 commented Jan 20, 2025

Huh ? 🤷 Let me spell it out ?

You are already opening file in line 595 with os.CreateTemp.

You have defer file.Close() on the "next" line.

os.WriteFile is the "next" line after that.

Ergo you already have an open file handler.

So ....

If you don't like the default permissions, don't use CreateTemp ? e.g. Maybe use os.MkdirTemp to create a directory in which you can put a file with your preferred permissions ?

Or you could use (f *[File]) Chmod(mode [FileMode]) (https://pkg.go.dev/os#File.Chmod) to change the permissions via the already open file handler ?

So ... just use the already open file handler, no ? Or if you want to keep using WriteFile don't defer the close on the prior line ?

But as presently structured it makes no logical, practical or technical sense ?

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

No branches or pull requests

2 participants