Saturday, October 7, 2017

[Fix]: No Audio in Linux / Videos Don't Play

The Problem
There exists an especially frustrating problem on some linux distributions where audio will simply not play. The lack of audio playing can mean that you're also not able to watch videos, even completely without audio! Most recently, I installed Ubuntu 17.04 to find that I was unable to listen to music or even watch Youtube videos with subtitles. That's untenable, and a complete deal-breaker for most people (for obvious reason).

The Explanation
So, what's really happening here is that your user isn't in the audio group. Because they're not in the audio group, videos which contain audio (i.e all video file formats) are simply not allowed to play! Why users aren't de facto members of the audio group, especially on Linux, is completely lost on me. But, it's enough information to fix the problem thankfully. 

(Optional) A Quick Rundown on Groups
For the uninitiated; Linux has two essential components of its security system: groups and users. Users are added to groups to give them certain privileges. So, since your user is NOT a member of the audio group, you cannot play audio.

The Fix
The fix for this couldn't be easier. You need to run the following command:

sudo usermod -a -G groupName userName
Then, we're already half way done! You are now a member of the audio group. 
To apply this fix, you *need* to shut down and power back up
For me, nothing actually applied this change other than a full shutdown / restart cycle. Even rebooting didn't actually do it for me, strangely enough. That goes against all of my intuition as an avid computer user and programmer. I can't explain it.
Once you've reboot, you should then be able to run the following command:
Then, hover over the columns which read 'MM' and hit 'm' to unmute them. Raise their volume to 100%. Your desktop environment will control the actual volume you here, but you don't want the underlying sound system to artificially lower your volume.
The Last Step
The final thing you have to do is to run the following command:
Then, from here, set up whatever audio device you'll be using. In my case, with headphones plugged into the audio jack it wound up being 'Line Out'. Under the configuration tab, I needed to select Audio Stereo Duplex for it to work. I'm also unsure why the Audio Stereo Duplex configuration works, but I know it does.
You're Done!
After all of this, you should have working audio. As you can tell, the rumor that Linux audio can be a headache is proven to be true. Videos should already be working. If you are still having trouble with it, leave a comment below and I'll do my best to help out.

Tuesday, March 7, 2017

[How To]: Not commit when a string is present in git

The Use Case
Howdy! So, I ran into an interesting problem this week. In order to test something, I had to make a change to code that should NEVER be commit... then, I commit it. D'oh!

If you want to protect yourself from something similar, or prevent data leakage, read on.

The Setup
When you clone or initialize a git project, git creates a "./.git" folder for you. This is where it tracks information about the repository, and stores some git metadata and logs.

Another thing this folder contains are "hooks", which are run at some point during the git workflow. For instance, if we had a "pre-push" hook, it would run before the push goes up to the remote.

So, what can we do with these hooks? Anything that BASH lets us. The most common use case for hooks is stopping a commit if something isn't right with the project, which is what we'll be doing in our case. However, you could do things like:

  • Print out the total number of lines changed
  • Check for a string and just give warnings (i,e mispellings, swear words)
  • Queue a local or remote backup
  • Package the project
Really, anything.

Protecting Ourselves
We would like to not commit if a string is present in the file. Imagine if we wanted to search for //[DANGER]. The presence of [DANGER] in any tracked file would stop the commit, so you could remember to go back and change the reason for it being there.

To do this, open the pre-commit.sample file and save it as pre-commit (with no extension). If there isn't a sample file, simply make the pre-commit file.

Before we write any code, let's look at what we want:

  • If a string is present ANYWHERE in the directory, fail
Alternatively, in large code bases, we want:
  • Search the diff for newly added instances of the string
Since searching through every file is going to take too long to do on every commit.

The Steps

Make the File
You should now have a file called ./.git/hooks/pre-commit. You make need to mark the pre-commit file as executable with chmod + x pre-commit.

Add this code to the file

printf "[Check]: Searching for commit mollyguard: '%s'..." "$breakingString"
sleep 1
#                                                                       VVVVVVV YOUR REGEX RIGHT THERE
if [ $(git diff --word-diff --cached --diff-filter=ACUM -U0 | grep -E "+*\[DANGER\]" | wc -l) -gt 0 ]
printf "\n"
cat <<\EOF
Error: Trying to commit a tree that contains uncommitable changes 

Search the projects for $breakingString and figure out why the comment is there
exit 1;

printf "Good!\n"

What this code is doing: 
  • Look at the last diff
  • Search the diff for "+{string}" (because + means it was ADDED, not removed in the last commit)
  • Count the lines
If the lines are greater than 0, and instance was found, so we shouldn't commit this branch.

Of course, this is searching just the latest commit, because searching a large software project is going to take a while. If you're working on a smaller project (or on a very fast machine), you can replace the if statement with simply

if [ $(grep -R "\[DANGER\]"../../  | wc- l) > 0 ]

Sunday, March 5, 2017

.NET - "%5C" Showing Up on URLs

Are you a developer, who is accepting a file or some string of text and putting it into a cloud? Are you, also, per chance, retrieving its URL, and notcing this pesky "%5C popping up in our URLs? Well, I know recently I was. It was a pretty strange, out of the blue error with an interesting cause. Here are some details for you:

What Causes This?
The actual causes of this problem are numerous. The bottom line is, '%5C' in a URL means that the backslash '\' got encoded in the URL. Somewhere along the way, either unintentionally or on purpose, a string containing one wound up in the encoding step.

The culprit for many users is using .NET's Path utility for trying to parse URLs. Here's an example:

Let's look at expectation vs Reality

Code: Path.GetDirectoryName("I/want/to/upload/to/this/place")
Expected: "I/want/to/upload/to/this/"
Actual: "I\want\to\upload\to\this"

This is because Path uses a field called Path.DirectorySeperatorChar, which on Windows is '\'... A bit backwards that Path can READ forward slashes as directory names, yet only output backslashes in the return value.

How Do I Fix This?
There are plenty of good solutions to fix this. However, with so many different causes to the problem, it's hard to say exactly.

To Fix it in a simple string
It's possible that all you need to do is Path.GetDirectoryName(your_string).Replace(Path.DirectorySeperatorChar, Path.AltDirectorySeperatorChar), and you're good to go.

Hopefully this shed some light on what the problem may be in your case. Hopefully, moving forward, Microsoft is not afraid to add a .NET feature like PathFlags or something so that this doesn't crop up as much as it currently does.