<titledata-rh="true">Practice exam B | mf</title><metadata-rh="true"name="viewport"content="width=device-width,initial-scale=1"><metadata-rh="true"name="twitter:card"content="summary_large_image"><metadata-rh="true"property="og:url"content="https://blog.mfocko.xyz/c/pexam/garbage_collect/"><metadata-rh="true"property="og:locale"content="en"><metadata-rh="true"name="docusaurus_locale"content="en"><metadata-rh="true"name="docsearch:language"content="en"><metadata-rh="true"name="docusaurus_version"content="current"><metadata-rh="true"name="docusaurus_tag"content="docs-c-current"><metadata-rh="true"name="docsearch:version"content="current"><metadata-rh="true"name="docsearch:docusaurus_tag"content="docs-c-current"><metadata-rh="true"property="og:title"content="Practice exam B | mf"><metadata-rh="true"name="description"content="Garbageeverywhere…
<li>You <strong>are not</strong> allowed to use your own source code, e.g. prepared beforehand
or from the seminars.</li>
<li>You have <strong>5 minutes</strong> to read through the assignment and ask any follow-up
questions should be there something unclear.</li>
<li>You have <strong>60 minutes</strong> to work on the assignment, afterward your work will be
discussed with your seminar tutor.</li>
</ul></div></div>
<p>You have gotten into a trouble during your regular upgrade of your archLinux<sup><ahref="#user-content-fn-1"id="user-content-fnref-1"data-footnote-ref="true"aria-describedby="footnote-label">1</a></sup>
installation… You've been carelessly running the upgrades for months and forgot
about clearing up the caches.</p>
<p>Your task is to write a program <code>garbage_collect</code> that will evaluate the shell
history provided as a file and will try to find files or directories that are
suspiciously big and decide which of them should be deleted to free some space.</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="format-of-the-shell-history">Format of the shell history<ahref="#format-of-the-shell-history"class="hash-link"aria-label="Direct link to Format of the shell history"title="Direct link to Format of the shell history"></a></h2>
<p>You are provided one file consisting of the captured buffer of the terminal. You
can see only two commands being used:</p>
<ol>
<li>
<p><code>cd ‹somewhere›</code> that changes the current working directory.</p>
<p>At the beginning you start in the root of the filesystem (i.e. <code>/</code>).</p>
<p>You are <strong>guaranteed</strong> that <code>‹somewhere›</code> is:</p>
<ul>
<li><code>.</code> that is a current working directory (i.e. does nothing),</li>
<li><code>..</code> that moves you up one level (in case you are in <code>/</code>, does nothing), or</li>
<li>is a valid directory in the current working directory.</li>
</ul>
<divclass="theme-admonition theme-admonition-caution admonition_xJq3 alert alert--warning"><divclass="admonitionHeading_Gvgb"><spanclass="admonitionIcon_Rf37"><svgviewBox="0 0 16 16"><pathfill-rule="evenodd"d="M8.893 1.5c-.183-.31-.52-.5-.887-.5s-.703.19-.886.5L.138 13.499a.98.98 0 0 0 0 1.001c.193.31.53.501.886.501h13.964c.367 0 .704-.19.877-.5a1.03 1.03 0 0 0 .01-1.002L8.893 1.5zm.133 11.497H6.987v-2.003h2.039v2.003zm0-3.004H6.987V5.987h2.039v4.006z"></path></svg></span>caution</div><divclass="admonitionContent_BuS1"><p>There are no guarantees or restrictions on the names of the files or
directories!</p></div></div>
</li>
<li>
<p><code>ls</code> that will list files in the current working directory and their
respective sizes. If there is a directory in the current working it has <code>dir</code>
instead of the size.</p>
</li>
</ol>
<divclass="codeBlockContainer_Ckt0 theme-code-block"style="--prism-color:#000000;--prism-background-color:#ffffff"><divclass="codeBlockContent_biex"><pretabindex="0"class="prism-code language-text codeBlock_bY9V thin-scrollbar"style="color:#000000;background-color:#ffffff"><codeclass="codeBlockLines_e6Vv"><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ls</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">dir a</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">14848514 b.txt</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">8504156 c.dat</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">dir d</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd a</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd .</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd .</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd .</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ls</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">dir e</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">29116 f</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">2557 g</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">62596 h.lst</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd e</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ls</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">584 i</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd ..</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd ..</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ cd d</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">$ ls</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">4060174 j</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">8033020 d.log</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">5626152 d.ext</span><br></span><spanclass="token-line"style="color:#000000"><spanclass="token plain">7214296 k</span><br></span></code></pre><divclass="buttonGroup__atx"><buttontype="button"aria-label="Copy code to clipboard"title="Copy"class="clean-btn"><spanclass="copyButtonIcons_eSgA"aria-hidden="true"><svgviewBox="0 0 24 24"class="copyButtonIcon_y97N"><pathfill="currentColor"d="M19,21H8V7H19M19,5H8A2,2 0 0,0 6,7V21A2,2 0 0,0 8,23H19A2,2 0 0,0 21,21V7A2,2 0 0,0 19,5M16,1H4A2,2 0 0,0 2,3V17H4V3H16V1Z"></path></svg><svgviewBox="0 0 24 24"class="copyButtonSuccessIcon_LjdS"><pathfill="currentColor"d="M21,7L9,19L3.5,13.5L4.91,12.09L9,16.17L19.59,5.59L21,7Z"></path></svg></span></button></div></div></div>
<p>For this input, you will get following file system:</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="format-of-the-output">Format of the output<ahref="#format-of-the-output"class="hash-link"aria-label="Direct link to Format of the output"title="Direct link to Format of the output"></a></h2>
<p>Your program should support 2 switches:</p>
<ul>
<li><code>-gt ‹min_size›</code> that will print out suspiciously big files.</li>
<li><code>-f ‹total_size›‹min_unused›</code> that will print out a file to be deleted.</li>
</ul>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="-gt-min_size"><code>-gt ‹min_size›</code><ahref="#-gt-min_size"class="hash-link"aria-label="Direct link to -gt-min_size"title="Direct link to -gt-min_size"></a></h3>
<p>With this switch you are provided one additional argument:</p>
<ul>
<li><code>min_size</code> that is the lower bound (inclusive) for size of any file or
directory that is supposed to be listed.</li>
</ul>
<p>When your program is being run with this switch, it is is supposed to print out
all files <strong>and</strong> directories that are bigger than the provided <code>min_size</code>.</p>
<h3class="anchor anchorWithStickyNavbar_LWe7"id="-f-total_size-min_unused"><code>-f ‹total_size›‹min_unused›</code><ahref="#-f-total_size-min_unused"class="hash-link"aria-label="Direct link to -f-total_size-min_unused"title="Direct link to -f-total_size-min_unused"></a></h3>
<p>With this switch you are provided two additional arguments:</p>
<ul>
<li><code>total_size</code> that is a total size of the filesystem<sup><ahref="#user-content-fn-2"id="user-content-fnref-2"data-footnote-ref="true"aria-describedby="footnote-label">2</a></sup>.</li>
<li><code>min_unused</code> that is a minimum of free space required for an upgrade.</li>
</ul>
<p>Your program should find <strong>exactly one</strong> file or a directory that is of the
smallest size, but big enough to free enough space for the upgrade to proceed.</p>
<p>In other words, if that file or directory is deleted, following should hold:</p>
<h2class="anchor anchorWithStickyNavbar_LWe7"id="example-usage">Example usage<ahref="#example-usage"class="hash-link"aria-label="Direct link to Example usage"title="Direct link to Example usage"></a></h2>
<p>You can have a look at the example usage of your program. We can run your
<h2class="anchor anchorWithStickyNavbar_LWe7"id="requirements-and-notes">Requirements and notes<ahref="#requirements-and-notes"class="hash-link"aria-label="Direct link to Requirements and notes"title="Direct link to Requirements and notes"></a></h2>
<ul>
<li>Define <strong>structures</strong> (and <strong>enumerations</strong>, if applicable) for the parsed
information from the files.</li>
<li>For keeping the “records”, use some <strong>dynamic</strong> data structure.<!---->
<ul>
<li>Don't forget to consider pros and cons of using <em>specific</em> data structures
before going through implementing.</li>
</ul>
</li>
<li>You <strong>are not required</strong> to produce 1:1 output to the provided examples, they
are just a hint to not waste your time tinkering with a user experience.</li>
<li>If any of the operations on the input files should fail,
<strong>you are expected to</strong> handle the situation <em>accordingly</em>.</li>
<li>Failures of any other common functions (e.g. functions used for memory
management) should be handled in <strong>the same way</strong> as they were in the
homeworks and seminars.</li>
<li>Your program <strong>must free</strong> all the resources before exiting.</li>
</ul>
<sectiondata-footnotes="true"class="footnotes"><h2class="anchor anchorWithStickyNavbar_LWe7 sr-only"id="footnote-label">Footnotes<ahref="#footnote-label"class="hash-link"aria-label="Direct link to Footnotes"title="Direct link to Footnotes"></a></h2>
<ol>
<liid="user-content-fn-1">
<p>Also applies to Fedora, but… we use arch btw <!---->😉 <ahref="#user-content-fnref-1"data-footnote-backref=""aria-label="Back to reference 1"class="data-footnote-backref">↩</a></p>
</li>
<liid="user-content-fn-2">
<p>duh! <ahref="#user-content-fnref-2"data-footnote-backref=""aria-label="Back to reference 2"class="data-footnote-backref">↩</a></p>