ZealOS/docs/Kernel/BlkDev/DiskFile.CC.html
2021-08-09 02:04:03 -04:00

148 lines
14 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.14">
<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=cF1>U8</span><span class=cF0> *</span><span class=cF5>FileRead</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *filename, </span><span class=cF9>I64</span><span class=cF0> *_size=</span><span class=cF3>NULL</span><span class=cF0>, </span><span class=cF9>I64</span><span class=cF0> *_attr=</span><span class=cF3>NULL</span><span class=cF0>)
<a name="l2"></a>{</span><span class=cF2>//Read whole file from disk.</span><span class=cF0>
<a name="l3"></a> </span><span class=cF9>CHashGeneric</span><span class=cF0> *tmph;
<a name="l4"></a> </span><span class=cF1>U8</span><span class=cF0> *absname, *res = </span><span class=cF3>NULL</span><span class=cF0>;
<a name="l5"></a> </span><span class=cF9>I64</span><span class=cF0> size = </span><span class=cFE>0</span><span class=cF0>, attr = </span><span class=cFE>0</span><span class=cF0>;
<a name="l6"></a> </span><span class=cF9>CDirContext</span><span class=cF0> *dirc;
<a name="l7"></a>
<a name="l8"></a> absname = </span><span class=cF5>FileNameAbs</span><span class=cF0>(filename);
<a name="l9"></a> </span><span class=cF1>if</span><span class=cF0> (tmph = </span><span class=cF5>HashFind</span><span class=cF7>(</span><span class=cF0>absname, </span><span class=cFB>sys_task</span><span class=cF0>-&gt;hash_table, </span><span class=cF3>HTT_FILE</span><span class=cF7>)</span><span class=cF0>)
<a name="l10"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l11"></a> size = tmph-&gt;user_data1;
<a name="l12"></a> res = </span><span class=cF5>MAlloc</span><span class=cF0>(size + </span><span class=cFE>1</span><span class=cF0>);
<a name="l13"></a> </span><span class=cF5>MemCopy</span><span class=cF0>(res, tmph-&gt;user_data0, size);
<a name="l14"></a> res[size] = </span><span class=cFE>0</span><span class=cF0>; </span><span class=cF2>//Terminate</span><span class=cF0>
<a name="l15"></a> attr = </span><span class=cF5>FileAttr</span><span class=cF0>(tmph-&gt;str, attr);
<a name="l16"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l17"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l18"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l19"></a> </span><span class=cF1>if</span><span class=cF0> (dirc = </span><span class=cF5>DirContextNew</span><span class=cF7>(</span><span class=cF0>absname</span><span class=cF7>)</span><span class=cF0>)
<a name="l20"></a> {
<a name="l21"></a> </span><span class=cF1>switch</span><span class=cF0> (dirc-&gt;drive-&gt;fs_type)
<a name="l22"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l23"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>FSt_REDSEA</span><span class=cF0>:
<a name="l24"></a> res = </span><span class=cFD>RedSeaFileRead</span><span class=cF0>(dirc-&gt;drive, </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir, dirc-&gt;mask, &amp;size, &amp;attr);
<a name="l25"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l26"></a>
<a name="l27"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>FSt_FAT32</span><span class=cF0>:
<a name="l28"></a> res = </span><span class=cFD>FAT32FileRead</span><span class=cF0>(dirc-&gt;drive, </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir, dirc-&gt;mask, &amp;size, &amp;attr);
<a name="l29"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l30"></a>
<a name="l31"></a> </span><span class=cF1>default</span><span class=cF0>:
<a name="l32"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;File System Not Supported\n&quot;</span><span class=cF0>);
<a name="l33"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l34"></a> </span><span class=cF5>DirContextDel</span><span class=cF0>(dirc);
<a name="l35"></a> }
<a name="l36"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l37"></a>
<a name="l38"></a> </span><span class=cF1>if</span><span class=cF0> (!res)
<a name="l39"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l40"></a> </span><span class=cF1>if</span><span class=cF0> (dirc = </span><span class=cF5>DirContextNew</span><span class=cF7>(</span><span class=cF0>absname</span><span class=cF7>)</span><span class=cF0>)
<a name="l41"></a> {
<a name="l42"></a> </span><span class=cF1>while</span><span class=cF0> (!res &amp;&amp; </span><span class=cF5>StrCompare</span><span class=cF7>(</span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir, </span><span class=cF6>&quot;/&quot;</span><span class=cF7>)</span><span class=cF0>)
<a name="l43"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l44"></a> </span><span class=cF5>Cd</span><span class=cF0>(</span><span class=cF6>&quot;..&quot;</span><span class=cF0>);
<a name="l45"></a> </span><span class=cF1>switch</span><span class=cF0> (</span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dv-&gt;fs_type)
<a name="l46"></a> {
<a name="l47"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>FSt_REDSEA</span><span class=cF0>:
<a name="l48"></a> res = </span><span class=cFD>RedSeaFileRead</span><span class=cF0>(dirc-&gt;drive, </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir, dirc-&gt;mask, &amp;size, &amp;attr);
<a name="l49"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l50"></a>
<a name="l51"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>FSt_FAT32</span><span class=cF0>:
<a name="l52"></a> res = </span><span class=cFD>FAT32FileRead</span><span class=cF0>(dirc-&gt;drive, </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir, dirc-&gt;mask, &amp;size, &amp;attr);
<a name="l53"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l54"></a>
<a name="l55"></a> </span><span class=cF1>default</span><span class=cF0>:
<a name="l56"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;File System Not Supported\n&quot;</span><span class=cF0>);
<a name="l57"></a> }
<a name="l58"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l59"></a> </span><span class=cF5>DirContextDel</span><span class=cF0>(dirc);
<a name="l60"></a> }
<a name="l61"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l62"></a> </span><span class=cF1>if</span><span class=cF0> (!res)
<a name="l63"></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>, filename);
<a name="l64"></a> </span><span class=cF1>if</span><span class=cF0> (res &amp;&amp; attr &amp; </span><span class=cF3>RS_ATTR_RESIDENT</span><span class=cF0>)
<a name="l65"></a> </span><span class=cF5>HashGenericAdd</span><span class=cF0>(absname, </span><span class=cF3>HTT_FILE</span><span class=cF0>, </span><span class=cF5>SysMAllocIdent</span><span class=cF7>(</span><span class=cF0>res</span><span class=cF7>)</span><span class=cF0>, size, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cFB>sys_task</span><span class=cF0>);
<a name="l66"></a>
<a name="l67"></a> </span><span class=cF1>if</span><span class=cF0> (_attr)
<a name="l68"></a> *_attr = attr;
<a name="l69"></a> </span><span class=cF1>if</span><span class=cF0> (_size)
<a name="l70"></a> *_size = size;
<a name="l71"></a> </span><span class=cF5>Free</span><span class=cF0>(absname);
<a name="l72"></a>
<a name="l73"></a> </span><span class=cF1>return</span><span class=cF0> res;
<a name="l74"></a>}
<a name="l75"></a>
<a name="l76"></a></span><span class=cF9>I64</span><span class=cF0> </span><span class=cF5>FileWrite</span><span class=cF0>(</span><span class=cF1>U8</span><span class=cF0> *filename, </span><span class=cF1>U8</span><span class=cF0> *fbuf, </span><span class=cF9>I64</span><span class=cF0> size, </span><span class=cF9>CDate</span><span class=cF0> cdt=</span><span class=cFE>0</span><span class=cF0>, </span><span class=cF9>I64</span><span class=cF0> attr=</span><span class=cFE>0</span><span class=cF0>)
<a name="l77"></a>{</span><span class=cF2>//Write whole file to disk.</span><span class=cF0>
<a name="l78"></a> </span><span class=cF9>I64</span><span class=cF0> c = </span><span class=cFE>0</span><span class=cF0>;
<a name="l79"></a> </span><span class=cF9>CHashGeneric</span><span class=cF0> *tmph;
<a name="l80"></a> </span><span class=cF9>CDirContext</span><span class=cF0> *dirc;
<a name="l81"></a> </span><span class=cF1>U8</span><span class=cF0> *absname = </span><span class=cF5>FileNameAbs</span><span class=cF0>(filename);
<a name="l82"></a>
<a name="l83"></a> </span><span class=cF1>if</span><span class=cF0> (dirc = </span><span class=cF5>DirContextNew</span><span class=cF7>(</span><span class=cF0>filename, </span><span class=cF3>FALSE</span><span class=cF0>, </span><span class=cF3>TRUE</span><span class=cF7>)</span><span class=cF0>)
<a name="l84"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l85"></a> attr = </span><span class=cF5>FileAttr</span><span class=cF0>(dirc-&gt;mask, attr);
<a name="l86"></a> </span><span class=cF1>if</span><span class=cF0> (!cdt)
<a name="l87"></a> cdt = </span><span class=cF5>Now</span><span class=cF0>;
<a name="l88"></a> </span><span class=cF1>switch</span><span class=cF0> (dirc-&gt;drive-&gt;fs_type)
<a name="l89"></a> {
<a name="l90"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>FSt_REDSEA</span><span class=cF0>:
<a name="l91"></a> c = </span><span class=cFD>RedSeaFileWrite</span><span class=cF0>(dirc-&gt;drive, </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir, dirc-&gt;mask, fbuf, size, cdt, attr);
<a name="l92"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l93"></a>
<a name="l94"></a> </span><span class=cF1>case</span><span class=cF0> </span><span class=cF3>FSt_FAT32</span><span class=cF0>:
<a name="l95"></a> c = </span><span class=cFD>FAT32FileWrite</span><span class=cF0>(dirc-&gt;drive, </span><span class=cF5>Fs</span><span class=cF0>-&gt;cur_dir, dirc-&gt;mask, fbuf, size, cdt, attr);
<a name="l96"></a> </span><span class=cF1>break</span><span class=cF0>;
<a name="l97"></a>
<a name="l98"></a> </span><span class=cF1>default</span><span class=cF0>:
<a name="l99"></a> </span><span class=cF5>PrintErr</span><span class=cF0>(</span><span class=cF6>&quot;File System Not Supported\n&quot;</span><span class=cF0>);
<a name="l100"></a> }
<a name="l101"></a> </span><span class=cF1>if</span><span class=cF0> (tmph = </span><span class=cF5>HashFind</span><span class=cF7>(</span><span class=cF0>absname, </span><span class=cFB>sys_task</span><span class=cF0>-&gt;hash_table, </span><span class=cF3>HTT_FILE</span><span class=cF7>)</span><span class=cF0>)
<a name="l102"></a> {
<a name="l103"></a> </span><span class=cF1>if</span><span class=cF0> (attr &amp; </span><span class=cF3>RS_ATTR_RESIDENT</span><span class=cF0>)
<a name="l104"></a> </span><span class=cF7>{</span><span class=cF0>
<a name="l105"></a> </span><span class=cF5>Free</span><span class=cF0>(tmph-&gt;user_data0);
<a name="l106"></a> tmph-&gt;user_data0 = </span><span class=cF5>SysMAllocIdent</span><span class=cF0>(fbuf);
<a name="l107"></a> tmph-&gt;user_data1 = size;
<a name="l108"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l109"></a> </span><span class=cF1>else</span><span class=cF0>
<a name="l110"></a> </span><span class=cF5>HashRemDel</span><span class=cF0>(tmph, </span><span class=cFB>sys_task</span><span class=cF0>-&gt;hash_table);
<a name="l111"></a> }
<a name="l112"></a> </span><span class=cF1>else</span><span class=cF0> </span><span class=cF1>if</span><span class=cF0> (attr &amp; </span><span class=cF3>RS_ATTR_RESIDENT</span><span class=cF0>)
<a name="l113"></a> </span><span class=cF5>HashGenericAdd</span><span class=cF0>(absname, </span><span class=cF3>HTT_FILE</span><span class=cF0>, </span><span class=cF5>SysMAllocIdent</span><span class=cF7>(</span><span class=cF0>fbuf</span><span class=cF7>)</span><span class=cF0>, size, </span><span class=cFE>0</span><span class=cF0>, </span><span class=cFB>sys_task</span><span class=cF0>);
<a name="l114"></a> </span><span class=cF5>DirContextDel</span><span class=cF0>(dirc);
<a name="l115"></a> </span><span class=cF7>}</span><span class=cF0>
<a name="l116"></a> </span><span class=cF5>Free</span><span class=cF0>(absname);
<a name="l117"></a>
<a name="l118"></a> </span><span class=cF1>return</span><span class=cF0> c;
<a name="l119"></a>}
</span></pre></body>
</html>