Skip to content

Instantly share code, notes, and snippets.

@NielsLiisberg
Last active October 24, 2023 10:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NielsLiisberg/dd45d94da1cd21a2f4eaa178023ab4f0 to your computer and use it in GitHub Desktop.
Save NielsLiisberg/dd45d94da1cd21a2f4eaa178023ab4f0 to your computer and use it in GitHub Desktop.
SQL - Check access to an IFS file or folder
-- 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
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment