Skip to content

Conversation

mat-hek
Copy link
Contributor

@mat-hek mat-hek commented Aug 20, 2025

These changes are made under both the "Apache 2.0" and the "GNU Lesser General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later

@mat-hek mat-hek force-pushed the mf/prim-file-get-cwd branch 2 times, most recently from 19e7b57 to e2ae3ca Compare August 20, 2025 12:56
@pguyot
Copy link
Collaborator

pguyot commented Aug 20, 2025

AFAIK, prim_file is not part of OTP documented library.
Shouldn't we implement file:get_cwd() instead?

@mat-hek
Copy link
Contributor Author

mat-hek commented Aug 21, 2025

@pguyot indeed it's not, but we need it because it's called by the OTP internals, and not only by the file server

@pguyot
Copy link
Collaborator

pguyot commented Aug 21, 2025

@pguyot indeed it's not, but we need it because it's called by the OTP internals, and not only by the file server

Which module calls it? I searched OTP source code (for prim_file:get_cwd and ?PRIM_FILE:get_cwd) and didn't find any relevant call.

@mat-hek
Copy link
Contributor Author

mat-hek commented Aug 22, 2025

@pguyot well, now I can't find it either... changed to file:get_cwd, I'll reiterate if prim_file pops out again

@mat-hek mat-hek changed the title Add prim_file:get_cwd/0 Add file:get_cwd/0 Aug 22, 2025
}
term result_tuple = term_alloc_tuple(2, &ctx->heap);
term reason = UNDEFINED_ATOM;
switch (errno) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

POSIX error code are already standard in OTP, so we don't have to invent new codes:
https://www.erlang.org/doc/apps/kernel/file.html#t:posix/0
Also we have a functions that handles them, that is posix_errno_to_term.

UNUSED(argv)

char cwd[PATH_MAX];
if (IS_NULL_PTR(getcwd(cwd, PATH_MAX))) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

PATH_MAX is not defined on STM32 and RPi Pico, also more broadly getcwd() is POSIX but not standard C, so I don't expect it to be available on all platforms.
We might use some kind of HAVE_CWD define to check if a platform has it or not, as we are already doing with open() and other POSIX functions.
Question: should we move this to posix_nifs.c? CC @pguyot

Copy link
Collaborator

Choose a reason for hiding this comment

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

Indeed, if we test for HAVE_GETCWD because it's posix and not C, I'd say it makes sense to move it to posix_nifs.c.

UNUSED(argc)
UNUSED(argv)

char cwd[PATH_MAX];
Copy link
Collaborator

Choose a reason for hiding this comment

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

PATH_MAX might be quite big, I'm not sure this is safe for stack allocation.

#include <stdio.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
Copy link
Collaborator

Choose a reason for hiding this comment

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

As I mention later, I'm worried we are leaking unistd and POSIX stuff into platform agnostic nifs.c.

true = is_valid_path(Path),
ok.

is_valid_path(<<"/", _/binary>>) ->
Copy link
Collaborator

Choose a reason for hiding this comment

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

We'll regret this the day we are going to test this on a platform that is not unix ;)
Let's check platform.

@mat-hek mat-hek force-pushed the mf/prim-file-get-cwd branch from 7692b6d to 980951e Compare September 16, 2025 10:14
@mat-hek mat-hek requested a review from bettio September 17, 2025 08:14
% SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
%

-module(test_file_get_cwd).
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we should have just a ŧest_file module as we are doing with other modules in our tests.
In order to get this done in a pain-free manner I suggest merging #1807 first, then we can rebase this, add relevant tests to test_file and squash everything.

@mat-hek mat-hek force-pushed the mf/prim-file-get-cwd branch 2 times, most recently from 53f1a4b to ea6d34e Compare September 23, 2025 10:00
@mat-hek mat-hek requested a review from bettio September 23, 2025 10:27
Copy link
Collaborator

@bettio bettio left a comment

Choose a reason for hiding this comment

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

Everything good, let's squash everything into a single commit.

@mat-hek mat-hek force-pushed the mf/prim-file-get-cwd branch 2 times, most recently from 898b61c to d580122 Compare September 24, 2025 09:12
@mat-hek
Copy link
Contributor Author

mat-hek commented Sep 24, 2025

@bettio done. BTW, can't you do squash and merge on github? Would save us one step ;)

@mat-hek mat-hek requested a review from bettio September 24, 2025 09:22

static const struct Nif make_ref_nif = {
static const struct Nif make_ref_nif =
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I didn't notice this. Let's rewrite this as:

static const struct Nif make_ref_nif = {

So clang-format check will pass.

define_if_function_exists(libAtomVM unlink "unistd.h" PRIVATE HAVE_UNLINK)
define_if_function_exists(libAtomVM execve "unistd.h" PRIVATE HAVE_EXECVE)
define_if_function_exists(libAtomVM closefrom "unistd.h" PRIVATE HAVE_CLOSEFROM)
define_if_function_exists(libAtomVM getcwd "unistd.h" PUBLIC HAVE_GETCWD)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I suggest using define_if_symbol_exists for PATH_MAX: on Pico it is failing with the following error:

/home/runner/work/AtomVM/AtomVM/src/libAtomVM/posix_nifs.c: In function 'nif_file_get_cwd_0':
/home/runner/work/AtomVM/AtomVM/src/libAtomVM/posix_nifs.c:953:24: error: 'PATH_MAX' undeclared (first use in this function)
  953 |     char *cwd = malloc(PATH_MAX);
      |                        ^~~~~~~~

So we need to define HAVE_PATH_MAX and we'll have the following:

#if defined(HAVE_GETCWD) && defined(HAVE_PATH_MAX)
static term nif_file_get_cwd_0(Context *ctx, int argc, term argv[])

@mat-hek mat-hek force-pushed the mf/prim-file-get-cwd branch 6 times, most recently from 9311e6e to 1075f7b Compare September 26, 2025 11:44
These changes are made under both the "Apache 2.0" and the "GNU Lesser
General
Public License 2.1 or later" license terms (dual license).

SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later
Added nif implementation for getcwd.

Signed-off-by: Mateusz Front <[email protected]>
@mat-hek mat-hek force-pushed the mf/prim-file-get-cwd branch from 1075f7b to 8c2d206 Compare September 26, 2025 13:03
@mat-hek mat-hek requested a review from bettio September 26, 2025 13:03
@bettio bettio merged commit f4973f2 into atomvm:main Sep 26, 2025
128 of 130 checks passed
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

Successfully merging this pull request may close these issues.

4 participants