ZealOS/docs/Kernel/BlkDev/FileSysRedSea.CC.html
TomAwezome 1b75d91002 Fix Mount AHCI Port selection.
Add arg to SATARep to specify drive types to show.
Add checks in AHCIPortInit to verify port signatures, add helper method to get signatures from port.
2021-08-02 16:40:05 -04:00

750 lines
81 KiB
HTML
Executable file

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=US-ASCII">
<meta name="generator" content="ZealOS V0.13">
<style type="text/css">
body {background-color:#fef1f0;}
.cF0{color:#000000;background-color:#fef1f0;}
.cF1{color:#0148a4;background-color:#fef1f0;}
.cF2{color:#3b7901;background-color:#fef1f0;}
.cF3{color:#057c7e;background-color:#fef1f0;}
.cF4{color:#bb2020;background-color:#fef1f0;}
.cF5{color:#9e42ae;background-color:#fef1f0;}
.cF6{color:#b57901;background-color:#fef1f0;}
.cF7{color:#b2b6af;background-color:#fef1f0;}
.cF8{color:#555753;background-color:#fef1f0;}
.cF9{color:#678fbb;background-color:#fef1f0;}
.cFA{color:#82bc49;background-color:#fef1f0;}
.cFB{color:#0097a2;background-color:#fef1f0;}
.cFC{color:#e26a6a;background-color:#fef1f0;}
.cFD{color:#c671bc;background-color:#fef1f0;}
.cFE{color:#c7ab00;background-color:#fef1f0;}
.cFF{color:#fef1f0;background-color:#fef1f0;}
</style>
</head>
<body>
<pre style="font-family:monospace;font-size:12pt">
<a name="l1"></a><span class=cF2>// See </span><a href="https://zeal-operating-system.github.io/ZealOS/Doc/RedSea.DD.html#l1"><span class=cF4>RedSea File System</span></a><span class=cF0>
<a name="l2"></a>
<a name="l3"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>RedSeaFreeFreeList</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive)
<a name="l4"></a>{
<a name="l5"></a> </span><span class=cF9>CFreeList</span><span class=cF0> *tmpf, *tmpf1;
<a name="l6"></a> </span><span class=cF1>Bool</span><span class=cF0> unlock;
<a name="l7"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l8"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l9"></a> unlock = </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l10"></a> </span><span class=cF1>if</span><span class=cF0> (tmpf = drive-&gt;next_free)
<a name="l11"></a> {
<a name="l12"></a> </span><span class=cF1>while</span><span class=cF0> (tmpf != &amp;drive-&gt;next_free)
<a name="l13"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l14"></a> tmpf1 = tmpf-&gt;next;
<a name="l15"></a> </span><span class=cF5>Free</span><span class=cF0>(tmpf);
<a name="l16"></a> tmpf = tmpf1;
<a name="l17"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l18"></a> }
<a name="l19"></a> drive-&gt;next_free = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l20"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l21"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l22"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l23"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l24"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l25"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l26"></a>}
<a name="l27"></a>
<a name="l28"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>RedSeaFreeListBuild</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive)
<a name="l29"></a>{
<a name="l30"></a> </span><span class=cF1>Bool</span><span class=cF0> unlock;
<a name="l31"></a> </span><span class=cF9>CFreeList</span><span class=cF0> *tmpf;
<a name="l32"></a> </span><span class=cF9>I64</span><span class=cF0> i, first = drive-&gt;data_area, max_blk = drive-&gt;size + drive-&gt;drv_offset;
<a name="l33"></a>
<a name="l34"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l35"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l36"></a> unlock = </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l37"></a> </span><span class=cF1>if</span><span class=cF0> (drive-&gt;next_free)
<a name="l38"></a> </span><span class=cFD>RedSeaFreeFreeList</span><span class=cF0>(drive);
<a name="l39"></a> </span><span class=cF5>QueueInit</span><span class=cF0>(&amp;drive-&gt;next_free);
<a name="l40"></a> </span><span class=cF1>while</span><span class=cF0> (first &lt; max_blk)
<a name="l41"></a> {
<a name="l42"></a> i = </span><span class=cFE>0</span><span class=cF0>; </span><span class=cF2>//count free clus</span><span class=cF0>
<a name="l43"></a> </span><span class=cF1>while</span><span class=cF0> (first + i &lt; max_blk)
<a name="l44"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l45"></a> </span><span class=cFD>DriveFATBlkSet</span><span class=cF0>(drive, first + i);
<a name="l46"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>drive-&gt;cur_fat_blk, (first + i - drive-&gt;data_area) &amp; (</span><span class=cF3>BLK_SIZE</span><span class=cF0> &lt;&lt; </span><span class=cFE>3</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0>)
<a name="l47"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l48"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l49"></a> i++;
<a name="l50"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l51"></a> </span><span class=cF1>if</span><span class=cF0> (i)
<a name="l52"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l53"></a> tmpf = </span><span class=cF5>SysMAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CFreeList</span><span class=cF7>)</span><span class=cF0>);
<a name="l54"></a> tmpf-&gt;size = i;
<a name="l55"></a> tmpf-&gt;</span><span class=cF1>start</span><span class=cF0> = first;
<a name="l56"></a> </span><span class=cF5>QueueInsert</span><span class=cF0>(tmpf, drive-&gt;last_free);
<a name="l57"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l58"></a> first += i + </span><span class=cFE>1</span><span class=cF0>;
<a name="l59"></a> }
<a name="l60"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l61"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l62"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l63"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l64"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l65"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l66"></a>}
<a name="l67"></a>
<a name="l68"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>RedSeaInit</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive)
<a name="l69"></a>{
<a name="l70"></a> </span><span class=cF9>CRedSeaBoot</span><span class=cF0> br;
<a name="l71"></a> </span><span class=cF1>Bool</span><span class=cF0> unlock;
<a name="l72"></a>
<a name="l73"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l74"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l75"></a> unlock = </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l76"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, &amp;br, drive-&gt;drv_offset, </span><span class=cFE>1</span><span class=cF0>);
<a name="l77"></a> </span><span class=cF1>if</span><span class=cF0> (br.signature != </span><span class=cF3>MBR_PT_REDSEA</span><span class=cF0> || br.signature2 != </span><span class=cFE>0xAA55</span><span class=cF0>)
<a name="l78"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'Drive'</span><span class=cF0>);
<a name="l79"></a> drive-&gt;fs_type = </span><span class=cF3>FSt_REDSEA</span><span class=cF0>;
<a name="l80"></a> </span><span class=cFD>RedSeaFreeFreeList</span><span class=cF0>(drive);
<a name="l81"></a> drive-&gt;spc = </span><span class=cFE>1</span><span class=cF0>;
<a name="l82"></a> drive-&gt;size = br.sects;
<a name="l83"></a> drive-&gt;data_area = drive-&gt;drv_offset + br.bitmap_sects;
<a name="l84"></a> drive-&gt;root_clus = br.root_clus;
<a name="l85"></a> drive-&gt;fat1 = drive-&gt;fat2 = drive-&gt;drv_offset + </span><span class=cFE>1</span><span class=cF0>;
<a name="l86"></a> </span><span class=cFD>DriveFATBlkAlloc</span><span class=cF0>(drive);
<a name="l87"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l88"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l89"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l90"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l91"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l92"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l93"></a>}
<a name="l94"></a>
<a name="l95"></a></span><span class=cF1>Bool</span><span class=cF0> </span><span class=cFD>RedSeaValidate</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> drv_let)
<a name="l96"></a>{
<a name="l97"></a> </span><span class=cF9>CDrive</span><span class=cF0> *drive;
<a name="l98"></a> </span><span class=cF9>CRedSeaBoot</span><span class=cF0> br;
<a name="l99"></a>
<a name="l100"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>drive = </span><span class=cF5>Letter2Drive</span><span class=cF0>(drv_let, </span><span class=cF3>FALSE</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0> &amp;&amp; drive-&gt;fs_type == </span><span class=cF3>FSt_REDSEA</span><span class=cF0> &amp;&amp;
<a name="l101"></a> </span><span class=cF5>BlkRead</span><span class=cF7>(</span><span class=cF0>drive, &amp;br, drive-&gt;drv_offset, </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> &amp;&amp; br.signature == </span><span class=cF3>MBR_PT_REDSEA</span><span class=cF0> &amp;&amp; br.signature2 == </span><span class=cFE>0xAA55</span><span class=cF0>)
<a name="l102"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l103"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l104"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l105"></a>}
<a name="l106"></a>
<a name="l107"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cFD>RedSeaFormat</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> drv_let, </span><span class=cF1>Bool</span><span class=cF0> quick=</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l108"></a>{
<a name="l109"></a> </span><span class=cF1>U8</span><span class=cF0> *root_dir;
<a name="l110"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> *d_native;
<a name="l111"></a> </span><span class=cF9>CRedSeaBoot</span><span class=cF0> *br = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l112"></a> </span><span class=cF9>CDrive</span><span class=cF0> *drive = </span><span class=cF5>Letter2Drive</span><span class=cF0>(drv_let);
<a name="l113"></a> </span><span class=cF9>I64</span><span class=cF0> i, n, root_dir_blks;
<a name="l114"></a>
<a name="l115"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l116"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l117"></a> </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l118"></a></span><span class=cF2>//</span><span class=cF0> </span><span class=cF2>DriveTypeSet(drv_let, FSt_REDSEA);</span><span class=cF0>
<a name="l119"></a> </span><span class=cFD>DriveTypeSet</span><span class=cF0>(drv_let, </span><span class=cF3>FSt_FAT32</span><span class=cF0>);
<a name="l120"></a> drive-&gt;fs_type = </span><span class=cF3>FSt_REDSEA</span><span class=cF0>;
<a name="l121"></a> br-&gt;signature = </span><span class=cF3>MBR_PT_REDSEA</span><span class=cF0>;
<a name="l122"></a> br-&gt;signature2 = </span><span class=cFE>0xAA55</span><span class=cF0>;
<a name="l123"></a> br-&gt;drv_offset = drive-&gt;drv_offset; </span><span class=cF2>//For CD/DVD image copy.</span><span class=cF0>
<a name="l124"></a> br-&gt;sects = drive-&gt;size;
<a name="l125"></a> n = (br-&gt;sects + </span><span class=cF3>BLK_SIZE</span><span class=cF0> &lt;&lt; </span><span class=cFE>3</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>) / </span><span class=cF3>BLK_SIZE</span><span class=cF0> &lt;&lt; </span><span class=cFE>3</span><span class=cF0>;
<a name="l126"></a> br-&gt;bitmap_sects = n;
<a name="l127"></a> br-&gt;unique_id = </span><span class=cF5>TSCGet</span><span class=cF0>^</span><span class=cF5>Now</span><span class=cF0>()(</span><span class=cF9>U64</span><span class=cF0>);
<a name="l128"></a> br-&gt;root_clus = </span><span class=cFE>0</span><span class=cF0>;
<a name="l129"></a>
<a name="l130"></a> </span><span class=cF1>if</span><span class=cF0> (quick)
<a name="l131"></a> i = n + </span><span class=cFE>1</span><span class=cF0>;
<a name="l132"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l133"></a> i = drive-&gt;size;
<a name="l134"></a> </span><span class=cF5>BlkWriteZero</span><span class=cF0>(drive, drive-&gt;drv_offset, i);
<a name="l135"></a>
<a name="l136"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, br, drive-&gt;drv_offset, </span><span class=cFE>1</span><span class=cF0>);
<a name="l137"></a> </span><span class=cFD>RedSeaInit</span><span class=cF0>(drive);
<a name="l138"></a> </span><span class=cF5>ClusAlloc</span><span class=cF0>(drive, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cFE>1</span><span class=cF0>, </span><span class=cF3>FALSE</span><span class=cF0>); </span><span class=cF2>//Alloc #1</span><span class=cF0>
<a name="l139"></a>
<a name="l140"></a> root_dir_blks = </span><span class=cF5>MaxI64</span><span class=cF0>(</span><span class=cFE>1</span><span class=cF0>, drive-&gt;bd-&gt;init_root_dir_blks);
<a name="l141"></a> br-&gt;root_clus = </span><span class=cF5>ClusAlloc</span><span class=cF0>(drive, </span><span class=cFE>0</span><span class=cF0>, root_dir_blks, </span><span class=cF3>FALSE</span><span class=cF0>);
<a name="l142"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, br, drive-&gt;drv_offset, </span><span class=cFE>1</span><span class=cF0>);
<a name="l143"></a> root_dir = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF3>BLK_SIZE</span><span class=cF0> * root_dir_blks);
<a name="l144"></a>
<a name="l145"></a> d_native = root_dir - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l146"></a>
<a name="l147"></a> d_native-&gt;attr = </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0> | </span><span class=cF3>RS_ATTR_CONTIGUOUS</span><span class=cF0>;
<a name="l148"></a> *d_native-&gt;name = </span><span class=cF6>'.'</span><span class=cF0>;
<a name="l149"></a> d_native-&gt;clus = br-&gt;root_clus;
<a name="l150"></a> d_native-&gt;size = </span><span class=cF3>BLK_SIZE</span><span class=cF0> * root_dir_blks;
<a name="l151"></a> d_native-&gt;datetime = </span><span class=cF5>Now</span><span class=cF0>;
<a name="l152"></a>
<a name="l153"></a> d_native(</span><span class=cF1>U8</span><span class=cF0> *) += </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l154"></a>
<a name="l155"></a> *d_native-&gt;name = </span><span class=cF6>'.'</span><span class=cF0>;
<a name="l156"></a> d_native-&gt;name[</span><span class=cFE>1</span><span class=cF0>] = </span><span class=cF6>'.'</span><span class=cF0>;
<a name="l157"></a> d_native-&gt;attr = </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0> | </span><span class=cF3>RS_ATTR_CONTIGUOUS</span><span class=cF0>;
<a name="l158"></a> d_native-&gt;clus = br-&gt;root_clus;
<a name="l159"></a> d_native-&gt;datetime = </span><span class=cF5>Now</span><span class=cF0>;
<a name="l160"></a>
<a name="l161"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, root_dir, br-&gt;root_clus, root_dir_blks);
<a name="l162"></a> </span><span class=cFD>RedSeaInit</span><span class=cF0>(drive);
<a name="l163"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l164"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l165"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l166"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l167"></a> </span><span class=cF5>Free</span><span class=cF0>(br);
<a name="l168"></a> </span><span class=cF5>Free</span><span class=cF0>(root_dir);
<a name="l169"></a>}
<a name="l170"></a>
<a name="l171"></a></span><span class=cF1>Bool</span><span class=cF0> </span><span class=cFD>RedSeaFileFind</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF9>I64</span><span class=cF0> cur_dir_clus, </span><span class=cF1>U8</span><span class=cF0> *name, </span><span class=cF9>CDirEntry</span><span class=cF0> *_res, </span><span class=cF9>I64</span><span class=cF0> fuf_flags=</span><span class=cFE>0</span><span class=cF0>)
<a name="l172"></a>{</span><span class=cF2>//</span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/KernelA.HH.html#l3261"><span class=cF4>FUF_JUST_DIRS</span></a><span class=cF2>, </span><a href="https://zeal-operating-system.github.io/ZealOS/Kernel/KernelA.HH.html#l3262"><span class=cF4>FUF_JUST_FILES</span></a><span class=cF0>
<a name="l173"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> *buf, *buf2, *ptr;
<a name="l174"></a> </span><span class=cF1>U8</span><span class=cF0> dname[</span><span class=cF3>CDIR_FILENAME_LEN</span><span class=cF0>];
<a name="l175"></a> </span><span class=cF9>I64</span><span class=cF0> ch;
<a name="l176"></a> </span><span class=cF1>Bool</span><span class=cF0> res = </span><span class=cF3>FALSE</span><span class=cF0>, unlock;
<a name="l177"></a>
<a name="l178"></a> </span><span class=cF1>if</span><span class=cF0> (fuf_flags &amp; ~</span><span class=cF3>FUG_FILE_FIND</span><span class=cF0>)
<a name="l179"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'FUF'</span><span class=cF0>);
<a name="l180"></a> </span><span class=cF5>MemSet</span><span class=cF0>(_res, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CDirEntry</span><span class=cF7>)</span><span class=cF0>);
<a name="l181"></a> </span><span class=cF5>DriveCheck</span><span class=cF0>(drive);
<a name="l182"></a> </span><span class=cF1>if</span><span class=cF0> (drive-&gt;fs_type != </span><span class=cF3>FSt_REDSEA</span><span class=cF0>)
<a name="l183"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;Not RedSea Drive\n&quot;</span><span class=cF0>);
<a name="l184"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cFD>CFileNameTo</span><span class=cF7>(</span><span class=cF0>dname, name</span><span class=cF7>)</span><span class=cF0>)
<a name="l185"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;Invalid FileName: \&quot;%s\&quot;.\n&quot;</span><span class=cF0>, name);
<a name="l186"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l187"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l188"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l189"></a> unlock = </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l190"></a> buf2 = </span><span class=cF5>MAlloc</span><span class=cF0>(</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l191"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf2, cur_dir_clus, </span><span class=cFE>1</span><span class=cF0>);
<a name="l192"></a>
<a name="l193"></a> ptr = buf2(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l194"></a> buf = </span><span class=cF5>MAlloc</span><span class=cF0>(ptr-&gt;size);
<a name="l195"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf, cur_dir_clus, ptr-&gt;size &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>);
<a name="l196"></a> </span><span class=cF5>Free</span><span class=cF0>(buf2);
<a name="l197"></a>
<a name="l198"></a> ptr = buf(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l199"></a> *ptr-&gt;name = </span><span class=cF6>'.'</span><span class=cF0>;
<a name="l200"></a> ptr-&gt;name[</span><span class=cFE>1</span><span class=cF0>] = </span><span class=cFE>0</span><span class=cF0>;
<a name="l201"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l202"></a> {
<a name="l203"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ch = *ptr-&gt;name</span><span class=cF7>)</span><span class=cF0>)
<a name="l204"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l205"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ptr-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DELETED</span><span class=cF7>)</span><span class=cF0> &amp;&amp;
<a name="l206"></a> !</span><span class=cF7>(</span><span class=cF0>fuf_flags &amp; </span><span class=cF3>FUF_JUST_DIRS</span><span class=cF0> &amp;&amp; !(ptr-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0> &amp;&amp;
<a name="l207"></a> !</span><span class=cF7>(</span><span class=cF0>fuf_flags &amp; </span><span class=cF3>FUF_JUST_FILES</span><span class=cF0> &amp;&amp; ptr-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DIR</span><span class=cF7>)</span><span class=cF0> &amp;&amp;
<a name="l208"></a> !</span><span class=cF5>StrCompare</span><span class=cF7>(</span><span class=cF0>dname, ptr-&gt;name</span><span class=cF7>)</span><span class=cF0>)
<a name="l209"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l210"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(&amp;_res-&gt;attr, &amp;ptr-&gt;attr, </span><span class=cF3>CDIR_SIZE</span><span class=cF0>);
<a name="l211"></a> res = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l212"></a> </span><span class=cF1>goto</span><span class=cF0> rsff_done;
<a name="l213"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l214"></a> ptr(</span><span class=cF1>U8</span><span class=cF0> *) += </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l215"></a> }
<a name="l216"></a>rsff_done:
<a name="l217"></a> </span><span class=cF5>Free</span><span class=cF0>(buf);
<a name="l218"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l219"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l220"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l221"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l222"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l223"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l224"></a>
<a name="l225"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l226"></a>}
<a name="l227"></a>
<a name="l228"></a></span><span class=cF1>U8</span><span class=cF0> *</span><span class=cFD>RedSeaFileRead</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF1>U8</span><span class=cF0> *cur_dir, </span><span class=cF1>U8</span><span class=cF0> *filename, </span><span class=cF9>I64</span><span class=cF0> *_size, </span><span class=cF9>I64</span><span class=cF0> *_attr)
<a name="l229"></a>{
<a name="l230"></a> </span><span class=cF1>U8</span><span class=cF0> *buf = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l231"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> de;
<a name="l232"></a> </span><span class=cF9>I64</span><span class=cF0> c, blk_count, cur_dir_clus;
<a name="l233"></a>
<a name="l234"></a> </span><span class=cF5>DriveCheck</span><span class=cF0>(drive);
<a name="l235"></a> *_size = </span><span class=cFE>0</span><span class=cF0>;
<a name="l236"></a> *_attr = </span><span class=cFE>0</span><span class=cF0>;
<a name="l237"></a> </span><span class=cF1>if</span><span class=cF0> (drive-&gt;fs_type != </span><span class=cF3>FSt_REDSEA</span><span class=cF0>)
<a name="l238"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;Not RedSea Drive\n&quot;</span><span class=cF0>);
<a name="l239"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l240"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l241"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l242"></a> </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l243"></a> cur_dir_clus = </span><span class=cF5>Name2DirClus</span><span class=cF0>(drive, cur_dir);
<a name="l244"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFD>RedSeaFileFind</span><span class=cF7>(</span><span class=cF0>drive, cur_dir_clus, filename, &amp;de, </span><span class=cF3>FUF_JUST_FILES</span><span class=cF7>)</span><span class=cF0>)
<a name="l245"></a> {
<a name="l246"></a> blk_count = (de.size + </span><span class=cF3>BLK_SIZE</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF0>) &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>;
<a name="l247"></a> buf = </span><span class=cF5>MAlloc</span><span class=cF0>(blk_count &lt;&lt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0> + </span><span class=cFE>1</span><span class=cF0>);
<a name="l248"></a> c = de.clus;
<a name="l249"></a> c = </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf, c, blk_count);
<a name="l250"></a> buf[de.size] = </span><span class=cFE>0</span><span class=cF0>; </span><span class=cF2>//Terminate</span><span class=cF0>
<a name="l251"></a> *_size = de.size;
<a name="l252"></a> *_attr = </span><span class=cF5>FileAttr</span><span class=cF0>(de.name, de.attr);
<a name="l253"></a> }
<a name="l254"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l255"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l256"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l257"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l258"></a>
<a name="l259"></a> </span><span class=cF1>return</span><span class=cF0> buf;
<a name="l260"></a>}
<a name="l261"></a>
<a name="l262"></a></span><span class=cF1>Bool</span><span class=cF0> </span><span class=cFD>RedSeaCd</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *name, </span><span class=cF9>I64</span><span class=cF0> cur_dir_clus)
<a name="l263"></a>{
<a name="l264"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> de;
<a name="l265"></a>
<a name="l266"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dv-&gt;fs_type != </span><span class=cF3>FSt_REDSEA</span><span class=cF0>)
<a name="l267"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;Not RedSea Drive\n&quot;</span><span class=cF0>);
<a name="l268"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFD>RedSeaFileFind</span><span class=cF7>(</span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dv, cur_dir_clus, name, &amp;de, </span><span class=cF3>FUF_JUST_DIRS</span><span class=cF7>)</span><span class=cF0>)
<a name="l269"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l270"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l271"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;File not found: \&quot;%s\&quot;.\n&quot;</span><span class=cF0>, name);
<a name="l272"></a>
<a name="l273"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l274"></a>}
<a name="l275"></a>
<a name="l276"></a></span><span class=cF1>U0</span><span class=cF0> </span><span class=cF5>RedSeaFreeClus</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF9>I64</span><span class=cF0> c, </span><span class=cF9>I64</span><span class=cF0> count)
<a name="l277"></a>{
<a name="l278"></a> </span><span class=cF9>CFreeList</span><span class=cF0> *tmpf;
<a name="l279"></a> </span><span class=cF1>Bool</span><span class=cF0> found = </span><span class=cF3>FALSE</span><span class=cF0>, unlock, unlock_break;
<a name="l280"></a>
<a name="l281"></a> </span><span class=cF5>DriveCheck</span><span class=cF0>(drive);
<a name="l282"></a> </span><span class=cF1>if</span><span class=cF0> (!c)
<a name="l283"></a> </span><span class=cF1>return</span><span class=cF0>;
<a name="l284"></a> </span><span class=cF1>if</span><span class=cF0> (drive-&gt;fs_type != </span><span class=cF3>FSt_REDSEA</span><span class=cF0>)
<a name="l285"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;Not RedSea Drive\n&quot;</span><span class=cF0>);
<a name="l286"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l287"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l288"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l289"></a> unlock_break = </span><span class=cF5>BreakLock</span><span class=cF0>;
<a name="l290"></a> unlock = </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l291"></a> </span><span class=cF1>if</span><span class=cF0> (!drive-&gt;next_free)
<a name="l292"></a> </span><span class=cFD>RedSeaFreeListBuild</span><span class=cF0>(drive);
<a name="l293"></a> tmpf = drive-&gt;next_free;
<a name="l294"></a> </span><span class=cF1>while</span><span class=cF0> (!found &amp;&amp; tmpf != &amp;drive-&gt;next_free)
<a name="l295"></a> {
<a name="l296"></a> </span><span class=cF1>if</span><span class=cF0> (tmpf-&gt;</span><span class=cF1>start</span><span class=cF0> + tmpf-&gt;size == c)
<a name="l297"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l298"></a> tmpf-&gt;size += count;
<a name="l299"></a> found = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l300"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l301"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (c + count == tmpf-&gt;</span><span class=cF1>start</span><span class=cF0>)
<a name="l302"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l303"></a> tmpf-&gt;size += count;
<a name="l304"></a> tmpf-&gt;</span><span class=cF1>start</span><span class=cF0> = c;
<a name="l305"></a> found = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l306"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l307"></a> tmpf = tmpf-&gt;next;
<a name="l308"></a> }
<a name="l309"></a> </span><span class=cF1>if</span><span class=cF0> (!found)
<a name="l310"></a> {
<a name="l311"></a> tmpf = </span><span class=cF5>SysMAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CFreeList</span><span class=cF7>)</span><span class=cF0>);
<a name="l312"></a> tmpf-&gt;size = count;
<a name="l313"></a> tmpf-&gt;</span><span class=cF1>start</span><span class=cF0> = c;
<a name="l314"></a> </span><span class=cF5>QueueInsert</span><span class=cF0>(tmpf, drive-&gt;last_free);
<a name="l315"></a> }
<a name="l316"></a> </span><span class=cF1>while</span><span class=cF0> (count-- &gt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l317"></a> {
<a name="l318"></a> </span><span class=cFD>DriveFATBlkSet</span><span class=cF0>(drive, c);
<a name="l319"></a> </span><span class=cF5>LBtr</span><span class=cF0>(drive-&gt;cur_fat_blk, </span><span class=cF7>(</span><span class=cF0>c - drive-&gt;data_area</span><span class=cF7>)</span><span class=cF0> &amp; </span><span class=cF7>(</span><span class=cF3>BLK_SIZE</span><span class=cF0> &lt;&lt; </span><span class=cFE>3</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0>);
<a name="l320"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;drive-&gt;fat_blk_dirty, </span><span class=cFE>0</span><span class=cF0>);
<a name="l321"></a> c++;
<a name="l322"></a> }
<a name="l323"></a> </span><span class=cFD>DriveFATBlkClean</span><span class=cF0>(drive);
<a name="l324"></a>
<a name="l325"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l326"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l327"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l328"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l329"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l330"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l331"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l332"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l333"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l334"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l335"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l336"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l337"></a>}
<a name="l338"></a>
<a name="l339"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFD>RedSeaAllocClus</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF9>I64</span><span class=cF0> count)
<a name="l340"></a>{
<a name="l341"></a> </span><span class=cF9>CFreeList</span><span class=cF0> *tmpf, *best_free = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l342"></a> </span><span class=cF9>I64</span><span class=cF0> i, first, best_size = </span><span class=cF3>I64_MAX</span><span class=cF0>;
<a name="l343"></a> </span><span class=cF1>Bool</span><span class=cF0> unlock, unlock_break;
<a name="l344"></a>
<a name="l345"></a> </span><span class=cF1>if</span><span class=cF0> (count &lt;= </span><span class=cFE>0</span><span class=cF0>)
<a name="l346"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'Drive'</span><span class=cF0>);
<a name="l347"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l348"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l349"></a> unlock_break = </span><span class=cF5>BreakLock</span><span class=cF0>;
<a name="l350"></a> unlock = </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l351"></a> </span><span class=cF1>if</span><span class=cF0> (!drive-&gt;next_free)
<a name="l352"></a> </span><span class=cFD>RedSeaFreeListBuild</span><span class=cF0>(drive);
<a name="l353"></a> tmpf = drive-&gt;next_free;
<a name="l354"></a> </span><span class=cF1>while</span><span class=cF0> (tmpf != &amp;drive-&gt;next_free)
<a name="l355"></a> {
<a name="l356"></a> </span><span class=cF1>if</span><span class=cF0> (tmpf-&gt;size &gt;= count &amp;&amp; tmpf-&gt;size &lt; best_size)
<a name="l357"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l358"></a> best_free = tmpf;
<a name="l359"></a> best_size = tmpf-&gt;size;
<a name="l360"></a> </span><span class=cF1>if</span><span class=cF0> (tmpf-&gt;size == count)
<a name="l361"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l362"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l363"></a> tmpf = tmpf-&gt;next;
<a name="l364"></a> }
<a name="l365"></a> </span><span class=cF1>if</span><span class=cF0> (!best_free)
<a name="l366"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'Drive'</span><span class=cF0>);
<a name="l367"></a> first = best_free-&gt;</span><span class=cF1>start</span><span class=cF0>;
<a name="l368"></a> </span><span class=cF1>for</span><span class=cF0> (i = </span><span class=cFE>0</span><span class=cF0>; i &lt; count; i++)
<a name="l369"></a> {
<a name="l370"></a> </span><span class=cFD>DriveFATBlkSet</span><span class=cF0>(drive, first + i);
<a name="l371"></a> </span><span class=cF5>LBts</span><span class=cF0>(drive-&gt;cur_fat_blk, </span><span class=cF7>(</span><span class=cF0>first + i - drive-&gt;data_area</span><span class=cF7>)</span><span class=cF0> &amp; </span><span class=cF7>(</span><span class=cF3>BLK_SIZE</span><span class=cF0> &lt;&lt; </span><span class=cFE>3</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0>);
<a name="l372"></a> </span><span class=cF5>LBts</span><span class=cF0>(&amp;drive-&gt;fat_blk_dirty, </span><span class=cFE>0</span><span class=cF0>);
<a name="l373"></a> }
<a name="l374"></a> </span><span class=cFD>DriveFATBlkClean</span><span class=cF0>(drive);
<a name="l375"></a> </span><span class=cF1>if</span><span class=cF0> (best_free-&gt;size -= count)
<a name="l376"></a> best_free-&gt;</span><span class=cF1>start</span><span class=cF0> += count;
<a name="l377"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l378"></a> {
<a name="l379"></a> </span><span class=cF5>QueueRemove</span><span class=cF0>(best_free);
<a name="l380"></a> </span><span class=cF5>Free</span><span class=cF0>(best_free);
<a name="l381"></a> }
<a name="l382"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l383"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l384"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l385"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l386"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l387"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l388"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l389"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l390"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l391"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l392"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l393"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l394"></a>
<a name="l395"></a> </span><span class=cF1>return</span><span class=cF0> first;
<a name="l396"></a>}
<a name="l397"></a>
<a name="l398"></a></span><span class=cF1>Bool</span><span class=cF0> </span><span class=cFD>RedSeaDirNew</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF1>U8</span><span class=cF0> *cur_dir, </span><span class=cF9>CDirEntry</span><span class=cF0> *tmpde, </span><span class=cF1>Bool</span><span class=cF0> free_old_chain)
<a name="l399"></a>{
<a name="l400"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> *buf, *buf2, *ptr, de2;
<a name="l401"></a> </span><span class=cF9>CRedSeaBoot</span><span class=cF0> *br;
<a name="l402"></a> </span><span class=cF9>I64</span><span class=cF0> c, ch, i = </span><span class=cFE>1</span><span class=cF0>, j = </span><span class=cFE>0</span><span class=cF0>, n = </span><span class=cF3>BLK_SIZE</span><span class=cF0> / </span><span class=cF3>CDIR_SIZE</span><span class=cF0>, dir_size, cur_dir_clus;
<a name="l403"></a> </span><span class=cF1>Bool</span><span class=cF0> written = </span><span class=cF3>FALSE</span><span class=cF0>, unlock, unlock_break;
<a name="l404"></a> </span><span class=cF1>U8</span><span class=cF0> *tmp, *parent_dir;
<a name="l405"></a>
<a name="l406"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l407"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l408"></a> unlock_break = </span><span class=cF5>BreakLock</span><span class=cF0>;
<a name="l409"></a> tmpde-&gt;attr |= </span><span class=cF3>RS_ATTR_CONTIGUOUS</span><span class=cF0>;
<a name="l410"></a> unlock = </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l411"></a> cur_dir_clus = </span><span class=cF5>Name2DirClus</span><span class=cF0>(drive, cur_dir);
<a name="l412"></a> buf2 = </span><span class=cF5>MAlloc</span><span class=cF0>(</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l413"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf2, cur_dir_clus, </span><span class=cFE>1</span><span class=cF0>);
<a name="l414"></a>
<a name="l415"></a> ptr = buf2(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l416"></a> buf = </span><span class=cF5>MAlloc</span><span class=cF0>(ptr-&gt;size);
<a name="l417"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf, cur_dir_clus, ptr-&gt;size &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>);
<a name="l418"></a>
<a name="l419"></a> dir_size = ptr-&gt;size;
<a name="l420"></a> ptr = buf(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>) + </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l421"></a> </span><span class=cF5>Free</span><span class=cF0>(buf2);
<a name="l422"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l423"></a> {
<a name="l424"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ch = *ptr-&gt;name</span><span class=cF7>)</span><span class=cF0>)
<a name="l425"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l426"></a> </span><span class=cF1>if</span><span class=cF0> (!written)
<a name="l427"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(&amp;ptr-&gt;</span><span class=cF1>start</span><span class=cF0>, &amp;tmpde-&gt;</span><span class=cF1>start</span><span class=cF0>, </span><span class=cF3>CDIR_SIZE</span><span class=cF0>);
<a name="l428"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>i + </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> * </span><span class=cF3>CDIR_SIZE</span><span class=cF0> + j &lt;&lt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0> &lt; dir_size)
<a name="l429"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, buf</span><span class=cF7>(</span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF7>)</span><span class=cF0> + j &lt;&lt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>, cur_dir_clus + j, </span><span class=cFE>1</span><span class=cF0>);
<a name="l430"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l431"></a> {
<a name="l432"></a> buf2 = </span><span class=cF5>CAlloc</span><span class=cF0>(dir_size + </span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l433"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(buf2, buf, dir_size);
<a name="l434"></a> </span><span class=cF5>RedSeaFreeClus</span><span class=cF0>(drive, cur_dir_clus, dir_size &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>);
<a name="l435"></a> dir_size += </span><span class=cF3>BLK_SIZE</span><span class=cF0>;
<a name="l436"></a> c = </span><span class=cF5>ClusAlloc</span><span class=cF0>(drive, </span><span class=cFE>0</span><span class=cF0>, dir_size &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>, </span><span class=cF3>TRUE</span><span class=cF0>);
<a name="l437"></a> </span><span class=cF5>Free</span><span class=cF0>(buf);
<a name="l438"></a> buf = buf2;
<a name="l439"></a> ptr = buf(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l440"></a> ptr-&gt;size = dir_size;
<a name="l441"></a> ptr-&gt;clus = c;
<a name="l442"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, buf, c, dir_size &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>);
<a name="l443"></a> </span><span class=cF1>if</span><span class=cF0> (cur_dir_clus == drive-&gt;root_clus)
<a name="l444"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l445"></a> br = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l446"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, br, drive-&gt;drv_offset, </span><span class=cFE>1</span><span class=cF0>);
<a name="l447"></a> br-&gt;root_clus = c;
<a name="l448"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, br, drive-&gt;drv_offset, </span><span class=cFE>1</span><span class=cF0>);
<a name="l449"></a> </span><span class=cF5>Free</span><span class=cF0>(br);
<a name="l450"></a> drive-&gt;root_clus = c;
<a name="l451"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l452"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l453"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l454"></a> tmp = </span><span class=cF5>StrNew</span><span class=cF0>(cur_dir);
<a name="l455"></a> parent_dir = </span><span class=cF5>StrNew</span><span class=cF0>(cur_dir);
<a name="l456"></a> </span><span class=cF5>StrLastRemove</span><span class=cF0>(parent_dir, </span><span class=cF6>&quot;/&quot;</span><span class=cF0>, tmp);
<a name="l457"></a> </span><span class=cF1>if</span><span class=cF0> (!*parent_dir)
<a name="l458"></a> {
<a name="l459"></a> </span><span class=cF5>Free</span><span class=cF0>(parent_dir);
<a name="l460"></a> parent_dir = </span><span class=cF5>StrNew</span><span class=cF0>(</span><span class=cF6>&quot;/&quot;</span><span class=cF0>);
<a name="l461"></a> }
<a name="l462"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cFD>RedSeaFileFind</span><span class=cF7>(</span><span class=cF0>drive, </span><span class=cF5>Name2DirClus</span><span class=cF0>(drive, parent_dir), tmp, &amp;de2, </span><span class=cF3>FUF_JUST_DIRS</span><span class=cF7>)</span><span class=cF0>)
<a name="l463"></a> {
<a name="l464"></a> de2.clus = c;
<a name="l465"></a> de2.size = dir_size;
<a name="l466"></a> </span><span class=cFD>RedSeaDirNew</span><span class=cF0>(drive, parent_dir, &amp;de2, </span><span class=cF3>FALSE</span><span class=cF0>);
<a name="l467"></a> }
<a name="l468"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l469"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'Drive'</span><span class=cF0>);
<a name="l470"></a> </span><span class=cF5>Free</span><span class=cF0>(tmp);
<a name="l471"></a> </span><span class=cF5>Free</span><span class=cF0>(parent_dir);
<a name="l472"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l473"></a> }
<a name="l474"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l475"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l476"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (ptr-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DELETED</span><span class=cF0>)
<a name="l477"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l478"></a> </span><span class=cF1>if</span><span class=cF0> (!written)
<a name="l479"></a> {
<a name="l480"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(&amp;ptr-&gt;</span><span class=cF1>start</span><span class=cF0>, &amp;tmpde-&gt;</span><span class=cF1>start</span><span class=cF0>, </span><span class=cF3>CDIR_SIZE</span><span class=cF0>);
<a name="l481"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, buf</span><span class=cF7>(</span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF7>)</span><span class=cF0> + j &lt;&lt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>, cur_dir_clus + j, </span><span class=cFE>1</span><span class=cF0>);
<a name="l482"></a> written = </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l483"></a> }
<a name="l484"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l485"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l486"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l487"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF5>StrCompare</span><span class=cF7>(</span><span class=cF0>tmpde-&gt;name, ptr-&gt;name</span><span class=cF7>)</span><span class=cF0>)
<a name="l488"></a> {
<a name="l489"></a> </span><span class=cF1>if</span><span class=cF0> (free_old_chain)
<a name="l490"></a> </span><span class=cF5>RedSeaFreeClus</span><span class=cF0>(drive, ptr-&gt;clus, </span><span class=cF7>(</span><span class=cF0>ptr-&gt;size + </span><span class=cF3>BLK_SIZE</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>);
<a name="l491"></a> </span><span class=cF1>if</span><span class=cF0> (!written)
<a name="l492"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(&amp;ptr-&gt;</span><span class=cF1>start</span><span class=cF0>, &amp;tmpde-&gt;</span><span class=cF1>start</span><span class=cF0>, </span><span class=cF3>CDIR_SIZE</span><span class=cF0>);
<a name="l493"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l494"></a> ptr-&gt;attr |= </span><span class=cF3>RS_ATTR_DELETED</span><span class=cF0>;
<a name="l495"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, buf</span><span class=cF7>(</span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF7>)</span><span class=cF0> + j &lt;&lt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>, cur_dir_clus + j, </span><span class=cFE>1</span><span class=cF0>);
<a name="l496"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l497"></a> }
<a name="l498"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l499"></a> ptr(</span><span class=cF1>U8</span><span class=cF0> *) += </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l500"></a> </span><span class=cF1>if</span><span class=cF0> (++i &gt;= n)
<a name="l501"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l502"></a> j++;
<a name="l503"></a> i = </span><span class=cFE>0</span><span class=cF0>;
<a name="l504"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l505"></a> }
<a name="l506"></a> </span><span class=cF5>Free</span><span class=cF0>(buf);
<a name="l507"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l508"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l509"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l510"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l511"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l512"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l513"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l514"></a> </span><span class=cF1>if</span><span class=cF0> (unlock)
<a name="l515"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l516"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l517"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l518"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l519"></a>
<a name="l520"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>FALSE</span><span class=cF0>;
<a name="l521"></a>}
<a name="l522"></a>
<a name="l523"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFD>RedSeaFilesDel</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF1>U8</span><span class=cF0> *cur_dir, </span><span class=cF1>U8</span><span class=cF0> *files_find_mask, </span><span class=cF9>I64</span><span class=cF0> fuf_flags, </span><span class=cF1>Bool</span><span class=cF0> del_dir, </span><span class=cF1>Bool</span><span class=cF0> print_message)
<a name="l524"></a>{
<a name="l525"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> *buf, *buf2, *ptr;
<a name="l526"></a> </span><span class=cF9>I64</span><span class=cF0> i = </span><span class=cFE>0</span><span class=cF0>, res = </span><span class=cFE>0</span><span class=cF0>, ch, j = </span><span class=cFE>0</span><span class=cF0>, n = </span><span class=cF3>BLK_SIZE</span><span class=cF0> / </span><span class=cF3>CDIR_SIZE</span><span class=cF0>, cur_dir_clus;
<a name="l527"></a> </span><span class=cF1>Bool</span><span class=cF0> unlock_break;
<a name="l528"></a>
<a name="l529"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l530"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l531"></a> unlock_break = </span><span class=cF5>BreakLock</span><span class=cF0>;
<a name="l532"></a> </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l533"></a> cur_dir_clus = </span><span class=cF5>Name2DirClus</span><span class=cF0>(drive, cur_dir);
<a name="l534"></a> buf2 = </span><span class=cF5>MAlloc</span><span class=cF0>(</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l535"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf2, cur_dir_clus, </span><span class=cFE>1</span><span class=cF0>);
<a name="l536"></a>
<a name="l537"></a> ptr = buf2(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l538"></a> buf = </span><span class=cF5>MAlloc</span><span class=cF0>(ptr-&gt;size);
<a name="l539"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf, cur_dir_clus, ptr-&gt;size &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>);
<a name="l540"></a> </span><span class=cF5>Free</span><span class=cF0>(buf2);
<a name="l541"></a>
<a name="l542"></a> ptr = buf(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l543"></a> *ptr-&gt;name = </span><span class=cF6>'.'</span><span class=cF0>;
<a name="l544"></a> ptr-&gt;name[</span><span class=cFE>1</span><span class=cF0>] = </span><span class=cFE>0</span><span class=cF0>;
<a name="l545"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l546"></a> {
<a name="l547"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ch = *ptr-&gt;name</span><span class=cF7>)</span><span class=cF0>)
<a name="l548"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l549"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ptr-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DELETED</span><span class=cF7>)</span><span class=cF0> &amp;&amp; ch != </span><span class=cF6>'.'</span><span class=cF0> &amp;&amp;
<a name="l550"></a> </span><span class=cF7>(</span><span class=cF0>del_dir || !(ptr-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0> &amp;&amp; </span><span class=cF5>FilesFindMatch</span><span class=cF7>(</span><span class=cF0>ptr-&gt;name, files_find_mask, fuf_flags</span><span class=cF7>)</span><span class=cF0>)
<a name="l551"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l552"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ptr-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DIR</span><span class=cF7>)</span><span class=cF0>)
<a name="l553"></a> res++;
<a name="l554"></a> </span><span class=cF1>if</span><span class=cF0> (print_message)
<a name="l555"></a> </span><span class=cF6>&quot;Del %s\n&quot;</span><span class=cF0>, ptr-&gt;name;
<a name="l556"></a> ptr-&gt;attr |= </span><span class=cF3>RS_ATTR_DELETED</span><span class=cF0>;
<a name="l557"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, buf</span><span class=cF7>(</span><span class=cF1>U8</span><span class=cF0> *</span><span class=cF7>)</span><span class=cF0> + j &lt;&lt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>, cur_dir_clus + j, </span><span class=cFE>1</span><span class=cF0>);
<a name="l558"></a> </span><span class=cF5>RedSeaFreeClus</span><span class=cF0>(drive, ptr-&gt;clus, </span><span class=cF7>(</span><span class=cF0>ptr-&gt;size + </span><span class=cF3>BLK_SIZE</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>);
<a name="l559"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l560"></a> ptr(</span><span class=cF1>U8</span><span class=cF0> *) += </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l561"></a> </span><span class=cF1>if</span><span class=cF0> (++i &gt;= n)
<a name="l562"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l563"></a> j++;
<a name="l564"></a> i = </span><span class=cFE>0</span><span class=cF0>;
<a name="l565"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l566"></a> }
<a name="l567"></a> </span><span class=cF5>Free</span><span class=cF0>(buf);
<a name="l568"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l569"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l570"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l571"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l572"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l573"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l574"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l575"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l576"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l577"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l578"></a>
<a name="l579"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l580"></a>}
<a name="l581"></a>
<a name="l582"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cFD>RedSeaFileWrite</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF1>U8</span><span class=cF0> *cur_dir, </span><span class=cF1>U8</span><span class=cF0> *name, </span><span class=cF1>U8</span><span class=cF0> *buf, </span><span class=cF9>I64</span><span class=cF0> size, </span><span class=cF9>CDate</span><span class=cF0> cdt, </span><span class=cF9>I64</span><span class=cF0> attr)
<a name="l583"></a>{
<a name="l584"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> de;
<a name="l585"></a> </span><span class=cF9>I64</span><span class=cF0> c = </span><span class=cFE>0</span><span class=cF0>, blk_count;
<a name="l586"></a>
<a name="l587"></a> </span><span class=cF5>MemSet</span><span class=cF0>(&amp;de, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CDirEntry</span><span class=cF7>)</span><span class=cF0>);
<a name="l588"></a> </span><span class=cF1>if</span><span class=cF0> (size &lt; </span><span class=cFE>0</span><span class=cF0>)
<a name="l589"></a> size = </span><span class=cFE>0</span><span class=cF0>;
<a name="l590"></a> </span><span class=cF1>if</span><span class=cF0> (drive-&gt;fs_type != </span><span class=cF3>FSt_REDSEA</span><span class=cF0>)
<a name="l591"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;Not RedSea Drive\n&quot;</span><span class=cF0>);
<a name="l592"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cFD>CFileNameTo</span><span class=cF7>(</span><span class=cF0>de.name, name</span><span class=cF7>)</span><span class=cF0>)
<a name="l593"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;Invalid FileName: \&quot;%s\&quot;.\n&quot;</span><span class=cF0>, name);
<a name="l594"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l595"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l596"></a> </span><span class=cFD>RedSeaFilesDel</span><span class=cF0>(drive, cur_dir, de.name, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cF3>FALSE</span><span class=cF0>, </span><span class=cF3>FALSE</span><span class=cF0>);
<a name="l597"></a> de.size = size;
<a name="l598"></a> </span><span class=cF1>if</span><span class=cF0> (blk_count = </span><span class=cF7>(</span><span class=cF0>size + </span><span class=cF3>BLK_SIZE</span><span class=cF0> - </span><span class=cFE>1</span><span class=cF7>)</span><span class=cF0> &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>)
<a name="l599"></a> c = </span><span class=cF5>ClusAlloc</span><span class=cF0>(drive, </span><span class=cFE>0</span><span class=cF0>, blk_count, </span><span class=cF3>TRUE</span><span class=cF0>); </span><span class=cF2>//Always contiguous</span><span class=cF0>
<a name="l600"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l601"></a> c = </span><span class=cF3>INVALID_CLUS</span><span class=cF0>;
<a name="l602"></a> de.clus = c;
<a name="l603"></a> de.attr = attr|</span><span class=cF3>RS_ATTR_CONTIGUOUS</span><span class=cF0>;
<a name="l604"></a> de.datetime = cdt;
<a name="l605"></a> </span><span class=cF1>if</span><span class=cF0> (blk_count)
<a name="l606"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, buf, c, blk_count);
<a name="l607"></a> </span><span class=cFD>RedSeaDirNew</span><span class=cF0>(drive, cur_dir, &amp;de, </span><span class=cF3>TRUE</span><span class=cF0>);
<a name="l608"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l609"></a> </span><span class=cF1>return</span><span class=cF0> c;
<a name="l610"></a>}
<a name="l611"></a>
<a name="l612"></a></span><span class=cF9>CDirEntry</span><span class=cF0> *</span><span class=cFD>RedSeaFilesFind</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *files_find_mask, </span><span class=cF9>I64</span><span class=cF0> fuf_flags, </span><span class=cF9>CDirEntry</span><span class=cF0> *parent=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l613"></a>{
<a name="l614"></a> </span><span class=cF9>CDrive</span><span class=cF0> *drive = </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dv;
<a name="l615"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> *buf, *buf2, *ptr, *res = </span><span class=cF3>NULL</span><span class=cF0>, *tmpde;
<a name="l616"></a> </span><span class=cF9>I64</span><span class=cF0> ch, cur_dir_clus;
<a name="l617"></a>
<a name="l618"></a> </span><span class=cF1>if</span><span class=cF0> (fuf_flags &amp; ~</span><span class=cF3>FUG_FILES_FIND</span><span class=cF0>)
<a name="l619"></a> </span><span class=cF5>throw</span><span class=cF0>(</span><span class=cF6>'FUF'</span><span class=cF0>);
<a name="l620"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l621"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l622"></a> </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l623"></a> cur_dir_clus = </span><span class=cF5>Name2DirClus</span><span class=cF0>(drive, </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir);
<a name="l624"></a> buf2 = </span><span class=cF5>MAlloc</span><span class=cF0>(</span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l625"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf2, cur_dir_clus, </span><span class=cFE>1</span><span class=cF0>);
<a name="l626"></a>
<a name="l627"></a> ptr = buf2(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l628"></a> buf = </span><span class=cF5>MAlloc</span><span class=cF0>(ptr-&gt;size);
<a name="l629"></a> </span><span class=cF5>BlkRead</span><span class=cF0>(drive, buf, cur_dir_clus, ptr-&gt;size &gt;&gt; </span><span class=cF3>BLK_SIZE_BITS</span><span class=cF0>);
<a name="l630"></a> </span><span class=cF5>Free</span><span class=cF0>(buf2);
<a name="l631"></a>
<a name="l632"></a> ptr = buf(</span><span class=cF1>U8</span><span class=cF0> *) - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l633"></a> *ptr-&gt;name = </span><span class=cF6>'.'</span><span class=cF0>;
<a name="l634"></a> ptr-&gt;name[</span><span class=cFE>1</span><span class=cF0>] = </span><span class=cFE>0</span><span class=cF0>;
<a name="l635"></a> ptr(</span><span class=cF1>U8</span><span class=cF0> *) += </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l636"></a> ptr-&gt;clus = </span><span class=cF5>Name2ParentDirClus</span><span class=cF0>(drive, </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir);
<a name="l637"></a> ptr(</span><span class=cF1>U8</span><span class=cF0> *) -= </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l638"></a> </span><span class=cF1>while</span><span class=cF0> (</span><span class=cF3>TRUE</span><span class=cF0>)
<a name="l639"></a> {
<a name="l640"></a> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ch = *ptr-&gt;name</span><span class=cF7>)</span><span class=cF0>)
<a name="l641"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l642"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (!</span><span class=cF7>(</span><span class=cF0>ptr-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DELETED</span><span class=cF7>)</span><span class=cF0>)
<a name="l643"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l644"></a> tmpde = </span><span class=cF5>CAlloc</span><span class=cF0>(</span><span class=cF1>sizeof</span><span class=cF7>(</span><span class=cF9>CDirEntry</span><span class=cF7>)</span><span class=cF0>);
<a name="l645"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(&amp;tmpde-&gt;</span><span class=cF1>start</span><span class=cF0>, &amp;ptr-&gt;</span><span class=cF1>start</span><span class=cF0>, </span><span class=cF3>CDIR_SIZE</span><span class=cF0>);
<a name="l646"></a> tmpde-&gt;parent = parent;
<a name="l647"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Bt</span><span class=cF7>(</span><span class=cF0>&amp;fuf_flags, </span><span class=cF3>FUf_RECURSE</span><span class=cF7>)</span><span class=cF0> &amp;&amp; tmpde-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0> &amp;&amp; *tmpde-&gt;name != </span><span class=cF6>'.'</span><span class=cF0>)
<a name="l648"></a> {
<a name="l649"></a> tmpde-&gt;next = res;
<a name="l650"></a> res = tmpde;
<a name="l651"></a> tmpde-&gt;full_name = </span><span class=cF5>DirNameAbs</span><span class=cF0>(tmpde-&gt;name);
<a name="l652"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l653"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF5>Cd</span><span class=cF7>(</span><span class=cF0>tmpde-&gt;name</span><span class=cF7>)</span><span class=cF0>)
<a name="l654"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l655"></a> tmpde-&gt;sub = </span><span class=cFD>RedSeaFilesFind</span><span class=cF0>(files_find_mask, fuf_flags, tmpde);
<a name="l656"></a> </span><span class=cF5>Cd</span><span class=cF0>(</span><span class=cF6>&quot;..&quot;</span><span class=cF0>);
<a name="l657"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l658"></a> </span><span class=cF5>DriveLock</span><span class=cF0>(drive);
<a name="l659"></a> }
<a name="l660"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l661"></a> {
<a name="l662"></a> tmpde-&gt;full_name = </span><span class=cF5>FileNameAbs</span><span class=cF0>(tmpde-&gt;name);
<a name="l663"></a> </span><span class=cF1>if</span><span class=cF0> (</span><span class=cF7>(</span><span class=cF0>tmpde-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0> || !</span><span class=cF5>Bt</span><span class=cF0>(&amp;fuf_flags, </span><span class=cF3>FUf_JUST_DIRS</span><span class=cF0>)</span><span class=cF7>)</span><span class=cF0> &amp;&amp;
<a name="l664"></a> !</span><span class=cF7>(</span><span class=cF5>Bt</span><span class=cF0>(&amp;fuf_flags, </span><span class=cF3>FUf_RECURSE</span><span class=cF0>) &amp;&amp; *tmpde-&gt;name == </span><span class=cF6>'.'</span><span class=cF0> &amp;&amp; tmpde-&gt;attr &amp; </span><span class=cF3>RS_ATTR_DIR</span><span class=cF7>)</span><span class=cF0> &amp;&amp;
<a name="l665"></a> </span><span class=cF5>FilesFindMatch</span><span class=cF7>(</span><span class=cF0>tmpde-&gt;full_name, files_find_mask, fuf_flags</span><span class=cF7>)</span><span class=cF0>)
<a name="l666"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l667"></a> tmpde-&gt;next = res;
<a name="l668"></a> res = tmpde;
<a name="l669"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l670"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l671"></a> </span><span class=cF5>DirEntryDel</span><span class=cF0>(tmpde);
<a name="l672"></a> }
<a name="l673"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l674"></a> ptr(</span><span class=cF1>U8</span><span class=cF0> *) += </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l675"></a> }
<a name="l676"></a> </span><span class=cF5>Free</span><span class=cF0>(buf);
<a name="l677"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l678"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l679"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l680"></a> </span><span class=cF5>DriveUnlock</span><span class=cF0>(drive);
<a name="l681"></a>
<a name="l682"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l683"></a>}
<a name="l684"></a>
<a name="l685"></a></span><span class=cF1>Bool</span><span class=cF0> </span><span class=cFD>RedSeaMkDir</span><span class=cF0>(</span><span class=cF9>CDrive</span><span class=cF0> *drive, </span><span class=cF1>U8</span><span class=cF0> *cur_dir, </span><span class=cF1>U8</span><span class=cF0> *name, </span><span class=cF9>I64</span><span class=cF0> entry_count)
<a name="l686"></a>{</span><span class=cF2>//entry_count is for preallocating dir blks.</span><span class=cF0>
<a name="l687"></a> </span><span class=cF9>I64</span><span class=cF0> c, cur_dir_clus = </span><span class=cF5>Name2DirClus</span><span class=cF0>(drive, cur_dir), size = </span><span class=cF5>CeilU64</span><span class=cF0>(</span><span class=cF7>(</span><span class=cF0>entry_count + </span><span class=cFE>3</span><span class=cF7>)</span><span class=cF0> &lt;&lt; </span><span class=cFE>6</span><span class=cF0>, </span><span class=cF3>BLK_SIZE</span><span class=cF0>);
<a name="l688"></a>#</span><span class=cF1>assert</span><span class=cF0> </span><span class=cF3>CDIR_SIZE</span><span class=cF0> == </span><span class=cFE>64</span><span class=cF0>
<a name="l689"></a> </span><span class=cF1>U8</span><span class=cF0> *buf = </span><span class=cF5>CAlloc</span><span class=cF0>(size);
<a name="l690"></a> </span><span class=cF9>CDirEntry</span><span class=cF0> *d_native = buf - </span><span class=cF1>offset</span><span class=cF0>(</span><span class=cF9>CDirEntry</span><span class=cF0>.</span><span class=cF1>start</span><span class=cF0>);
<a name="l691"></a> </span><span class=cF1>Bool</span><span class=cF0> unlock_break;
<a name="l692"></a>
<a name="l693"></a> </span><span class=cF1>try</span><span class=cF0>
<a name="l694"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l695"></a> unlock_break = </span><span class=cF5>BreakLock</span><span class=cF0>;
<a name="l696"></a> c = </span><span class=cF5>FileWrite</span><span class=cF0>(name, buf, size, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0>);
<a name="l697"></a> d_native-&gt;attr = </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0> | </span><span class=cF3>RS_ATTR_CONTIGUOUS</span><span class=cF0>;
<a name="l698"></a> </span><span class=cF5>StrCopy</span><span class=cF0>(d_native-&gt;name, name);
<a name="l699"></a> d_native-&gt;clus = c;
<a name="l700"></a> d_native-&gt;size = size;
<a name="l701"></a> d_native-&gt;datetime = </span><span class=cF5>Now</span><span class=cF0>;
<a name="l702"></a> d_native(</span><span class=cF1>U8</span><span class=cF0> *) += </span><span class=cF3>CDIR_SIZE</span><span class=cF0>;
<a name="l703"></a>
<a name="l704"></a> d_native-&gt;attr = </span><span class=cF3>RS_ATTR_DIR</span><span class=cF0> | </span><span class=cF3>RS_ATTR_CONTIGUOUS</span><span class=cF0>;
<a name="l705"></a> *d_native-&gt;name = </span><span class=cF6>'.'</span><span class=cF0>;
<a name="l706"></a> d_native-&gt;name[</span><span class=cFE>1</span><span class=cF0>] = </span><span class=cF6>'.'</span><span class=cF0>;
<a name="l707"></a> d_native-&gt;name[</span><span class=cFE>2</span><span class=cF0>] = </span><span class=cFE>0</span><span class=cF0>;
<a name="l708"></a> d_native-&gt;clus = cur_dir_clus;
<a name="l709"></a> d_native-&gt;size = </span><span class=cFE>0</span><span class=cF0>;
<a name="l710"></a> d_native-&gt;datetime = </span><span class=cF5>Now</span><span class=cF0>;
<a name="l711"></a> </span><span class=cF5>BlkWrite</span><span class=cF0>(drive, buf, c, </span><span class=cFE>1</span><span class=cF0>);
<a name="l712"></a> </span><span class=cF5>Free</span><span class=cF0>(buf);
<a name="l713"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l714"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l715"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l716"></a> </span><span class=cF1>catch</span><span class=cF0>
<a name="l717"></a> </span><span class=cF1>if</span><span class=cF0> (unlock_break)
<a name="l718"></a> </span><span class=cF5>BreakUnlock</span><span class=cF0>;
<a name="l719"></a>
<a name="l720"></a> </span><span class=cF1>return</span><span class=cF0> </span><span class=cF3>TRUE</span><span class=cF0>;
<a name="l721"></a>}
</span></pre></body>
</html>