Article 6GQ9F CodeSOD: Free From Space

CodeSOD: Free From Space

by
Remy Porter
from The Daily WTF on (#6GQ9F)

Henrik H is contracting with a client, and that client uses a number of other contractors. Some of them have... interesting approaches to problem solving.

For example, one of the servers is a Windows server, which stores a lot of temp files on the D: drive. So someone needed to write a C# function that would check the available space, and if it exceeded some threshold, delete the temp files.

This is what they came up with.

 private void Serverspacedelete() { System.IO.DriveInfo di = new System.IO.DriveInfo("D"); double byteCount = Convert.ToDouble(di.TotalFreeSpace.ToString()); string size = "0"; if (byteCount >= 1073741824.0) size = String.Format("{0:##.##}", byteCount / 1073741824.0);// + " GB"; else if (byteCount >= 1048576.0) size = String.Format("{0:##.##}", byteCount / 1048576.0);// + " MB"; else if (byteCount >= 1024.0) size = String.Format("{0:##.##}", byteCount / 1024.0);// + " KB"; else if (byteCount > 0 && byteCount < 1024.0) size = byteCount.ToString(); // + " Bytes"; if (size.Contains(',')) size = size.Substring(0, size.LastIndexOf(',')); if (size.Contains('.')) size = size.Substring(0, size.LastIndexOf('.')); int k = Convert.ToInt32(size); if (k <= 10) {// code deleting temp files } }

What we have here is a developer who started this function by searching the Internet for "how to get free drive space c#" and found a sample program that would pretty print the size in human readable values. They copy-pasted some of that code here, commented-out the GB suffix, and were off to the races.

Which brings us to the attempts to trim the string- if it contains a ",", grab only everything before the last comma. If it contains a ".", grab everything before the decimal point.

I can only think that they wanted to maybe remove those symbols? Which they didn't need to, because Convert.ToInt32 can parse all of that just fine. But I can't think of a logical reason to do this otherwise.

The end result of this is that if they have less than 10 bytes free, they'll delete the temp files. If they have less than 10kb free, they'll delete the temp files. If they have less than 10MBs free, they'll delete the temp files. 10GB, same thing. But also, because of that comma substring, for anything less than 10TB.

otter-icon.png [Advertisement] Otter - Provision your servers automatically without ever needing to log-in to a command prompt. Get started today!
External Content
Source RSS or Atom Feed
Feed Location http://syndication.thedailywtf.com/TheDailyWtf
Feed Title The Daily WTF
Feed Link http://thedailywtf.com/
Reply 0 comments