[SOLVED] Linux: how to detect whether a certain partition is mounted? lazarus / 32-Linux ---------------------------------------------------------------------- Hartmut · 2018-12-06 13:22 In a console program I want to detect, whether a certain Windows- Partition is currently mounted, but without to change the mount- status. As a workaround I detect via FindFirst() whether the mounted folder is empty or not (if it's not empty, the partition must be mounted). But this would fail, if the mounted partition is totally empty. How can this be done? I'm using FPC 3.0.4 on Ubuntu 18.04. Thanks in advance. ---------------------------------------------------------------------- vincococka · 2018-12-06 13:55 One possible solution is to check /proc/mounts if it contains exact path that you expected to be mounted. ---------------------------------------------------------------------- lucamar · 2018-12-06 14:30 Or you could use fpStatFS() and check the result for congruence. ---------------------------------------------------------------------- Hartmut · 2018-12-06 16:45 Quote from: vincococka on December 06, 2018, 02:55:28 pmOne possible solution is to check /proc/mounts if it contains exact path that you expected to be mounted. I looked into file /proc/mounts and it seems to work. But I'm afraid to "disturb" the Operating System when I open that file while reading: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function isMounted: boolean;   var f1: text;       s: ansistring;   begin   assign(f1,'/proc/mounts'); reset(f1);   while not eof(f1) do      begin      readln(f1, s);         if pos(' /media/C ', s) > 0 then begin close(f1); exit(true); end;      end;   close(f1); exit(false);   end;   What do the experts say? Is this "dangerous" or not? Quote from: lucamar on December 06, 2018, 03:30:52 pmOr you could use fpStatFS() and check the result for congruence. I tried function fpStatFS from Unit Unix: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure Test_fpStatFS;   var info: TStatFs;       i: longint;   begin   fillchar(info,sizeof(info),0);   i:=fpStatFS('/media/C/', @info);    writeln('i=', i);   end;   Result 'i' was always 0. if mounted => INFO={FSTYPE = 1702057286, BSIZE = 4096, BLOCKS = 1048233, BFREE = 106248, BAVAIL = 106248, FILES = 457760, FFREE = 438097, FSID = {0, 0}, NAMELEN = 255, FRSIZE = 4096, SPARE = {4142, 0, 0, 0, 0}} if not mounted => INFO={FSTYPE = 61267, BSIZE = 4096, BLOCKS = 6417977, BFREE = 1245557, BAVAIL = 913781, FILES = 1638400, FFREE = 1111304, FSID = {-1900541924, -1966772697}, NAMELEN = 255, FRSIZE = 4096, SPARE = {4128, 0, 0, 0, 0}} How do you think that I can distinguish mounted state and not mounted state? ---------------------------------------------------------------------- lucamar · 2018-12-06 19:12 Quote from: Hartmut on December 06, 2018, 05:45:43 pmIs this "dangerous" or not? No, it's not dangerous unless you try to lock it for too much time. Think that it's there precisely to be able to do what you are doing. Although you may as well "slurp" the whole file in one pass and then search into the buffer, if just to minimize file operations. QuoteI tried function fpStatFS from Unit Unix: [. . .] How do you think that I can distinguish mounted state and not mounted state? Well, to start with: the filesystem type is different, the number of blocks is different, etc. I don't know off the top of ny head how to parse FSType but a man invocation or a search in the kernel docs should tell you that. Let me have a look, I'll be back in a jiffy... There and back again ... OK, here is (part of) what man statfs has to say about fstype: Code: [Select]File system types:               [. . .]               EXT_SUPER_MAGIC       0x137D               EXT2_OLD_SUPER_MAGIC  0xEF51               EXT2_SUPER_MAGIC      0xEF53               EXT3_SUPER_MAGIC      0xEF53               EXT4_SUPER_MAGIC      0xEF53               HFS_SUPER_MAGIC       0x4244               HPFS_SUPER_MAGIC      0xF995E849               [. . .]               MSDOS_SUPER_MAGIC     0x4d44               NCP_SUPER_MAGIC       0x564c               NFS_SUPER_MAGIC       0x6969               NTFS_SB_MAGIC         0x5346544e               [. . .] Just with that you can know whether the path is still in an EXTx partition or an NTFS (or many others!) one. ---------------------------------------------------------------------- Hartmut · 2018-12-06 20:21 Thanks again for your help! No I understood: when the partition is not mounted, I get FSTYPE = 61267 = 0xef53 which means: EXT2_SUPER_MAGIC      0xef53 EXT3_SUPER_MAGIC      0xef53 EXT4_SUPER_MAGIC      0xef53           This is because folder '/media/C/' is not mounted and that folder is part of the etx4 filesystem of Ubuntu. If folder '/media/C/' is mounted, I get FSTYPE = 1702057286 = 0x65735546 which means: FUSE_SUPER_MAGIC      0x65735546 Quote from: lucamar on December 06, 2018, 08:12:39 pmAlthough you may as well "slurp" the whole file in one pass and then search into the buffer, if just to minimize file operations. Do you mean: read that file in binary mode into an ansistring and then search in that ansistring with pos() like: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function readFile_string(filespec: ansistring; var s: ansistring): boolean;   {reads file 'filespec' into var 's'}   var f1: file;       size: int64;   begin   assign(f1,filespec); reset(f1,1);   size:=filesize(f1);    try    SetLength(s,size);   except close(f1); {if not enough memory}          SetLength(s,0);          exit(false);   end;    if size > 0 then blockread(f1, s[1],size); {0 => "Range check error"}   close(f1);   exit(true);   end; ---------------------------------------------------------------------- lucamar · 2018-12-06 21:37 Quote from: Hartmut on December 06, 2018, 09:21:37 pmQuote from: lucamar on December 06, 2018, 08:12:39 pmAlthough you may as well "slurp" the whole file in one pass and then search into the buffer, if just to minimize file operations. Do you mean: read that file in binary mode into an ansistring and then search in that ansistring with pos() like: [... some code ...] Well, yes, that's one way. Or use a (buffered) stream, or even a TStringList. But you got the point: since it's usually rather small (mine's never more than ~2 Kb, negligible) it's better to load it whole in memory and close the file as quickly as possible. Incidentally, I've taken the liberty of modifying your code a tiny bit: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function readFile_string(filespec: ansistring; var s: ansistring): boolean;   {reads file 'filespec' into var 's'}var  f1: file;  size: int64;begin  assign(f1,filespec);  reset(f1,1);  try    size:=filesize(f1);    if size > 0 then begin      try        SetLength(s, size);      except        close(f1); {if not enough memory}        {               .^.         You should make *sure* whether the problem is really NEM        }        s := ''        Exit(false);      end;      blockread(f1, s[1], size); {0 => "Range check error"}  end else    s := '';  finally      close(f1);  end;  Result := true;end; ---------------------------------------------------------------------- Hartmut · 2018-12-07 18:51 The result of this Topic are 2 good solutions:  - checking file /proc/mounts for the expected path to be mounted or  - calling function fpStatFS() from unit Unix and checking the magic codes in field INFO.FSTYPE Thanks a lot to all who helped me.