Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a directory is readable

Tags:

delphi

How we can check if a directory is readOnly or Not?

like image 619
user476601 Avatar asked Oct 15 '10 08:10

user476601


People also ask

How can I tell if a directory is writable?

You need to check the directory itself with the filename removed, if this is the case. If you do that, is_writable will correctly tell you whether the directory is writable or not. If $file contains your file path do this: $file_directory = dirname($file);

How can I tell if a file is readable?

To check if the a file is readable, in other words if the file has read permissions, using bash scripting, use [ -r FILE ] expression with bash if statement.

How do I check permissions on a directory?

To view the permissions for all files in a directory, use the ls command with the -la options. Add other options as desired; for help, see List the files in a directory in Unix. In the output example above, the first character in each line indicates whether the listed object is a file or a directory.


3 Answers

you can use the FileGetAttr function and check if the faReadOnly flag is set.

try this code

function DirIsReadOnly(Path:string):Boolean;
var
 attrs    : Integer;
begin
 attrs  := FileGetAttr(Path);
 Result := (attrs  and faReadOnly) > 0;
end;
like image 154
RRUZ Avatar answered Nov 11 '22 17:11

RRUZ


Testing if the directory's attribute is R/O is only part of the answer. You can easily have a R/W directory that you still can't write to - because of Access Rights.

The best way to check if you can write to a directory or not is - to try it:

FUNCTION WritableDir(CONST Dir : STRING) : BOOLEAN;
  VAR
    FIL : FILE;
    N   : STRING;
    I   : Cardinal;

  BEGIN
    REPEAT
      N:=IncludeTrailingPathDelimiter(Dir);
      FOR I:=1 TO 250-LENGTH(N) DO N:=N+CHAR(RANDOM(26)+65)
    UNTIL NOT FileExists(N);
    Result:=TRUE;
    TRY
      AssignFile(FIL,N);
      REWRITE(FIL,1);
      Result:=FileExists(N); // Not sure if this is needed, but AlainD says so :-)
    EXCEPT
      Result:=FALSE
    END;
    IF Result THEN BEGIN
      CloseFile(FIL); 
      ERASE(FIL)
    END
  END;
like image 39
HeartWare Avatar answered Nov 11 '22 17:11

HeartWare


The version HeartWare has given is nice but contains two bugs. This modified versions works more reliably and has comments to explain what is going on:

function IsPathWriteable(const cszPath: String) : Boolean;
var
    fileTest: file;
    szFile: String;
    nChar: Cardinal;
begin
    // Generate a random filename that does NOT exist in the directory
    Result := True;
    repeat
        szFile := IncludeTrailingPathDelimiter(cszPath);
        for nChar:=1 to (250 - Length(szFile)) do
            szFile := (szFile + char(Random(26) + 65));
    until (not FileExists(szFile));

    // Attempt to write the file to the directory. This will fail on something like a CD drive or
    // if the user does not have permission, but otherwise should work.
    try
        AssignFile(fileTest, szFile);
        Rewrite(fileTest, 1);

        // Note: Actually check for the existence of the file. Windows may appear to have created
        // the file, but this fails (without an exception) if advanced security attibutes for the
        // folder have denied "Create Files / Write Data" access to the logged in user.
        if (not FileExists(szFile)) then
            Result := False;
    except
        Result := False;
    end;

    // If the file was written to the path, delete it
    if (Result) then
        begin
        CloseFile(fileTest);
        Erase(fileTest);
        end;
end;
like image 35
AlainD Avatar answered Nov 11 '22 17:11

AlainD