How to Update Your Website with rsync

Did you ever upload a website via FTP, scp, or even a clunky web uploader?

This can be quite cumbersome. Especially, when you forget to upload certain files and as a result, your website is broken. And doing this over and over again and trying very hard to make no mistakes can really break your spirit.

There must be a better way.

And there is! You can just sync the local directory where you keep the code of your website with rsync. This amazing tool will check each file and each subdirectory and make sure that the files that are on your server are in sync with the files on your computer. It can also delete all files that exist on the server but not on your local machine.

my-website/
├── css/
|   ├── mobile.css
│   └── style.css
├── downloads/
|   ├── download.zip
│   └── ...
├── fonts/
│   └── ...
├── js/
|   ├── jquery.js
│   └── ...
├── images/
│   └── ...
├── index.php
├── about.php
├── ...
└── sync.sh

Let’s say we have a classic PHP website. You can then just add a sync.sh script at the root of your project directory:

Continue reading “How to Update Your Website with rsync”

Better than Singletons: The Service Locator Pattern

One of the biggest problems in object-oriented programming is getting access to the objects you need.

A very common solution to this problem is dependency injection. This means you have to pass every method the objects it needs to do its work (you can also pass them to the constructor of the methods object and store them in member variables). What sounds easy and logical at first also gets messy quite quickly when applied in practice.

With dependency injection, you have to make sure the right objects are available where ever they are needed. That means you often have to pass several objects to the same method and you have to add new parameters to methods and constructors all the time. As your code grows bigger passing those objects through your project starts to feel very repetitive and when you refactor your code you have to touch a lot more methods than you would need to touch without dependency injection.

Continue reading “Better than Singletons: The Service Locator Pattern”

How to Create a Modern C Project with CMake and Conan

The C programming language is over 50 years old. Despite its age, it is still going strong. Many languages have tried to replace it. But so far none did succeed. To this very day, C is still the foundation on which all the software we use each and every day ultimately depends.

But with such a long history it comes as no surprise that there are a lot of legacy C projects out there. Projects that use outdated build systems and outdated practices. This is really a shame because, in this day and age, we have many awesome new tools at our fingertips that can help us tremendously when it comes to writing better and more robust C code.

So what if we would start a new C project from scratch today? How would we structure it? How would we build it? And what tools and features would we integrate into the build system from the start?

There are many possible answers to this question but in this article, I want to give you mine. And that’s why we are going to build the template of a modern C project from scratch. There will be code and it will compile into an executable but our main focus will be on the build system.

Continue reading “How to Create a Modern C Project with CMake and Conan”

Const Pointers and Pointers to Const Values in C

In C, the syntax and semantics of pointers that are const and pointers to const values can be a bit confusing.

Constants

Constants can be used like variables but it is only possible to read from them. Changing their initial value is not allowed.

This is where things are still simple. So let’s start with ordinary constants:

#include <stdio.h>

int main(void)
{
    const int number = 42;
    const char str[] = "Hello World";

    // It is not allowed to change the value of a const variable:
    //number = 5; // Compile Error!!!

    // Same here:
    //str[5] = 'N'; // Compile Error!!!

    printf("Number: %d\n", number);
    printf("Str: %s\n", str);
}
Continue reading “Const Pointers and Pointers to Const Values in C”

Terminate a hanging Docker Container automatically

Let’s say you have a docker container that you run periodically, and it runs fine most of the time, but in some rare cases, it hangs. This can be very annoying, and it might take some time to find the root cause of the problem.

In the meantime, it would be great if the container would be terminated automatically after exceeding a user-defined maximum runtime.

To solve this problem, you can use the following Python script (don’t forget to change image_name and max_minutes to your environment):

Continue reading “Terminate a hanging Docker Container automatically”

What is Name Mangling in C++?

When we write programs in C++, we create new functions and methods with (hopefully) descriptive names all the time. Names are important for us humans to understand code. But in the compiled machine code of our finished executables, they don’t play any role. The CPU doesn’t even know about them.

Whenever our program has loaded, all functions and methods are placed at specific memory addresses of the virtual address space assigned to the process of our running program. Whenever a function calls another function, it doesn’t call the name of the function but the address where the function is located in memory.

Continue reading “What is Name Mangling in C++?”