IFS_ACCESS - SQL Scalar function to check access to an IFS file or folder
It returns:
0=If the file or folder Exists and you have access
-1=No access or file does not exists
use case:
-- returns 0 for the home folder - it exists
values IFS_ACCESS (
ifs_path => '/home'
);
-- returns -1 for files that does not exists
values IFS_ACCESS (
ifs_path => '/home/does_not_exists'
);
-- returns 0 for the "cat" file exists
values IFS_ACCESS (
ifs_path => '/bin/cat'
);
-- Can i execute /bin/cat
values IFS_ACCESS (
ifs_path => '/bin/cat',
access_type => ifs_access_execute
);
-- Can i write to home folder
values IFS_ACCESS (
ifs_path => '/home',
access_type => ifs_access_write
);
It returns:
0=If the file or folder Exists and you have access
-1=No access or file does not exists
This is a wrapper for the access() unix API:
https://pubs.opengroup.org/onlinepubs/009695299/functions/access.html
Note second parameter is the access mode, that defaults to F_OK
Constants for access() are also declare as SQL global variables
The implementation can be found here:
https://gist.github.com/NielsLiisberg/dd45d94da1cd21a2f4eaa178023ab4f0
or paste from here:
_____________
ifs_access.sql
-- SQL Scalar function to check access to an IFS file or folder
-- It returns:
-- 0=If the file or folder Exists and you have access
-- -1=No access or file does not exists
--
-- This is a wrapper for the access() unix API:
-- https://pubs.opengroup.org/onlinepubs/009695299/functions/access.html
-- Note second parameter is the acces mode, that defaults to F_OK
-- Constants for access() are also declare as SQL global variables
-- Simply paste this gist into ACS SQL and run it to create the feature.
-- Note: I am using library QUSRSYS. I suggest you put it into your own tool library
-- It is a cool example how far you can go with SQL: Have fun -
-- (C) Niels Liisberg 2023
-- This gist is distributed on an "as is" basis, without warranties
-- or conditions of any kind, either express or implied.
----------------------------------------------------------------------------------------------
call qcmdexc ('addlible qsysinc');
--call qcmdexc ('dltf FILE(QTEMP/C) ');
call qcmdexc ('crtsrcpf FILE(QTEMP/C) MBR(C) rcdlen(256)');
truncate qtemp.c;
insert into qtemp.c (srcdta) values
('#include <unistd.h>'),
('IFS_ACCESS.IFS_PATH.DAT[IFS_ACCESS.IFS_PATH.LEN] = 0;'),
('MAIN.RC = access ( IFS_ACCESS.IFS_PATH.DAT , IFS_ACCESS.ACCESS_TYPE);')
;
create or replace function qusrsys.ifs_access (
ifs_path varchar(256) ,
access_type int default 0 -- Posix: 0 = F_OK = Test for existence of a file
)
returns int
specific IFS_ACCESS
no external action
set option output=*print, commit=*none, dbgview = *source
main:
begin
declare rc int default 0;
include qtemp/c(c);
return rc;
end;
-- Constants for access()
create or replace variable qusrsys.ifs_access_read int default 4; -- R_OK 4
create or replace variable qusrsys.ifs_access_write int default 2; -- W_OK 2
create or replace variable qusrsys.ifs_access_execute int default 1; -- X_OK 1
stop;
-- use case:
------------
-- returns 0 for the home folder - it exists
values qusrsys.IFS_ACCESS (
ifs_path => '/home'
);
-- returns -1 for files that does not exists
values qusrsys.IFS_ACCESS (
ifs_path => '/home/does_not_exists'
);
-- returns 0 for the "cat" file exists
values qusrsys.IFS_ACCESS (
ifs_path => '/bin/cat'
);
-- Can i execute /bin/cat
values qusrsys.IFS_ACCESS (
ifs_path => '/bin/cat',
access_type => qusrsys.ifs_access_execute
);
-- Can i write to home folder
values qusrsys.IFS_ACCESS (
ifs_path => '/home',
access_type => qusrsys.ifs_access_write
);
A new scalar function, SYSTOOLS.IFS_ACCESS, has been provided.
See this IBM Documentation page for the details: https://www.ibm.com/docs/en/i/7.5?topic=services-ifs-access-scalar-function
Db2 for i development team
IBM Power Systems Development
The CAAC has reviewed this IBM Idea and recommends that IBM not implement this request.
This function already exists as en easy call to access API.
Background: The COMMON Americas Advisory Council (CAAC) members have a broad range of experience in working with small and medium-sized IBM i customers. CAAC has a key role in working with IBM i development to help assess the value and impact of individual IBM Ideas on the broader IBM i community and has therefore reviewed your Idea.
For more information about CAAC, see www.common.org/caac
Carmelita Ruvalcaba - CAAC Program Manager
Db2 for i development team
IBM Power Systems Development