Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: get_file_size - gets the size of a file in bytes #1029

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

Open
wassup05 wants to merge 8 commits into fortran-lang:master
base: master
Choose a base branch
Loading
from wassup05:file_size

Conversation

Copy link
Contributor

@wassup05 wassup05 commented Aug 23, 2025

User facing function added is get_file_size(path [, err])

It returns the file size in bytes. It returns an error if the path is a directory or a symlink to a directory (because for the size of directories you will need to recurse into the directory and add all its component sizes to get its actual size)

It uses stat and _stati64 on POSIX and windows respectively. It returns an integer of kind int64 to handle large enough file sizes.

perazz reacted with thumbs up emoji
@@ -720,6 +720,46 @@ Subroutine

---

## `get_file_size` - Gets the size (in bytes) of a file.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have not used get_* patterns anywhere else so, this function may perhaps just be file_size? The fact that it is a function already means we're get_ting stuff.

Comment on lines +1142 to +1158
type(state_type) :: err0
integer :: code
integer(c_int64_t) :: c_size

if (is_directory(path)) then
err0 = FS_ERROR("Is a directory!")
call err0%handle(err)
return
end if

code = stdlib_get_file_size(to_c_char(trim(path)), c_size)

if (code /= 0) then
err0 = FS_ERROR_CODE(code, c_get_strerror())
call err0%handle(err)
return
end if
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This functionality can be achieved by Fortran intrinsics only. Perhaps that would be a good way to avoid cross-platform CI issues and avoid the C backend calls?

Suggested change
type(state_type) :: err0
integer :: code
integer(c_int64_t) :: c_size
if (is_directory(path)) then
err0 = FS_ERROR("Is a directory!")
call err0%handle(err)
return
end if
code = stdlib_get_file_size(to_c_char(trim(path)), c_size)
if (code /= 0) then
err0 = FS_ERROR_CODE(code, c_get_strerror())
call err0%handle(err)
return
end if
type(state_type) :: err0
integer :: code
character(len=256) :: iomsg
integer(c_int64_t) :: c_size
if (is_directory(path)) then
err0 = FS_ERROR("Is a directory!")
call err0%handle(err)
return
end if
inquire(file=path, size=c_size, iostat=code, iomsg=iomsg)
if (code /= 0) then
err0 = FS_ERROR_CODE(code, iomsg)
call err0%handle(err)
return
end if

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@perazz perazz perazz left review comments

At least 1 approving review is required to merge this pull request.

Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

2 participants

AltStyle によって変換されたページ (->オリジナル) /