text:
search:
   keywords: ['storage', 'paginated local storage', 'PLocal']
---

# PLocal Storage

The Paginated Local Storage, "**plocal**" from now, is a disk based storage which works with data using page model.

plocal storage consists of several components each of those components use disk data through **[disk cache](plocal-storage-disk-cache.md)**.

Below is list of plocal storage components and short description of each of them:

1. **Clusters** are managed by 2 kinds of files:
 - **.pcl** files contain the cluster data
 - **.cpm** files contain the mapping between record's cluster position and real physical position
2. **Write Ahead (operation) Log ([WAL](Write-Ahead-Log.md))** are managed by 2 kinds of files:
 - **.wal** to store the log content
 - **.wmr** contains timing about synchronization operations between storage cache and disk system
3. **SBTree Index**, it uses files with extensions **.sbt**.
4. **Hash Index**, it uses files with extensions **.hit**, **.him** and **.hib**.
5. **Index Containers** to store values of single entries of not unique index (Index RID Set). It uses files with extension **.irs**.
6. **File mapping**, maps between file names and file ids (used internally). It's a single file with name: **name_id_map.cm**.

## File System
Since PLOCAL is disk-based, all pages are flushed to physical files. You can specify any mounted partitions on your machine, backed by Disks, SSD, Flash Disks or DRAM.

## Cluster

**Cluster** is logical piece of disk space where storage stores records data. Each cluster is split in pages. **Page** is a single atomic unit, which is used by cluster.

Each page contains system information and records data. System information includes "magic number" and a crc32 check sum of the page content. This information is used to check storage integrity after a DB crash. To start an integrity check run command "check database" from console.

Each cluster has 2 sub components:
- data file, with extension .pcl
- mapping between physical position of record in the data file and cluster position, with extension .cpm

### File System
To speed up access to the most requested clusters it's recommended to use the cluster files to a SSD or any faster support than disk. To do that, move the files to the mounted partitions and create symbolic links to them on original path. OrientDB will follow symbolic links and will open cluster files everywhere are reachable.

### Cluster pointers
The mapping between data file and physical position is managed with a list. Each entry in this list is a fixed size element, which is the pointer to the physical position of the record in the data file.

Because the data file is paginated, this pointer will consist of 2 items: a page index (long value) and the position of the record inside the page (int value). Each record pointer consumes 12 bytes.

### Creation of new records in cluster
When a new record is inserted, a pointer is added to the list. The index of this pointer is the cluster position. The list is an append only data structure, so if you add a new record its cluster position will be unique and will not be reused.

### Deletion of records in cluster
When you delete a record, the page index and record position are set to -1. So the record pointer is transformed into a "tombstone". You can think of a record id like a **uuid**.. It is unique and never reused.

Usually when you delete records you lose very small amount of disk space. This can be mitigated with a periodic "offline compaction", by performing a database export/import. During this process, cluster positions will be changed (tombstones will be ignored during export) and the lost space will be recovered. So during the import process, the cluster positions can change.

#### Migration of RID
The OrientDB import tool uses a manual hash index (by default the name is '___exportImportRIDMap') to map the old record ids to new record ids.

## Write Ahead (operation) Log (WAL)

The [Write Ahead Log](Write-Ahead-Log.md) (or WAL) is used to restore storage data after a non-soft shutdown:
- Hard kill of the OrientDB process
- Crash/Failure of the Java Virtual Machine that runs OrientDB
- Crash/Failure of the Operating System that is hosting OrientDB

All the operations on **plocal** components are logged in WAL before they are performed. WAL is an append only data structure. You can think of it as a list of records which contain information about operations performed on storage components.

### WAL flush
WAL content is flushed to the disk on these events:
- every 1 second in background thread (flush interval can be changed in **storage.wal.commitTimeout** configuration property)
- synchronously if the amount of RAM used by WAL exceeds 65Mb (can be changed in **storage.wal.cacheSize** configuration property).

As result if OrientDB crashes, all data changes done during <=1 second interval before crash will be lost. This is a trade off between performance and durability.

### Put the WAL on a separate disk
It's strongly recommended that WAL records are stored on a different disk than the disk used to store the DB content. In this way data I/O operations will not be interrupted by WAL I/O operations. This can be done by setting the **storage.wal.path** property to the folder where storage WAL files will be placed.

### How Indexes use WAL?
Indexes can work with WAL in 2 modes:
- **ROLLBACK_ONLY** (default mode) and
- **FULL**

In ROLLBACK_ONLY mode only the data needed to rollback transactions is stored. This means that WAL records can not be used to restore index content after a crash. In the case of a crash, the indexes will be rebuilt automatically.

In FULL mode, indexes can be restored after DB crash without a rebuild. You can change index durability mode by setting the property **index.txMode**.

You can find more details about WAL **[here](Write-Ahead-Log.md)**.

## File types
PLocal stores data on the file system using different files, using the following extensions:

- **.cpm**, contains the mapping between real physical positions and cluster positions. If you delete a record, the tombstone is placed here. Each tombstone consumes about 12 bytes.
- **.pcl**, data file
- **.sbt**, is index file
- **.wal** and **.wmr**, are Journal Write Ahead (operation) Log files
- **.cm**, is the mapping between file id and real file name (used internally)
- **.irs**, is the RID set file for non-unique indexes

## How it works (Internal)
Paginated storage is a 2-level disk cache that works together with the write ahead log.

Every file is spit into pages, and each file operation is atomic at a page level. The 2-level disk cache allows:

1. Cache frequently accessed pages in memory.
2. Automatically separate pages which are rarely accessed from frequently accessed and rid off the first from cache memory.
3. Minimize amount of disk head seeks during data writes.
4. In case of low or middle write data load allows to mitigate pauses are needed to write data to the disk by flushing all changed or newly added pages to the disk in background thread.
5. Works together with WAL to make any set changes on single page look like atomic operation.

2-level cache itself consist of a **Read Cache** (implementation is based on 2Q cache algorithm) and a **Write cache* (implementation is based on WOW cache algorithm).

Typical set of operations are needed to work with any file looks like following:

1. Open file using OReadWriteDiskCache#openFile operation and get id of open file. If the file does not exist it will be automatically created. The id of file is stored in a special meta data file and will always belong to the given file till it will be deleted.
2. Allocate new page OReadWriteDiskCache#allocateNewPage or load existing one ORreadWriteDiskCache#load into off-heap memory.
3. Retrieve pointer to the allocated area of off-heap memory OCacheEntry#getCachePointer().
4. If you plan to change page data acquire a write lock, or a read lock if you read data and your single file page is shared across several data structures. Write lock must be acquired whether a single page is used between several data structures or not. The write lock is needed to prevent flushing inconsistent pages to the disk by the "data flush" thread of the write cache. OCachePointer#acquireExclusiveLock.
5. Update/read data in off heap memory.
6. Release write lock if needed. OCachePointer#releaseExclusiveLock.
7. Mark page as dirty if you changed page data. It will allow write cache to flush pages which are really changed OCacheEntry#markDirty.
8. Push record back to the disk cache: indicate to the cache that you will not use this page any more so it can be safely evicted from the memory to make room to other pages OReadWriteDiskCache#release.

### So what is going on underneath when we load and release pages?

When we load page the Read Cache looks it in one of its two LRU lists. One list is for data that was accessed several times and then not accessed for very long period of time. It consumes 25% of memory. The second is for data that is accessed frequently for a long period of time. It consumes 75% of memory.

If the page is not in either LRU queue, the Read Cache asks the Write Cache to load page from the disk.

If we are lucky and the page is queued to flush but is still in the Write Queue of Write Cache it will be retrieved from there. Otherwise, the Write Cache will load the page from disk.

When data will be read from file by Write Cache, it will be put in LRU queue which contains "short living" pages. Eventually, if this pages will be accessed frequently during long time interval, loaded page will be moved to the LRU of "long living" pages.

When we release a page and the page is marked as dirty, it is put into the Write Cache which adds it to the Write Queue. The Write Queue can be considered as ring buffer where all the pages are sorted by their position on the disk. This trick allows to minimize disk head movements during pages flush. What is more interesting is that pages are always flushed in the background in the "background flush" thread. This approach allows to mitigate I/O bottleneck if we have enough RAM to work in memory only and flush data in background.

So it was about how disk cache works. But how we achieve durability of changes on page level and what is more interesting on the level when we work with complex data structures like Trees or Hash Maps (these data structures are used in indexes).

If we look back on set of operations which we perform to manipulate file data you see that step 5 does not contains any references to OrientDB API. That is because there are two ways to work with off heap pages: durable and not durable.

The simple (not durable way) is to work with methods of direct memory pointer com.orientechnologies.common.directmemory.ODirectMemoryPointer(setLong/getLong, setInt/getInt and so on).
If you would like to make all changes in your data structures durable you should not work with direct memory pointer but should create a component that will present part of your data structure by extending com.orientechnologies.orient.core.storage.impl.local.paginated.ODurablePage class. This class has similar methods for manipulation of data in off heap pages, but it also tracks all changes made to the page. It can return the diff between the old/new states of page using the com.orientechnologies.orient.core.storage.impl.local.paginated.ODurablePage#getPageChanges method. Also this class allows to apply given diff to the old/new snapshot of given pages to repeat/revert (restoreChanges()/revertChanges()) changes are done for this page.


text:
@interface BTAddressProvider : NSObject

+ (instancetype)instance;

#pragma mark - password

- (BOOL)changePasswordWithOldPassword:(NSString *)oldPassword andNewPassword:(NSString *)newPassword;

- (BTPasswordSeed *)getPasswordSeed;

- (BOOL)hasPasswordSeed;

#pragma mark - hdm

- (NSArray *)getHDSeedIds;

- (NSString *)getEncryptMnemonicSeed:(int)hdSeedId;

- (NSString *)getEncryptHDSeed:(int)hdSeedId;

//- (void)updateHDSeedWithHDSeedId:(int)hdSeedId andEncryptHDSeed:(NSString *)encryptHDSeed;
//- (void)updateHDSeedWithHDSeedId:(int)hdSeedId andEncryptSeed:(NSString *)encryptSeed andEncryptHDSeed:(NSString *)encryptHDSeed;
- (BOOL)isHDSeedFromXRandom:(int)hdSeedId;

- (NSString *)getHDMFirstAddress:(int)hdSeedId;

- (NSString *)getSingularModeBackup:(int)hdSeedId;

- (void)setSingularModeBackupWithHDSeedId:(int)hdSeedId andSingularModeBackup:(NSString *)singularModeBackup;

- (int)addHDSeedWithMnemonicEncryptSeed:(NSString *)encryptMnemonicSeed andEncryptHDSeed:(NSString *)encryptHDSeed
                        andFirstAddress:(NSString *)firstAddress andIsXRandom:(BOOL)isXRandom
                         andAddressOfPs:(NSString *)addressOfPs;

- (BTHDMBid *)getHDMBid;

- (BOOL)addHDMBid:(BTHDMBid *)hdmBid andAddressOfPS:(NSString *)addressOfPS;

- (NSArray *)getHDMAddressInUse:(BTHDMKeychain *)keychain;

- (BOOL)prepareHDMAddressesWithHDSeedId:(int)hdSeedId andPubs:(NSArray *)pubs;

- (NSArray *)getUncompletedHDMAddressPubs:(int)hdSeedId andCount:(int)count;

- (int)maxHDMAddressPubIndex:(int)hdSeedId;

//including completed and uncompleted
- (BOOL)recoverHDMAddressesWithHDSeedId:(int)hdSeedId andHDMAddresses:(NSArray *)addresses;

- (BOOL)completeHDMAddressesWithHDSeedId:(int)hdSeedId andHDMAddresses:(NSArray *)addresses;

- (void)setHDMPubsRemoteWithHDSeedId:(int)hdSeedId andIndex:(int)index andPubKeyRemote:(NSData *)pubKeyRemote;

- (int)uncompletedHDMAddressCount:(int)hdSeedId;

- (void)updateSyncCompleteHDSeedId:(int)hdSeedId hdSeedIndex:(uint)hdSeedIndex syncComplete:(BOOL)syncComplete;

#pragma mark - normal

- (NSArray *)getAddresses;

- (BOOL)addAddress:(BTAddress *)address;

- (BOOL)addAddresses:(NSArray *)addresses andPasswordSeed:(BTPasswordSeed *)passwordSeed;

- (NSString *)getEncryptPrivKeyWith:(NSString *)address;

- (void)updatePrivateKey:(BTAddress *)address;

- (void)removeWatchOnlyAddress:(BTAddress *)address;

- (void)trashPrivKeyAddress:(BTAddress *)address;

- (void)restorePrivKeyAddress:(BTAddress *)address;

- (void)updateSyncComplete:(BTAddress *)address;

- (NSString *)getAlias:(NSString *)address;

- (NSDictionary *)getAliases;

- (void)updateAliasWithAddress:(NSString *)address andAlias:(NSString *)alias;

-(int) getVanityLen:(NSString *)address;
-(NSDictionary *)getVanityAddresses;
-(void)updateVanityAddress:(NSString *)address andLen:(int)len;

#pragma mark - hd account

- (int)addHDAccount:(NSString *)encryptedMnemonicSeed encryptSeed:(NSString *)encryptSeed
       firstAddress:(NSString *)firstAddress isXrandom:(BOOL)isXrandom encryptSeedOfPS:(NSString *)encryptSeedOfPs addressOfPS:(NSString *)addressOfPs
        externalPub:(NSData *)externalPub internalPub:(NSData *)internalPub;

- (NSData *)getExternalPub:(int)hdSeedid;

- (NSData *)getInternalPub:(int)hdSeedid;

- (NSString *)getHDAccountEncryptSeed:(int)hdSeedId;

- (NSString *)getHDAccountEncryptMnmonicSeed:(int)hdSeedId;

- (NSArray *)getHDAccountSeeds;

- (NSString *)getHDAccountFristAddress:(int)seedId;

- (BOOL)hdAccountIsXRandom:(int)seedId;

@end

redpajama_set_name: RedPajamaGithub


text:
package com.maximusvladimir.saveablehash.test;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.HashMap;
import java.util.Random;

import com.maximusvladimir.saveablehash.HashIO;
import com.maximusvladimir.saveablehash.IOSerialize;
import com.maximusvladimir.saveablehash.ParseFactory;

public class Demo {
	public static void main(String[] args) {

		class Timestamp {
			public long start;
			public long end;
			public String buf;
			public Timestamp() {

			}

			public void setStart(long s) {
				start = s;
			}

			public long getStart() {
				return start;
			}

			public void setEnd(long e) {
				end = e;
			}

			public long getEnd() {
				return end;
			}

			public String toString() {
				return "start:" + start + " end:" + end + " buffer:" + buf;
			}
		}

		ParseFactory factory = new ParseFactory();
		factory.add(Timestamp.class);
		/*factory.add(Timestamp.class, new IOSerialize<Timestamp>() {
			public <T> T load(ParseFactory factory, String data) {
				Timestamp stamp = new Timestamp();
				String[] parts = data.split(",");
				stamp.setStart(Long.parseLong(parts[0]));
				stamp.setEnd(Long.parseLong(parts[1]));
				return (T)stamp;
			}

			@Override
			public <T> String save(ParseFactory factory, T obj) {
				return ((Timestamp)obj).getStart() + "," + ((Timestamp)obj).getEnd();
			}

		});*/

		HashMap<String, Timestamp> millisecondTimeFrames = new HashMap<String, Timestamp>();
		for (int i = 0; i < 20; i++) {
			Timestamp stamp = new Timestamp();
			long sl = (long)(Math.random() * Long.MAX_VALUE);
			if (Math.random() < 0.5)
				sl = -sl;
			long el = (long)(Math.random() * Long.MAX_VALUE);
			if (Math.random() < 0.5)
				el = -el;
			stamp.setStart(sl);
			stamp.setEnd(el);
			stamp.buf = "jigj" + -new Random(i*200 + (int)(Math.random() * 300)).nextLong();
			millisecondTimeFrames.put("i"+i, stamp);
		}

		try {
			FileWriter fr = new FileWriter(new File("timeframes.txt"));
			BufferedWriter writer = new BufferedWriter(fr);
			HashIO.save(factory, millisecondTimeFrames, writer);
			writer.flush();
		} catch (Throwable t) {

		}

		millisecondTimeFrames.clear();

		try {
			FileReader fr = new FileReader(new File("timeframes.txt"));
			BufferedReader reader = new BufferedReader(fr);
			HashIO.load(factory, millisecondTimeFrames, reader);
		} catch (Throwable t) {

		}

		String[] keys = new String[millisecondTimeFrames.keySet().size()];
		keys = millisecondTimeFrames.keySet().toArray(keys);
		for (int i = 0; i < keys.length; i++) {
			System.out.println(keys[i] + "=" + millisecondTimeFrames.get(keys[i]));
		}
		// Don't forget to close your stuff here. :wink:
	}
}


text:
using System;
using System.Collections.Generic;

namespace Mozu.Api.Contracts.CommerceRuntime.Channels
{
		///
		///	Paged list of channels defined for a tenant.
		///
		public class ChannelCollection
		{
			public List<Channel> Items { get; set; }

			public int PageCount { get; set; }

			public int PageSize { get; set; }

			public int StartIndex { get; set; }

			public int TotalCount { get; set; }

		}

}


text:
/* First created by JCasGen Mon May 11 11:00:52 EDT 2015 */
package org.apache.ctakes.typesystem.type.syntax;

import org.apache.uima.jcas.JCas;
import org.apache.uima.jcas.JCasRegistry;
import org.apache.uima.cas.impl.CASImpl;
import org.apache.uima.cas.impl.FSGenerator;
import org.apache.uima.cas.FeatureStructure;
import org.apache.uima.cas.impl.TypeImpl;
import org.apache.uima.cas.Type;

/** A particle, e.g., "out" in "rule out"
Equivalent to cTAKES: edu.mayo.bmi.uima.chunker.type.PRT
 * Updated by JCasGen Mon May 11 11:00:52 EDT 2015
 * @generated */
public class PRT_Type extends Chunk_Type {
  /** @generated
   * @return the generator for this type
   */
  @Override
  protected FSGenerator getFSGenerator() {return fsGenerator;}
  /** @generated */
  private final FSGenerator fsGenerator =
    new FSGenerator() {
      public FeatureStructure createFS(int addr, CASImpl cas) {
  			 if (PRT_Type.this.useExistingInstance) {
  			   // Return eq fs instance if already created
  		     FeatureStructure fs = PRT_Type.this.jcas.getJfsFromCaddr(addr);
  		     if (null == fs) {
  		       fs = new PRT(addr, PRT_Type.this);
  			   PRT_Type.this.jcas.putJfsFromCaddr(addr, fs);
  			   return fs;
  		     }
  		     return fs;
        } else return new PRT(addr, PRT_Type.this);
  	  }
    };
  /** @generated */
  @SuppressWarnings ("hiding")
  public final static int typeIndexID = PRT.typeIndexID;
  /** @generated
     @modifiable */
  @SuppressWarnings ("hiding")
  public final static boolean featOkTst = JCasRegistry.getFeatOkTst("org.apache.ctakes.typesystem.type.syntax.PRT");

  /** initialize variables to correspond with Cas Type and Features
	 * @generated
	 * @param jcas JCas
	 * @param casType Type
	 */
  public PRT_Type(JCas jcas, Type casType) {
    super(jcas, casType);
    casImpl.getFSClassRegistry().addGeneratorForType((TypeImpl)this.casType, getFSGenerator());

  }
}


text:
<?php

function gc_recording_setup_columns() {
    add_filter ( 'manage_gc_recording_posts_columns', 'gc_recording_column_headers', 10, 2 );
    add_action( 'manage_gc_recording_posts_custom_column', 'gc_recording_fill_columns', 10, 2 );
}

function gc_recording_column_headers( $columns ) {
    $columns[ 'chant_variant' ] = 'Chant Variant';
    $columns[ 'chant' ] = 'Chant';

    return $columns;
}

function gc_recording_fill_columns( $column_name, $post_id ) {
    $chant_variant_id = get_post( $post_id ) -> post_parent;
    $chant_id = get_post( $chant_variant_id ) -> post_parent;

    if ( $column_name === 'chant_variant' ) {
        gc_echo_post_title( $chant_variant_id );
    } else if ( $column_name === 'chant' ) {
        gc_echo_post_title( $chant_id );
    }
}


text:
@rem
@rem Copyright (c) 2007 BUSINESS OBJECTS SOFTWARE LIMITED
@rem All rights reserved.
@rem
@rem Redistribution and use in source and binary forms, with or without
@rem modification, are permitted provided that the following conditions are met:
@rem
@rem     * Redistributions of source code must retain the above copyright notice,
@rem       this list of conditions and the following disclaimer.
@rem
@rem     * Redistributions in binary form must reproduce the above copyright
@rem       notice, this list of conditions and the following disclaimer in the
@rem       documentation and/or other materials provided with the distribution.
@rem
@rem     * Neither the name of Business Objects nor the names of its contributors
@rem       may be used to endorse or promote products derived from this software
@rem       without specific prior written permission.
@rem
@rem THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
@rem AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
@rem IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
@rem ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
@rem LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
@rem CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
@rem SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
@rem INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
@rem CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
@rem ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
@rem POSSIBILITY OF SUCH DAMAGE.
@rem

@rem This script runs the Open Quark Standalone Jar Tool
@rem The arguments to this script are:
@rem [-o] <workspaceFileName> [-verbose] (-main <functionName> <mainClassName> | -lib <moduleName> <libClassScope> <libClassName>)+ <outputJarName> [-src <outputSrcZipName>]
@rem where:
@rem -o is the optional flag for turning on the CAL-based optimizer
@rem <workspaceFileName> is the name of the workspace declaration file (just the name, no paths are accepted).
@rem     This file should be in a "Workspace Declarations" subdirectory of a directory on the classpath.
@rem -verbose is the optional flag to dump out more information during the operation
@rem -main specifies a CAL application to be included with the standalone JAR.
@rem   <functionName> is the fully-qualified name of the CAL function that is the main entry point for the application.
@rem   <mainClassName> is the fully-qualified name of the Java main class to be generated.
@rem -lib specifies a CAL library to be included with the standalone JAR.
@rem   <moduleName> is the fully-qualified name of the CAL library module.
@rem   <libClassScope> is the scope of the generated library class. Can be one of: public, protected, package, or private.
@rem   <libClassName> is the fully-qualified name of the Java main class to be generated.
@rem <outputJarName> is the name of the output JAR file (can specify a path).
@rem -src <outputSrcZipName> (optional) specifies the name of the output zip file containing source files for the generated classes.

@echo off
setlocal

set VMARGS=-Dorg.openquark.cal.machine.lecc.non_interruptible

if "%1" == "-o" (
set VMARGS=%VMARGS% -Dorg.openquark.cal.optimizer_level=1
shift
)

set QUARK_CP=.;%QUARK_CP%
set QUARK_VMARGS=%VMARGS% %QUARK_VMARGS%

@javacp org.openquark.cal.services.StandaloneJarTool %1 %2 %3 %4 %5 %6 %7 %8 %9

@endlocal


text:
package share.app.dictionary;

public class Category
{
	public enum Categories
	{
		FIX_ORDER_STATUS, SUPPORT_STATUS, MEASURING_UNIT, PAYMENT_METHOD, MONTHS, SYSTEM_MODULES, TAX
	}

	public static class FIX_ORDER_STATUS
	{
		public static final String RECEIVED = "R";
	}

	public static class SUPPORT_STATUS
	{
		public static final String PENDING = "P";
	}

	public static class TAX
	{
		public static final String GENERAL = "A";
		public static final String LOW = "B";
		public static final String SUPER_LOW = "C";
	}
}


text:
package api

import (
	"encoding/json"
	"errors"
	"io"
	"net/http"

	log "go-micro.dev/v4/logger"
)

// Errors ...
var (
	ErrNotFound = errors.New("K8s: not found")
	ErrDecode   = errors.New("K8s: error decoding")
	ErrOther    = errors.New("K8s: error")
)

// Status is an object that is returned when a request
// failed or delete succeeded.
// type Status struct {
// 	Kind    string `json:"kind"`
// 	Status  string `json:"status"`
// 	Message string `json:"message"`
// 	Reason  string `json:"reason"`
// 	Code    int    `json:"code"`
// }

// Response ...
type Response struct {
	res *http.Response
	err error

	body []byte
}

// Error returns an error
func (r *Response) Error() error {
	return r.err
}

// StatusCode returns status code for response
func (r *Response) StatusCode() int {
	return r.res.StatusCode
}

// Into decode body into `data`
func (r *Response) Into(data interface{}) error {
	if r.err != nil {
		return r.err
	}

	defer r.res.Body.Close()
	decoder := json.NewDecoder(r.res.Body)
	err := decoder.Decode(&data)
	if err != nil {
		return ErrDecode
	}

	return r.err
}

func newResponse(res *http.Response, err error) *Response {
	r := &Response{
		res: res,
		err: err,
	}

	if err != nil {
		return r
	}

	if r.res.StatusCode == http.StatusOK ||
		r.res.StatusCode == http.StatusCreated ||
		r.res.StatusCode == http.StatusNoContent {
		// Non error status code
		return r
	}

	if r.res.StatusCode == http.StatusNotFound {
		r.err = ErrNotFound
		return r
	}

	log.Errorf("K8s: request failed with code %v", r.res.StatusCode)

	b, err := io.ReadAll(r.res.Body)
	if err == nil {
		log.Error("K8s: request failed with body:")
		log.Error(string(b))
	}
	r.err = ErrOther
	return r
}


text:
<?php

namespace BlogBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;

class SignalementController extends Controller
{
    public function listAction()
    {
        return $this->render('BlogBundle:Signalement:list.html.twig', array(
            // ...
        ));
    }

    public function addAction()
    {
        return $this->render('BlogBundle:Signalement:add.html.twig', array(
            // ...
        ));
    }

}


text:
package com.bc.zarr.storage;

import com.bc.zarr.ZarrConstants;
import com.bc.zarr.ZarrUtils;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;
import java.nio.file.FileSystem;
import java.nio.file.FileSystemAlreadyExistsException;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.TreeSet;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ZipStore implements Store {
    private final FileSystem zfs;
    private final Path internalRoot;

    public ZipStore(Path zipFilePath) throws IOException {
        final HashMap<String, String> zipParams = new HashMap<>();
        if (!Files.exists(zipFilePath)) {
            zipParams.put("create", "true");
        }
        final URI uri = URI.create("jar:file:" + zipFilePath.toUri().getPath());
        zfs = getFileSystem(zipParams, uri);
        internalRoot = zfs.getRootDirectories().iterator().next();
    }

    @Override
    public InputStream getInputStream(String key) throws IOException {
        final Path path = internalRoot.resolve(key);
        if (Files.isReadable(path)) {
            byte[] bytes = Files.readAllBytes(path);
            return new ByteArrayInputStream(bytes);
        }
        return null;
    }

    @Override
    public OutputStream getOutputStream(String key) {
        return new ByteArrayOutputStream() {
            private boolean closed = false;

            @Override
            public void close() throws IOException {
                try {
                    if (!closed) {
                        final byte[] bytes = this.toByteArray();
                        final Path filePath = internalRoot.resolve(key);
                        if (Files.exists(filePath)) {
                            Files.delete(filePath);
                        } else {
                            Files.createDirectories(filePath.getParent());
                        }
                        Files.write(filePath, bytes);
                    }
                } finally {
                    closed = true;
                }
            }
        };
    }

    @Override
    public void delete(String key) throws IOException {
        final Path toBeDeleted = internalRoot.resolve(key);
        if (Files.isDirectory(toBeDeleted)) {
            ZarrUtils.deleteDirectoryTreeRecursively(toBeDeleted);
        }
        if (Files.exists(toBeDeleted)) {
            Files.delete(toBeDeleted);
        }
        if (Files.exists(toBeDeleted) || Files.isDirectory(toBeDeleted)) {
            throw new IOException("Unable to initialize " + toBeDeleted.toAbsolutePath());
        }
    }

    @Override
    public TreeSet<String> getArrayKeys() throws IOException {
        return getParentsOf(ZarrConstants.FILENAME_DOT_ZARRAY);
    }

    @Override
    public TreeSet<String> getGroupKeys() throws IOException {
        return getParentsOf(ZarrConstants.FILENAME_DOT_ZGROUP);
    }

    private TreeSet<String> getParentsOf(String suffix) throws IOException {
        return getKeysEndingWith(suffix).stream()
                .map(s -> internalRoot.relativize(internalRoot.resolve(s).getParent()).toString())
                .collect(Collectors.toCollection(TreeSet::new));
    }

    @Override
    public TreeSet<String> getKeysEndingWith(String suffix) throws IOException {
        return Files.walk(internalRoot)
                .filter(path -> path.toString().endsWith(suffix))
                .map(path -> internalRoot.relativize(path).toString())
                .collect(Collectors.toCollection(TreeSet::new));
    }

    @Override
    public Stream<String> getRelativeLeafKeys(String key) throws IOException {
        final Path walkingRoot = internalRoot.resolve(key);
        return Files.walk(walkingRoot)
                .filter(path -> !Files.isDirectory(path))
                .map(path -> walkingRoot.relativize(path).toString())
                .map(ZarrUtils::normalizeStoragePath)
                .filter(s -> s.trim().length() > 0);
    }

    @Override
    public void close() throws IOException {
        zfs.close();
    }

    // Gets or creates a FileSystem
    private FileSystem getFileSystem(HashMap<String, String> zipParams, URI uri) throws IOException {
        try {
            return FileSystems.newFileSystem(uri, zipParams);
        } catch (FileSystemAlreadyExistsException e) {
            return FileSystems.getFileSystem(uri);
        }
    }
}


text:
'use strict'; // eslint-disable-line strict

const { validateUserPolicy, validateResourcePolicy } =
    require('../../../lib/policy/policyValidator');
const { default: errors, ArsenalError } = require('../../../lib/errors');
const successRes = { error: null, valid: true };
const sampleUserPolicy = {
    Version: '2012-10-17',
    Statement: {
        Sid: 'FooBar1234',
        Effect: 'Allow',
        Action: 's3:PutObject',
        Resource: 'arn:aws:s3:::my_bucket/uploads/widgetco/*',
        Condition: { NumericLessThanEquals: { 's3:max-keys': '10' } },
    },
};
const sampleResourcePolicy = {
    Version: '2012-10-17',
    Statement: [
        {
            Sid: 'ResourcePolicy1',
            Effect: 'Allow',
            Action: 's3:ListBucket',
            Resource: 'arn:aws:s3:::example-bucket',
            Condition: { StringLike: { 's3:prefix': 'foo' } },
            Principal: '*',
        },
    ],
};

const errDict = {
    required: {
        Version: 'Policy document must be version 2012-10-17 or greater.',
        Action: 'Policy statement must contain actions.',
    },
    pattern: {
        Action: 'Actions/Conditions must be prefaced by a vendor,' +
            ' e.g., iam, sdb, ec2, etc.',
        Resource: 'Resource must be in ARN format or "*".',
    },
    minItems: {
        Resource: 'Policy statement must contain resources.',
    },
};

function failRes(policyType, errDescription) {
    let error;
    if (policyType === 'user') {
        error = errors.MalformedPolicyDocument;
    }
    if (policyType === 'resource') {
        error = errors.MalformedPolicy;
    }
    if (errDescription || error.description) {
        error = error.customizeDescription(errDescription || error.description);
    }
    return { error, valid: false };
}

function check(input, expected, policyType) {
    let result;
    if (policyType === 'user') {
        result = validateUserPolicy(JSON.stringify(input));
    }
    if (policyType === 'resource') {
        result = validateResourcePolicy(JSON.stringify(input));
    }
    expect(result.valid).toEqual(expected.valid);
    if (!result.valid) {
        expect(result.error.type).toEqual(expected.error.type);
        expect(result.error.description).toEqual(expected.error.description);
        expect(result.error).toBeInstanceOf(ArsenalError);
    }
}

let userPolicy;
let resourcePolicy;
const user = 'user';
const resource = 'resource';

beforeEach(() => {
    userPolicy = JSON.parse(JSON.stringify(sampleUserPolicy));
    resourcePolicy = JSON.parse(JSON.stringify(sampleResourcePolicy));
});

describe('Policies validation - Invalid JSON', () => {
    it('should return error for invalid user policy JSON', () => {
        const result = validateUserPolicy('{"Version":"2012-10-17",' +
        '"Statement":{"Effect":"Allow""Action":"s3:PutObject",' +
        '"Resource":"arn:aws:s3*"}}');
        const fail = failRes(user);
        expect(result.valid).toBeFalsy();
        expect(result.error.type).toEqual(fail.error.type);
        expect(result.error.description).toEqual(fail.error.description);
        expect(result.error).toBeInstanceOf(ArsenalError);
    });
    it('should return error for invaild resource policy JSON', () => {
        const result = validateResourcePolicy('{"Version":"2012-10-17",' +
        '"Statement":{"Effect":"Allow""Action":"s3:PutObject",' +
        '"Resource":"arn:aws:s3*"}}');
        const fail = failRes(resource);
        expect(result.valid).toBeFalsy();
        expect(result.error.type).toEqual(fail.error.type);
        expect(result.error.description).toEqual(fail.error.description);
        expect(result.error).toBeInstanceOf(ArsenalError);
    });
});

describe('Policies validation - Version', () => {
    it('should validate user policy with version date 2012-10-17', () => {
        check(userPolicy, successRes, user);
    });

    it('should validate resource policy with version date 2012-10-17', () => {
        check(resourcePolicy, successRes, 'resource');
    });

    it('user policy should return error for other dates', () => {
        userPolicy.Version = '2012-11-17';
        check(userPolicy, failRes(user), user);
    });

    it('resource policy should return error for other dates', () => {
        resourcePolicy.Version = '2012-11-17';
        check(resourcePolicy, failRes(resource), resource);
    });

    it('should return error if Version field in user policy is missing', () => {
        userPolicy.Version = undefined;
        check(userPolicy, failRes(user, errDict.required.Version), user);
    });

    it('should return error if Version field in resource policy is missing',
        () => {
            resourcePolicy.Version = undefined;
            check(resourcePolicy, failRes(resource, errDict.required.Version),
                resource);
        });
});

describe('Policies validation - Principal', () => {
    [
        {
            name: 'an account id',
            value: { AWS: '111111111111' },
            policyType: [user, resource],
        },
        {
            name: 'anonymous user AWS form',
            value: { AWS: '*' },
            policyType: [user, resource],
        },
        {
            name: 'an account arn',
            value: { AWS: 'arn:aws:iam::111111111111:root' },
            policyType: [user, resource],
        },
        {
            name: 'multiple account id',
            value: {
                AWS: ['111111111111', '111111111112'],
            },
            policyType: [user, resource],
        },
        {
            name: 'multiple account arn',
            value: {
                AWS: [
                    'arn:aws:iam::111111111111:root',
                    'arn:aws:iam::111111111112:root',
                ],
            },
            policyType: [user, resource],
        },
        {
            name: 'anonymous user as string',
            value: '*',
            policyType: [user, resource],
        },
        {
            name: 'user arn',
            value: { AWS: 'arn:aws:iam::111111111111:user/alex' },
            policyType: [user, resource],
        },
        {
            name: 'user arn with path',
            value: { AWS: 'arn:aws:iam::111111111111:user/path/in/org/leaf' },
            policyType: [user, resource],
        },
        {
            name: 'multiple user arns',
            value: {
                AWS: [
                    'arn:aws:iam::111111111111:user/alex',
                    'arn:aws:iam::111111111111:user/thibault',
                ],
            },
            policyType: [user, resource],
        },
        {
            name: 'role arn',
            value: {
                AWS: 'arn:aws:iam::111111111111:role/dev',
            },
            policyType: [user, resource],
        },
        {
            name: 'multiple role arn',
            value: {
                AWS: [
                    'arn:aws:iam::111111111111:role/dev',
                    'arn:aws:iam::111111111111:role/prod',
                ],
            },
            policyType: [user, resource],
        },
        {
            name: 'saml provider',
            value: {
                Federated:
                    'arn:aws:iam::111111111111:saml-provider/mysamlprovider',
            },
            policyType: [user],
        },
        {
            name: 'with backbeat service',
            value: { Service: 'backbeat' },
            policyType: [user, resource],
        },
        {
            name: 'with canonical user id',
            value: { CanonicalUser:
                '1examplecanonicalid12345678909876' +
                '54321qwerty12345asdfg67890z1x2c' },
            policyType: [resource],
        },
    ].forEach(test => {
        if (test.policyType.includes(user)) {
            it(`should allow user policy principal field with ${test.name}`,
                () => {
                    userPolicy.Statement.Principal = test.value;
                    delete userPolicy.Statement.Resource;
                    check(userPolicy, successRes, user);
                });

            it(`should allow user policy notPrincipal field with ${test.name}`,
                () => {
                    userPolicy.Statement.NotPrincipal = test.value;
                    delete userPolicy.Statement.Resource;
                    check(userPolicy, successRes, user);
                });
        }
        if (test.policyType.includes(resource)) {
            it(`should allow resource policy principal field with ${test.name}`,
                () => {
                    resourcePolicy.Statement[0].Principal = test.value;
                    check(resourcePolicy, successRes, resource);
                });
        }
    });

    [
        {
            name: 'wrong format account id',
            value: { AWS: '11111111111z' },
            policyType: [user, resource],
        },
        {
            name: 'empty string',
            value: '',
            policyType: [user, resource],
        },
        {
            name: 'anonymous user federated form',
            value: { federated: '*' },
            policyType: [user, resource],
        },
        {
            name: 'wildcard in resource',
            value: { AWS: 'arn:aws:iam::111111111111:user/*' },
            policyType: [user, resource],
        },
        {
            name: 'a malformed account arn',
            value: { AWS: 'arn:aws:iam::111111111111:' },
            policyType: [user, resource],
        },
        {
            name: 'multiple malformed account id',
            value: {
                AWS: ['1111111111z1', '1111z1111112'],
            },
            policyType: [user, resource],
        },
        {
            name: 'multiple anonymous',
            value: {
                AWS: ['*', '*'],
            },
            policyType: [user, resource],
        },
        {
            name: 'multiple malformed account arn',
            value: {
                AWS: [
                    'arn:aws:iam::111111111111:root',
                    'arn:aws:iam::111111111112:',
                ],
            },
            policyType: [user, resource],
        },
        {
            name: 'account id as a string',
            value: '111111111111',
            policyType: [user, resource],
        },
        {
            name: 'account arn as a string',
            value: 'arn:aws:iam::111111111111:root',
            policyType: [user, resource],
        },
        {
            name: 'user arn as a string',
            value: 'arn:aws:iam::111111111111:user/alex',
            policyType: [user, resource],
        },
        {
            name: 'multiple malformed user arns',
            value: {
                AWS: [
                    'arn:aws:iam::111111111111:user/alex',
                    'arn:aws:iam::111111111111:user/',
                ],
            },
            policyType: [user, resource],
        },
        {
            name: 'malformed role arn',
            value: {
                AWS: 'arn:aws:iam::111111111111:role/',
            },
            policyType: [user, resource],
        },
        {
            name: 'multiple malformed role arn',
            value: {
                AWS: [
                    'arn:aws:iam::111111111111:role/dev',
                    'arn:aws:iam::11111111z111:role/prod',
                ],
            },
            policyType: [user, resource],
        },
        {
            name: 'saml provider as a string',
            value: 'arn:aws:iam::111111111111:saml-provider/mysamlprovider',
            policyType: [user],
        },
        {
            name: 'with other service than backbeat',
            value: { Service: 'non-existent-service' },
            policyType: [user, resource],
        },
        {
            name: 'invalid canonical user',
            value: { CanonicalUser:
                '12345invalid-canonical-id$$$//098' +
                '7654321poiu1q2w3e4r5t6y7u8i9o0p' },
            policyType: [resource],
        },
    ].forEach(test => {
        if (test.policyType.includes(user)) {
            it(`user policy should fail with ${test.name}`, () => {
                userPolicy.Statement.Principal = test.value;
                delete userPolicy.Statement.Resource;
                check(userPolicy, failRes(user), user);
            });
        }
        if (test.policyType.includes(resource)) {
            it(`resource policy should fail with ${test.name}`, () => {
                resourcePolicy.Statement[0].Principal = test.value;
                check(resourcePolicy, failRes(resource), resource);
            });
        }
    });

    it('should not allow Resource field', () => {
        userPolicy.Statement.Principal = '*';
        check(userPolicy, failRes(user), user);
    });
});

describe('Policies validation - Statement', () => {
    [
        {
            name: 'should return error for undefined',
            value: undefined,
        },
        {
            name: 'should return an error for an empty list',
            value: [],
        },
        {
            name: 'should return an error for an empty object',
            value: {},
            errMessage: errDict.required.Action,
        },
    ].forEach(test => {
        it(`user policy ${test.name}`, () => {
            userPolicy.Statement = test.value;
            check(userPolicy, failRes(user, test.errMessage), user);
        });

        it(`resource policy ${test.name}`, () => {
            resourcePolicy.Statement = test.value;
            check(resourcePolicy, failRes(resource, test.errMessage), resource);
        });
    });

    it('user policy should succeed for a valid object', () => {
        check(userPolicy, successRes, user);
    });

    it('resource policy should succeed for a valid object', () => {
        check(resourcePolicy, successRes, resource);
    });

    it('user policy should succeed for a valid object', () => {
        userPolicy.Statement = [
            {
                Effect: 'Allow',
                Action: 's3:PutObject',
                Resource: 'arn:aws:s3:::my_bucket/uploads/widgetco/*',
            },
            {
                Effect: 'Deny',
                Action: 's3:DeleteObject',
                Resource: 'arn:aws:s3:::my_bucket/uploads/widgetco/*',
            },
        ];
        check(userPolicy, successRes, user);
    });

    it('resource policy should succeed for a valid object', () => {
        resourcePolicy.Statement = [
            {
                Effect: 'Allow',
                Action: 's3:PutObject',
                Resource: 'arn:aws:s3:::my_bucket/uploads/widgetco/*',
                Principal: '*',
            },
            {
                Effect: 'Deny',
                Action: 's3:DeleteObject',
                Resource: 'arn:aws:s3:::my_bucket/uploads/widgetco/*',
                Principal: '*',
            },
        ];
        check(resourcePolicy, successRes, resource);
    });

    [
        {
            name: 'should return error for missing a required field - Action',
            toDelete: ['Action'],
            expected: 'fail',
            errMessage: errDict.required.Action,
        },
        {
            name: 'should return error for missing a required field - Effect',
            toDelete: ['Effect'],
            expected: 'fail',
        },
        {
            name: 'should return error for missing required field - Resource',
            toDelete: ['Resource'],
            expected: 'fail',
        },
        {
            name: 'should return error for missing multiple required fields',
            toDelete: ['Effect', 'Resource'],
            expected: 'fail',
        },
        {
            name: 'should succeed w optional fields missing - Sid, Condition',
            toDelete: ['Sid', 'Condition'],
            expected: successRes,
        },
    ].forEach(test => {
        it(`user policy ${test.name}`, () => {
            test.toDelete.forEach(p => delete userPolicy.Statement[p]);
            if (test.expected === 'fail') {
                check(userPolicy, failRes(user, test.errMessage), user);
            } else {
                check(userPolicy, test.expected, user);
            }
        });

        it(`resource policy ${test.name}`, () => {
            test.toDelete.forEach(p => delete resourcePolicy.Statement[0][p]);
            if (test.expected === 'fail') {
                check(resourcePolicy, failRes(resource, test.errMessage),
                    resource);
            } else {
                check(resourcePolicy, test.expected, resource);
            }
        });
    });
});

describe('Policies validation - Statement::Sid_block', () => {
    it('user policy should succeed if Sid is any alphanumeric string', () => {
        check(userPolicy, successRes, user);
    });

    it('resource policy should succeed if Sid is any alphanumeric string',
        () => {
            check(resourcePolicy, successRes, resource);
        });

    it('user policy should fail if Sid is not a valid format', () => {
        userPolicy.Statement.Sid = 'foo bar()';
        check(userPolicy, failRes(user), user);
    });

    it('resource policy should fail if Sid is not a valid format', () => {
        resourcePolicy.Statement[0].Sid = 'foo bar()';
        check(resourcePolicy, failRes(resource), resource);
    });

    it('user policy should fail if Sid is not a string', () => {
        userPolicy.Statement.Sid = 1234;
        check(userPolicy, failRes(user), user);
    });

    it('resource policy should fail if Sid is not a string', () => {
        resourcePolicy.Statement[0].Sid = 1234;
        check(resourcePolicy, failRes(resource), resource);
    });
});

describe('Policies validation - Statement::Effect_block', () => {
    it('user policy should succeed for Allow', () => {
        check(userPolicy, successRes, user);
    });

    it('resource policy should succeed for Allow', () => {
        check(resourcePolicy, successRes, resource);
    });

    it('user policy should succeed for Deny', () => {
        userPolicy.Statement.Effect = 'Deny';
        check(userPolicy, successRes, user);
    });

    it('resource policy should succeed for Deny', () => {
        resourcePolicy.Statement[0].Effect = 'Deny';
        check(resourcePolicy, successRes, resource);
    });

    it('user policy should fail for strings other than Allow/Deny', () => {
        userPolicy.Statement.Effect = 'Reject';
        check(userPolicy, failRes(user), user);
    });

    it('resource policy should fail for strings other than Allow/Deny', () => {
        resourcePolicy.Statement[0].Effect = 'Reject';
        check(resourcePolicy, failRes(resource), resource);
    });

    it('user policy should fail if Effect is not a string', () => {
        userPolicy.Statement.Effect = 1;
        check(userPolicy, failRes(user), user);
    });

    it('resource policy should fail if Effect is not a string', () => {
        resourcePolicy.Statement[0].Effect = 1;
        check(resourcePolicy, failRes(resource), resource);
    });
});

const actionTests = [
    {
        name: 'should succeed for foo:bar',
        value: 'foo:bar',
        expected: successRes,
    },
    {
        name: 'should succeed for foo:*',
        value: 'foo:*',
        expected: successRes,
    },
    {
        name: 'should succeed for *',
        value: '*',
        expected: successRes,
    },
    {
        name: 'should fail for **',
        value: '**',
        expected: 'fail',
        errMessage: errDict.pattern.Action,
    },
    {
        name: 'should fail for foobar',
        value: 'foobar',
        expected: 'fail',
        errMessage: errDict.pattern.Action,
    },
];

describe('User policies validation - Statement::Action_block/' +
    'Statement::NotAction_block', () => {
    beforeEach(() => {
        userPolicy.Statement.Action = undefined;
        userPolicy.Statement.NotAction = undefined;
    });

    actionTests.forEach(test => {
        it(`${test.name}`, () => {
            userPolicy.Statement.Action = test.value;
            if (test.expected === 'fail') {
                check(userPolicy, failRes(user, test.errMessage), user);
            } else {
                check(userPolicy, test.expected, user);
            }

            userPolicy.Statement.Action = undefined;
            userPolicy.Statement.NotAction = test.value;
            if (test.expected === 'fail') {
                check(userPolicy, failRes(user, test.errMessage), user);
            } else {
                check(userPolicy, test.expected, user);
            }
        });
    });
});

describe('Resource policies validation - Statement::Action_block', () => {
    actionTests.forEach(test => {
        it(`${test.name}`, () => {
            resourcePolicy.Statement[0].Action = test.value;
            if (test.expected === 'fail') {
                check(resourcePolicy, failRes(resource, test.errMessage),
                    resource);
            } else {
                check(resourcePolicy, test.expected, resource);
            }
        });
    });
});

const resourceTests = [
    {
        name: 'should succeed for arn:aws::s3:::*',
        value: 'arn:aws:s3:::*',
        expected: successRes,
    },
    {
        name: 'should succeed for arn:aws:s3:::test/home/${aws:username}',
        value: 'arn:aws:s3:::test/home/${aws:username}',
        expected: successRes,
    },
    {
        name: 'should succeed for arn:aws:ec2:us-west-1:1234567890:vol/*',
        value: 'arn:aws:ec2:us-west-1:1234567890:vol/*',
        expected: successRes,
    },
    {
        name: 'should succeed for *',
        value: '*',
        expected: successRes,
    },
    {
        name: 'should fail for arn:aws:ec2:us-west-1:vol/* - missing region',
        value: 'arn:aws:ec2:us-west-1:vol/*',
        expected: 'fail',
        errMessage: errDict.pattern.Resource,
    },
    {
        name: 'should fail for arn:aws:ec2:us-west-1:123456789:v/${} - ${}',
        value: 'arn:aws:ec2:us-west-1:123456789:v/${}',
        expected: 'fail',
        errMessage: errDict.pattern.Resource,
    },
    {
        name: 'should fail for ec2:us-west-1:qwerty:vol/* - missing arn:aws:',
        value: 'ec2:us-west-1:123456789012:vol/*',
        expected: 'fail',
        errMessage: errDict.pattern.Resource,
    },
];

describe('User policies validation - Statement::Resource_block' +
    'Statement::NotResource_block', () => {
    beforeEach(() => {
        userPolicy.Statement.Resource = undefined;
        userPolicy.Statement.NotResource = undefined;
    });

    resourceTests.forEach(test => {
        it(`${test.name}`, () => {
            userPolicy.Statement.Resource = test.value;
            if (test.expected === 'fail') {
                check(userPolicy, failRes(user, test.errMessage), user);
            } else {
                check(userPolicy, test.expected, user);
            }

            userPolicy.Statement.Resource = undefined;
            userPolicy.Statement.NotResource = test.value;
            if (test.expected === 'fail') {
                check(userPolicy, failRes(user, test.errMessage), user);
            } else {
                check(userPolicy, test.expected, user);
            }
        });
    });

    it('should fail for empty list of resources', () => {
        userPolicy.Statement.Resource = [];
        check(userPolicy, failRes(user, errDict.minItems.Resource), user);
    });
});

describe('Resource policies validation - Statement::Resource_block', () => {
    resourceTests.forEach(test => {
        it(`${test.name}`, () => {
            resourcePolicy.Statement[0].Resource = test.value;
            if (test.expected === 'fail') {
                check(resourcePolicy, failRes(resource, test.errMessage),
                    resource);
            } else {
                check(resourcePolicy, test.expected, resource);
            }
        });
    });

    it('should fail for empty list of resources', () => {
        resourcePolicy.Statement[0].Resource = [];
        check(resourcePolicy, failRes(resource, errDict.minItems.Resource),
            resource);
    });
});

describe('Policies validation - Statement::Condition_block', () => {
    it('user policy should succeed for single Condition', () => {
        check(userPolicy, successRes, user);
    });

    it('resource policy should succeed for single Condition', () => {
        check(resourcePolicy, successRes, resource);
    });

    [
        {
            name: 'should succeed for multiple Conditions',
            value: {
                StringNotLike: { 's3:prefix': ['Development/*'] },
                Null: { 's3:prefix': false },
            },
            expected: successRes,
        },
        {
            name: 'should fail when Condition is not an Object',
            value: 'NumericLessThanEquals',
            expected: 'fail',
        },
        {
            name: 'should fail for an invalid Condition',
            value: {
                SomethingLike: { 's3:prefix': ['Development/*'] },
            },
            expected: 'fail',
        },
        {
            name: 'should fail when one of the multiple conditions is invalid',
            value: {
                Null: { 's3:prefix': false },
                SomethingLike: { 's3:prefix': ['Development/*'] },
            },
            expected: 'fail',
        },
        {
            name: 'should fail when invalid property is assigned',
            value: {
                SomethingLike: { 's3:prefix': ['Development/*'] },
            },
            expected: 'fail',
        },
    ].forEach(test => {
        it(`user policy ${test.name}`, () => {
            userPolicy.Statement.Condition = test.value;
            if (test.expected === 'fail') {
                check(userPolicy, failRes(user), user);
            } else {
                check(userPolicy, test.expected, user);
            }
        });

        it(`resource policy ${test.name}`, () => {
            resourcePolicy.Statement[0].Condition = test.value;
            if (test.expected === 'fail') {
                check(resourcePolicy, failRes(resource), resource);
            } else {
                check(resourcePolicy, test.expected, resource);
            }
        });
    });
});


text:
import React from 'react';
import PropTypes from 'prop-types';
import autobind from 'core-decorators/es/autobind';
import { Button, NonIdealState } from '@blueprintjs/core';
import { graphql } from 'react-apollo';
import { getStories } from '../../../../graphql/story';
import LoadingSpinner from '../../../components/LoadingSpinner';
import StoryList from './StoryList';
import CreateUserStory from './CreateUserStory';

const GetStoriesParams = '_id type createdBy published publishDate iconName intent content likes';

const GetStoriesOptions = {
	name: 'QueryGetStories',
	options: {
		fetchPolicy: 'cache-and-network',
		variables: { skip: 0, limit: 50 }
	}
};

@graphql(getStories(GetStoriesParams), GetStoriesOptions)
@autobind
class Feed extends React.Component {
	state = {
		loading: false,
		showHelp: false
	}

	toggleShowHelp() {
		this.setState((prevState) => {
			return { showHelp: !prevState.showHelp };
		});
	}

	componentDidMount() {
		this.refresh();
	}

	refresh() {
		this.props.QueryGetStories.refetch();
	}

	render() {
		const { getStories, loading } = this.props.QueryGetStories;
		let content;

		if (getStories) {
			if (getStories.length) {
				content = <StoryList stories={getStories} refetch={this.props.QueryGetStories.refetch}/>;
			}
			else {
				content = (

						<NonIdealState title='No stories' description={`I guess there's nothing much happening yet.`} visual='feed'/>

				);
			}
		}
		else if (loading) {
			content = <LoadingSpinner/>;
		}

		return (

						Newsfeed
						<Button className='helper-button pt-small pt-minimal pt-intent-warning' iconName='refresh' onClick={this.refresh} loading={this.props.QueryGetStories.loading} style={{padding:'0'}}/>
						<Button className='helper-button pt-small pt-minimal pt-intent-primary' iconName='help' onClick={this.toggleShowHelp}/>

					{ this.state.showHelp ?

							See regular updates on what the other teams have been up to...
							make sure you keep up or you don't stand a chance!

							Or, you could say a few words and send the other teams some love with a word of encourgement.
							Double tap to like!

						: null
					}
					<CreateUserStory refetch={this.props.QueryGetStories.refetch}/>
					{content}

		);
	}
}

export default Feed;


text:
System.register(['angular2/core', './cms/product-list', 'angular2/router', './service/account-service'], function(exports_1) {
    var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
        var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
        if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
        else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
        return c > 3 && r && Object.defineProperty(target, key, r), r;
    };
    var __metadata = (this && this.__metadata) || function (k, v) {
        if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
    };
    var core_1, product_list_1, router_1, account_service_1;
    var CmsLogin;
    return {
        setters:[
            function (core_1_1) {
                core_1 = core_1_1;
            },
            function (product_list_1_1) {
                product_list_1 = product_list_1_1;
            },
            function (router_1_1) {
                router_1 = router_1_1;
            },
            function (account_service_1_1) {
                account_service_1 = account_service_1_1;
            }],
        execute: function() {
            CmsLogin = (function () {
                function CmsLogin(_accountService, router) {
                    this._accountService = _accountService;
                    this.router = router;
                    this.username = "";
                    this.password = "";
                }
                CmsLogin.prototype.login = function () {
                    var _this = this;
                    console.log("login clicked.");
                    this._accountService.login(this.username, this.password).subscribe(function (result) {
                        console.log("login result: " + result);
                        _this._accountService.token = result;
                        _this.router.navigate(["CmsProduct"]);
                    }, function (err) { alert("Invalid login."); });
                };
                CmsLogin = __decorate([
                    core_1.Component({
                        selector: 'cms-product',
                        template: "\n        <form (ngSubmit)=\"login()\">\n             <div>\n                <label for=\"username\">Username: </label><input id=\"username\" [(ngModel)]=\"username\" type=\"text\" required>\n             </div>\n\n            <div>\n                <label for=\"password\">Password: </label><input id=\"password\" [(ngModel)]=\"password\" type=\"text\" required>\n            </div>\n            <button>Login</button>\n        </form>\n    ",
                        directives: [product_list_1.ProductList, router_1.RouterLink, router_1.ROUTER_DIRECTIVES]
                    }),
                    __metadata('design:paramtypes', [account_service_1.AccountService, router_1.Router])
                ], CmsLogin);
                return CmsLogin;
            })();
            exports_1("CmsLogin", CmsLogin);
        }
    }
});
//# sourceMappingURL=cms.login.component.js.map


text:
@implementation OpenAction {
	NSString *path;
}

- (id)init {
	self = [super init];
    if (!self) {
		return nil;
    }

	path = @"";
	return self;
}

- (id)initWithDictionary:(NSDictionary *)dict {
	self = [super initWithDictionary:dict];
    if (!self) {
		return nil;
    }

	path = [dict[@"parameter"] copy];
	return self;
}

- (id)initWithFile:(NSString *)file {
	self = [super init];
    if (!self) {
		return nil;
    }

	path = [file copy];
	return self;
}

- (NSMutableDictionary *)dictionary {
	NSMutableDictionary *dict = [super dictionary];
	dict[@"parameter"] = [path copy];
	return dict;
}

- (NSString *)description {
	return [NSString stringWithFormat:NSLocalizedString(@"Opening '%@'.", @""), path];
}

- (NSWorkspaceLaunchOptions)launchOptions {
    return NSWorkspaceLaunchDefault;
}

- (BOOL)execute:(NSString **)errorString {
	NSString *app, *fileType;

	if (![[NSWorkspace sharedWorkspace] getInfoForFile:path application:&app type:&fileType]) {
		*errorString = [NSString stringWithFormat:NSLocalizedString(@"Failed opening '%@'.", @""), path];
        return NO;
	}

	if ([[fileType uppercaseString] isEqualToString:@"SCPT"]) {
		NSArray *args = [NSArray arrayWithObject:path];
		NSTask *task = [NSTask launchedTaskWithLaunchPath:@"/usr/bin/osascript" arguments:args];
		[task waitUntilExit];
		if ([task terminationStatus] == 0) {
            return YES;
        }

	} else {
        NSBundle *requestedAppBundle = [NSBundle bundleWithPath:path];

        // if the requestedAppBundle comes back nil then
        // they are either specifying that an actual file (not an app) be opened
        if (requestedAppBundle != nil) {
            NSString *bundleId = [requestedAppBundle bundleIdentifier];
            @try {
                // Do not "open" an app that is already running
                if (bundleId && [[NSRunningApplication runningApplicationsWithBundleIdentifier:bundleId] count]) {
                    DSLog(@"%@ is already running", bundleId);
                    return YES;
                }
            }
            @catch (NSException *e) {
                DSLog(@"failed to get the bundleidentifier for %@", bundleId);
            }
        }

        // whether it is a file or an app, it needs to get opened here
        NSArray *urls = [NSArray arrayWithObject:[NSURL fileURLWithPath:path]];
        if ([[NSWorkspace sharedWorkspace] openURLs:urls withAppBundleIdentifier:nil options:[self launchOptions] additionalEventParamDescriptor:nil launchIdentifiers:nil]) {
            return YES;
        }
	}

	*errorString = [NSString stringWithFormat:NSLocalizedString(@"Failed opening '%@'.", @""), path];
	return NO;
}

+ (NSString *)helpText {
	return NSLocalizedString(@"The parameter for Open actions is the full path of the"
                             " object to be opened, such as an application or a document.", @"");
}

+ (NSString *)friendlyName {
    return NSLocalizedString(@"Open File or Application", @"");
}

+ (NSString *)menuCategory {
    return NSLocalizedString(@"Application", @"");
}

@end


text:
package syscall

const OS = "nacl"

// Auto-generated

//sys	Chmod(path string, mode int) (errno int)
//sys	Clock() (clock int)
//sys	Close(fd int) (errno int)
//sys	Exit(code int)
//sys	Fstat(fd int, stat *Stat_t) (errno int)
//sys	Getdents(fd int, buf []byte) (n int, errno int)
//sys	Getpid() (pid int)
//sys	Gettimeofday(tv *Timeval) (errno int)
//sys	Open(path string, mode int, perm int) (fd int, errno int)
//sys	Read(fd int, p []byte) (n int, errno int)
//sys	read(fd int, buf *byte, nbuf int) (n int, errno int)
//sys	Stat(path string, stat *Stat_t) (errno int)
//sys	Write(fd int, p []byte) (n int, errno int)

//sys	MultimediaInit(subsys int) (errno int)
//sys	MultimediaShutdown() (errno int)

//sys	CondCreate() (cv int, errno int)
//sys	CondWait(cv int, mutex int) (errno int)
//sys	CondSignal(cv int) (errno int)
//sys	CondBroadcast(cv int) (errno int)
//sys	CondTimedWaitAbs(cv int, mutex int, abstime *Timespec) (errno int)
//sys	MutexCreate() (mutex int, errno int)
//sys	MutexLock(mutex int) (errno int)
//sys	MutexUnlock(mutex int) (errno int)
//sys	MutexTryLock(mutex int) (errno int) = SYS_MUTEX_TRYLOCK
//sys	SemCreate() (sema int, errno int)
//sys	SemWait(sema int) (errno int)
//sys	SemPost(sema int) (errno int)
//sys	VideoInit(dx int, dy int) (errno int)
//sys	VideoUpdate(data *uint32) (errno int)
//sys	VideoPollEvent(ev *byte) (errno int)
//sys	VideoShutdown() (errno int)
//sys	AudioInit(fmt int, nreq int, data *int) (errno int)
//sys	AudioShutdown() (errno int)
//sys	AudioStream(data *uint16, size *uintptr) (errno int)

// Hand-written

func Seek(fd int, offset int64, whence int) (newoffset int64, errno int) {
	// Offset passed to system call is 32 bits.  Failure of vision by NaCl.
	if int64(int32(offset)) != offset {
		return 0, ERANGE
	}
	o, _, e := Syscall(SYS_LSEEK, uintptr(fd), uintptr(offset), uintptr(whence));
	return int64(o), int(e);
}

// Sleep by waiting on a condition variable that will never be signaled.
// TODO(rsc): Replace when NaCl adds a proper sleep system call.
var tcv, tmu int

func init() {
	tmu, _ = MutexCreate();
	tcv, _ = CondCreate();
}

func Sleep(ns int64) (errno int) {
	ts := NsecToTimespec(ns);
	var tv Timeval;
	if errno = Gettimeofday(&tv); errno != 0 {
		return
	}
	ts.Sec += tv.Sec;
	ts.Nsec += tv.Usec * 1000;
	switch {
	case ts.Nsec >= 1e9:
		ts.Nsec -= 1e9;
		ts.Sec++;
	case ts.Nsec <= -1e9:
		ts.Nsec += 1e9;
		ts.Sec--;
	}
	if errno = MutexLock(tmu); errno != 0 {
		return
	}
	errno = CondTimedWaitAbs(tcv, tmu, &ts);
	if e := MutexUnlock(tmu); e != 0 && errno == 0 {
		errno = e
	}
	return;
}

// Implemented in NaCl but not here; maybe later:
//	SYS_IOCTL
//	SYS_IMC_*
//	SYS_MMAP ???
//	SYS_SRPC_*
//	SYS_SYSCONF

// Implemented in NaCl but not here; used by runtime instead:
//	SYS_SYSBRK
//	SYS_MMAP
//	SYS_MUNMAP
//	SYS_THREAD_*
//	SYS_TLS_*
//	SYS_SCHED_YIELD

// Not implemented in NaCl but needed to compile other packages.

const (
	SIGTRAP = 5;
)

func Pipe(p []int) (errno int)	{ return ENACL }

func fcntl(fd, cmd, arg int) (val int, errno int) {
	return 0, ENACL
}

func Pread(fd int, p []byte, offset int64) (n int, errno int) {
	return 0, ENACL
}

func Pwrite(fd int, p []byte, offset int64) (n int, errno int) {
	return 0, ENACL
}

func Mkdir(path string, mode int) (errno int)	{ return ENACL }

func Lstat(path string, stat *Stat_t) (errno int) {
	return ENACL
}

func Chdir(path string) (errno int)	{ return ENACL }

func Fchdir(fd int) (errno int)	{ return ENACL }

func Unlink(path string) (errno int)	{ return ENACL }

func Rmdir(path string) (errno int)	{ return ENACL }

func Link(oldpath, newpath string) (errno int) {
	return ENACL
}

func Symlink(path, link string) (errno int)	{ return ENACL }

func Readlink(path string, buf []byte) (n int, errno int) {
	return 0, ENACL
}

func Fchmod(fd int, mode int) (errno int)	{ return ENACL }

func Chown(path string, uid int, gid int) (errno int) {
	return ENACL
}

func Lchown(path string, uid int, gid int) (errno int) {
	return ENACL
}

func Fchown(fd int, uid int, gid int) (errno int) {
	return ENACL
}

func Truncate(name string, size int64) (errno int) {
	return ENACL
}

func Ftruncate(fd int, length int64) (errno int) {
	return ENACL
}

// NaCL doesn't actually implement Getwd, but it also
// don't implement Chdir, so the fallback algorithm
// fails worse than calling Getwd does.

const ImplementsGetwd = true

func Getwd() (wd string, errno int)	{ return "", ENACL }

func Getuid() (uid int)	{ return -1 }

func Geteuid() (euid int)	{ return -1 }

func Getgid() (gid int)	{ return -1 }

func Getegid() (egid int)	{ return -1 }

func Getppid() (ppid int)	{ return -1 }

func Getgroups() (gids []int, errno int)	{ return nil, ENACL }

type Sockaddr interface {
	sockaddr();
}

type SockaddrInet4 struct {
	Port	int;
	Addr	[4]byte;
}

func (*SockaddrInet4) sockaddr()	{}

type SockaddrInet6 struct {
	Port	int;
	Addr	[16]byte;
}

func (*SockaddrInet6) sockaddr()	{}

type SockaddrUnix struct {
	Name string;
}

func (*SockaddrUnix) sockaddr()	{}

const (
	AF_INET	= 1 + iota;
	AF_INET6;
	AF_UNIX;
	IPPROTO_TCP;
	SOCK_DGRAM;
	SOCK_STREAM;
	SOL_SOCKET;
	SOMAXCONN;
	SO_DONTROUTE;
	SO_KEEPALIVE;
	SO_LINGER;
	SO_RCVBUF;
	SO_REUSEADDR;
	SO_SNDBUF;
	TCP_NODELAY;
	WNOHANG;
	WSTOPPED;
	_PTRACE_TRACEME;
)

func Accept(fd int) (nfd int, sa Sockaddr, errno int) {
	return 0, nil, ENACL
}

func Getsockname(fd int) (sa Sockaddr, errno int) {
	return nil, ENACL
}

func Getpeername(fd int) (sa Sockaddr, errno int) {
	return nil, ENACL
}

func Bind(fd int, sa Sockaddr) (errno int)	{ return ENACL }

func Connect(fd int, sa Sockaddr) (errno int)	{ return ENACL }

func Socket(domain, typ, proto int) (fd, errno int) {
	return 0, ENACL
}

func SetsockoptInt(fd, level, opt int, value int) (errno int) {
	return ENACL
}

func SetsockoptTimeval(fd, level, opt int, tv *Timeval) (errno int) {
	return ENACL
}

type Linger struct {
	Onoff	int32;
	Linger	int32;
}

func SetsockoptLinger(fd, level, opt int, l *Linger) (errno int) {
	return ENACL
}

func Listen(s int, n int) (errno int)	{ return ENACL }

type Rusage struct {
	Utime		Timeval;
	Stime		Timeval;
	Maxrss		int32;
	Ixrss		int32;
	Idrss		int32;
	Isrss		int32;
	Minflt		int32;
	Majflt		int32;
	Nswap		int32;
	Inblock		int32;
	Oublock		int32;
	Msgsnd		int32;
	Msgrcv		int32;
	Nsignals	int32;
	Nvcsw		int32;
	Nivcsw		int32;
}

func Wait4(pid int, wstatus *WaitStatus, options int, rusage *Rusage) (wpid int, errno int) {
	return 0, ENACL
}

type WaitStatus uint32

func (WaitStatus) Exited() bool	{ return false }

func (WaitStatus) ExitStatus() int	{ return -1 }

func (WaitStatus) Signal() int	{ return -1 }

func (WaitStatus) CoreDump() bool	{ return false }

func (WaitStatus) Stopped() bool	{ return false }

func (WaitStatus) Continued() bool	{ return false }

func (WaitStatus) StopSignal() int	{ return -1 }

func (WaitStatus) Signaled() bool	{ return false }

func (WaitStatus) TrapCause() int	{ return -1 }


text:
<?php

/**
 * Adminhtml billing agreements tabs view
 */
namespace Magento\Paypal\Block\Adminhtml\Billing\Agreement\View;

class Tabs extends \Magento\Backend\Block\Widget\Tabs
{
    /**
     * Initialize tab
     *
     * @return void
     */
    protected function _construct()
    {
        parent::_construct();
        $this->setId('billing_agreement_view_tabs');
        $this->setDestElementId('billing_agreement_view');
        $this->setTitle(__('Billing Agreement View'));
    }
}


text:
ices
====

[IceS][1] is a source client for the Icecast streaming server. The purpose of this
client is to provide an audio stream to Icecast, so that one or more listeners
can access the stream. With this layout, the source client can be situated
remotely from the Icecast server.

How It Works
------------

```
                +-> (LAN) -> (icecast) -+
(in) -> (ices) -|                       |-> (out) -> (mpd) -> (file)
                +-> (WAN) -> (icecast) -+
```

docker-compose.yml
------------------

```yaml
ices:
  image: easypi/ices-arm
  volumes:
    - ./data:/etc/ices
  devices:
    - /dev/snd
  links:
    - icecast
  restart: always

icecast:
  image: easypi/icecast-arm
  ports:
    - "8000:8000"
  restart: always
```

ices.xml
--------

```xml
<?xml version="1.0"?>
<ices>
    <background>0</background>
    <loglevel>4</loglevel>
    <consolelog>1</consolelog>
    <pidfile>/var/run/ices.pid</pidfile>
    <stream>
        <metadata>
            <name>Example stream name</name>
            <genre>Example genre</genre>
            <description>A short description of your stream</description>
            <url>http://easypi.info</url>
        </metadata>
        <input>
            <module>alsa</module>
            <param name="rate">44100</param>
            <param name="channels">1</param>
            <param name="device">hw:1,0</param>
            <param name="metadata">1</param>
            <param name="metadatafilename">/etc/ices/ices.txt</param>
        </input>
        <instance>
            <hostname>icecast</hostname>
            <port>8000</port>
            <password>hackme</password>
            <mount>/live.ogg</mount>
            <encode>
                <quality>0</quality>
                <samplerate>44100</samplerate>
                <channels>1</channels>
            </encode>
        </instance>
    </stream>
</ices>
```

> You can setup multiple `instances` (e.g. LAN & WAN).

ices.txt
--------

```ini
artist=Various Artists
title=Untitled Song
```

Running
-------

```bash
$ arecord -l
**** List of CAPTURE Hardware Devices ****
card 1: Device [USB Audio Device], device 0: USB Audio [USB Audio]
  Subdevices: 0/1
  Subdevice #0: subdevice #0
$ alsamixer -c 1
$ docker-compose up -d
$ streamripper http://localhost:8000/live.ogg
$ vi data/ices.txt
$ docker-compose kill -s SIGUSR1
```

[1]: http://icecast.org/ices/


text:
<?php

final class PhragmentSnapshotPromoteController extends PhragmentController {

  private $dblob;
  private $id;
  private $targetSnapshot;
  private $targetFragment;
  private $snapshots;
  private $options;

  public function willProcessRequest(array $data) {
    $this->dblob = idx($data, 'dblob', null);
    $this->id = idx($data, 'id', null);
  }

  public function processRequest() {
    $request = $this->getRequest();
    $viewer = $request->getUser();

    // When the user is promoting a snapshot to the latest version, the
    // identifier is a fragment path.
    if ($this->dblob !== null) {
      $this->targetFragment = id(new PhragmentFragmentQuery())
        ->setViewer($viewer)
        ->requireCapabilities(array(
          PhabricatorPolicyCapability::CAN_VIEW,
          PhabricatorPolicyCapability::CAN_EDIT,
        ))
        ->withPaths(array($this->dblob))
        ->executeOne();
      if ($this->targetFragment === null) {
        return new Aphront404Response();
      }

      $this->snapshots = id(new PhragmentSnapshotQuery())
        ->setViewer($viewer)
        ->withPrimaryFragmentPHIDs(array($this->targetFragment->getPHID()))
        ->execute();
    }

    // When the user is promoting a snapshot to another snapshot, the
    // identifier is another snapshot ID.
    if ($this->id !== null) {
      $this->targetSnapshot = id(new PhragmentSnapshotQuery())
        ->setViewer($viewer)
        ->requireCapabilities(array(
          PhabricatorPolicyCapability::CAN_VIEW,
          PhabricatorPolicyCapability::CAN_EDIT,
        ))
        ->withIDs(array($this->id))
        ->executeOne();
      if ($this->targetSnapshot === null) {
        return new Aphront404Response();
      }

      $this->snapshots = id(new PhragmentSnapshotQuery())
        ->setViewer($viewer)
        ->withPrimaryFragmentPHIDs(array(
          $this->targetSnapshot->getPrimaryFragmentPHID(),
        ))
        ->execute();
    }

    // If there's no identifier, just 404.
    if ($this->snapshots === null) {
      return new Aphront404Response();
    }

    // Work out what options the user has.
    $this->options = mpull(
      $this->snapshots,
      'getName',
      'getID');
    if ($this->id !== null) {
      unset($this->options[$this->id]);
    }

    // If there's no options, show a dialog telling the
    // user there are no snapshots to promote.
    if (count($this->options) === 0) {
      return id(new AphrontDialogResponse())->setDialog(
        id(new AphrontDialogView())
          ->setTitle(pht('No snapshots to promote'))
          ->appendParagraph(pht(
            'There are no snapshots available to promote.'))
          ->setUser($request->getUser())
          ->addCancelButton(pht('Cancel')));
    }

    // Handle snapshot promotion.
    if ($request->isDialogFormPost()) {
      $snapshot = id(new PhragmentSnapshotQuery())
        ->setViewer($viewer)
        ->withIDs(array($request->getStr('snapshot')))
        ->executeOne();
      if ($snapshot === null) {
        return new Aphront404Response();
      }

      $snapshot->openTransaction();
        // Delete all existing child entries.
        $children = id(new PhragmentSnapshotChildQuery())
          ->setViewer(PhabricatorUser::getOmnipotentUser())
          ->withSnapshotPHIDs(array($snapshot->getPHID()))
          ->execute();
        foreach ($children as $child) {
          $child->delete();
        }

        if ($this->id === null) {
          // The user is promoting the snapshot to the latest version.
          $children = id(new PhragmentFragmentQuery())
            ->setViewer($viewer)
            ->needLatestVersion(true)
            ->withLeadingPath($this->targetFragment->getPath().'/')
            ->execute();

          // Add the primary fragment.
          id(new PhragmentSnapshotChild())
            ->setSnapshotPHID($snapshot->getPHID())
            ->setFragmentPHID($this->targetFragment->getPHID())
            ->setFragmentVersionPHID(
              $this->targetFragment->getLatestVersionPHID())
            ->save();

          // Add all of the child fragments.
          foreach ($children as $child) {
            id(new PhragmentSnapshotChild())
              ->setSnapshotPHID($snapshot->getPHID())
              ->setFragmentPHID($child->getPHID())
              ->setFragmentVersionPHID($child->getLatestVersionPHID())
              ->save();
          }
        } else {
          // The user is promoting the snapshot to another snapshot. We just
          // copy the other snapshot's child entries and change the snapshot
          // PHID to make it identical.
          $children = id(new PhragmentSnapshotChildQuery())
            ->setViewer($viewer)
            ->withSnapshotPHIDs(array($this->targetSnapshot->getPHID()))
            ->execute();
          foreach ($children as $child) {
            id(new PhragmentSnapshotChild())
              ->setSnapshotPHID($snapshot->getPHID())
              ->setFragmentPHID($child->getFragmentPHID())
              ->setFragmentVersionPHID($child->getFragmentVersionPHID())
              ->save();
          }
        }
      $snapshot->saveTransaction();

      if ($this->id === null) {
        return id(new AphrontRedirectResponse())
          ->setURI($this->targetFragment->getURI());
      } else {
        return id(new AphrontRedirectResponse())
          ->setURI($this->targetSnapshot->getURI());
      }
    }

    return $this->createDialog();
  }

  public function createDialog() {
    $request = $this->getRequest();
    $viewer = $request->getUser();

    $dialog = id(new AphrontDialogView())
      ->setTitle(pht('Promote which snapshot?'))
      ->setUser($request->getUser())
      ->addSubmitButton(pht('Promote'))
      ->addCancelButton(pht('Cancel'));

    if ($this->id === null) {
      // The user is promoting a snapshot to the latest version.
      $dialog->appendParagraph(pht(
        'Select the snapshot you want to promote to the latest version:'));
    } else {
      // The user is promoting a snapshot to another snapshot.
      $dialog->appendParagraph(pht(
        "Select the snapshot you want to promote to '%s':",
        $this->targetSnapshot->getName()));
    }

    $dialog->appendChild(
      id(new AphrontFormSelectControl())
        ->setUser($viewer)
        ->setName('snapshot')
        ->setOptions($this->options));

    return id(new AphrontDialogResponse())->setDialog($dialog);
  }

}


text:
Esse tutorial ensinará a criar cronjobs para envio de e-mails do sistema Map-os utlizando um programa do próprio Windows.

1- Abra o menu de pesquisa do Windows e digite 'Agendador de Tarefas' conforme imagem:

![07mDyiE](https://user-images.githubusercontent.com/10822915/158883875-d688412a-a331-408f-8f17-856f0731edf3.png)

2- Na janela que se abre, clique em 'Criar Tarefa' ou nas guias em cima 'Ação > Criar Tarefa':

![iFptNra](https://user-images.githubusercontent.com/10822915/158883952-63244787-ec9c-4789-91be-c847715f2e14.png)

3- Coloque um nome para a tarefa exemplo 'Enviar e-mails map-os' (para o processo rodar em backgraund e não ficar encomodando aparecendo a tela de cmd executando o comando a cada vez no intervalo de tempo informado, marque a opção 'Executar estando o usuário conectado ou não', esta opção irá exigir seus dados de login do Windows):

![cQxX11L](https://user-images.githubusercontent.com/10822915/158884024-a0f113a9-952c-4721-9f5a-d3af29b03d48.png)

4- Vá para a guia 'Disparadores' depois 'Novo...':

![E7mqa66](https://user-images.githubusercontent.com/10822915/158884109-3d7ff889-e2a2-4426-a4b9-5172978295fe.png)

5- Deixei conforme a imagem, 'Iniciar a tarafa: Em Agendamento', 'Uma vez', marque a opção 'Repetir a tarefa a cada (coloque o tempo que deseja)', e na opção ao lado 'por um período de tempo de: Indefinidamente' e clique em 'Ok':

![bNxPRpx](https://user-images.githubusercontent.com/10822915/158884165-be3fd21f-42fc-49a4-addc-84b841bfc451.png)

6- Vá para a guia 'Ações' e clique em 'Novo...':

![N4uggns](https://user-images.githubusercontent.com/10822915/158884203-00a3da70-359a-4767-9167-86ec3728f021.png)

7- Nesta janela, deixa a opção selecionada 'Iniciar um programa',
e em 'Programa/Script' coloque o caminho do seu php.exe, se for Xampp o caminho padrão é 'C:\xampp\php\php.exe',
em baixo em 'Adicione argumentos' coloque o comando do map-os para processar a fila de e-mails 'index.php email/process' na próxima opção 'Iniciar em' coloque o diretório onde está a pasta do seu map-os desta forma (Para o Xampp) 'C:\xampp\htdocs\mapos' e clique em 'Ok':

![Oi07I9J](https://user-images.githubusercontent.com/10822915/158884265-64539007-95a2-4e71-b80a-7d4ae14bee49.png)

8- Feito os passos acima, clique em 'OK' e a tarefa será criada (Se for solicitado dados de login do windows siga o passo 9, se não pule para o 10):

![2JBbi0Q](https://user-images.githubusercontent.com/10822915/158884324-4d92ef4e-b849-481e-bb29-5b6fbcdced30.png)

9- Se você marcou a opção do passo 3 na primeira guia 'Geral' a opção 'Executar estando o usuário conectado ou não', ao clicar em 'Ok' vai ser solicitado uma credencial de login do Windows, selecione qualquer uma da lista ou o seu usuário do windows e preencha a senha de login (Talves tenha na lista alguma opção que não exija senha),
se mesmo assim informando os dados certos de login nao der certo e ficar dando mensagem de erro, você pode voltar na primeira guia 'Geral' e desmarcar a opção 'Executar estando o usuário conectado ou não', lembrando que está opção é somente para quando a tarefa for executada nao ficar aparecendo uma janela de cmd por uns 3 segundos executando o comando
(Se mesmo seguindo tudo do passo 9 nao aceitar o usuário, você pode clicar na opção 'Alterar usuário ou grupo', na janela que abre, no último campo escreva seu nome de USer ou 'sistema' e depois em verificar nomes ):

![T5XSXql](https://user-images.githubusercontent.com/10822915/158884385-cd5ed389-07c1-438e-9704-424f99c325ca.png)

10- Para testar se funcionou, não precisa esperar o intervalo de tempo selecionado, basta clicar com o botão direito na tarefa e 'Executar', pode tambem selecionar com um clique a tarefa e ao lado nas opções em 'Executar':

![iEY2U45](https://user-images.githubusercontent.com/10822915/158884432-ecf5efda-1974-4c3b-be1f-1e8ec8825e9e.png)

11- Para conferir se executou a fila, vá em seu Map-os 'Configurações' e em 'Emails' e veja o 'Status', se tiver com o status 'Falhou' possivelmente o problema é na sua configuração smtp, para saber se executou ou não basta nao estar em 'Pendente', lembrando que o email em Emitente em configurações precisa estar o mesmo no smtp (Para problemas com configuração de smtp procure por Issues abertas ou fechadas exemplo 'envio de email smtp')

Por fim, você pode criar outra tarefa para o comando 'index.php email/retry', que serve para reenviar e-mails com erros ou não enviados, basta seguir os mesmos passos, somente alterando conforme o passo 7 a opção 'Adicione argumentos'


text:
/* GStreamer EBML I/O
 * (c) 2003 Ronald Bultje <rbultje@ronald.bitfreak.net>
 *
 * ebml-read.c: read EBML data from file/stream
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Library General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details.
 *
 * You should have received a copy of the GNU Library General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

#ifndef __GST_EBML_READ_H__
#define __GST_EBML_READ_H__

#include <gst/gst.h>
#include <gst/base/gstbytereader.h>

G_BEGIN_DECLS

#define GST_TYPE_EBML_READ \
  (gst_ebml_read_get_type ())
#define GST_EBML_READ(obj) \
  (G_TYPE_CHECK_INSTANCE_CAST ((obj), GST_TYPE_EBML_READ, GstEbmlRead))
#define GST_EBML_READ_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_CAST ((klass), GST_TYPE_EBML_READ, GstEbmlReadClass))
#define GST_IS_EBML_READ(obj) \
  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GST_TYPE_EBML_READ))
#define GST_IS_EBML_READ_CLASS(klass) \
  (G_TYPE_CHECK_CLASS_TYPE ((klass), GST_TYPE_EBML_READ))
#define GST_EBML_READ_GET_CLASS(obj) \
  (G_TYPE_INSTANCE_GET_CLASS ((obj), GST_TYPE_EBML_READ, GstEbmlReadClass))

GST_DEBUG_CATEGORY_EXTERN (ebmlread_debug);

/* custom flow return code */
#define  GST_FLOW_PARSE  GST_FLOW_CUSTOM_ERROR

typedef struct _GstEbmlMaster {
  guint64       offset;
  GstByteReader br;
} GstEbmlMaster;

typedef struct _GstEbmlRead {
  GstElement *el;

  GstBuffer *buf;
  guint64 offset;
  GstMapInfo map;

  GArray *readers;
} GstEbmlRead;

typedef GstFlowReturn (*GstPeekData) (gpointer * context, guint peek, const guint8 ** data);

/* returns UNEXPECTED if not enough data */
GstFlowReturn gst_ebml_peek_id_length    (guint32 * _id, guint64 * _length,
                                          guint * _needed,
                                          GstPeekData peek, gpointer * ctx,
                                          GstElement * el, guint64 offset);

void          gst_ebml_read_init         (GstEbmlRead * ebml,
                                          GstElement * el, GstBuffer * buf,
                                          guint64 offset);

void          gst_ebml_read_clear        (GstEbmlRead * ebml);

GstFlowReturn gst_ebml_peek_id           (GstEbmlRead * ebml, guint32 * id);

/* return _PARSE if not enough data to read what is needed, _ERROR or _OK */
GstFlowReturn gst_ebml_read_skip         (GstEbmlRead *ebml);

GstFlowReturn gst_ebml_read_buffer       (GstEbmlRead *ebml,
                                          guint32     *id,
                                          GstBuffer  **buf);

GstFlowReturn gst_ebml_read_uint         (GstEbmlRead *ebml,
                                          guint32     *id,
                                          guint64     *num);

GstFlowReturn gst_ebml_read_sint         (GstEbmlRead *ebml,
                                          guint32     *id,
                                          gint64      *num);

GstFlowReturn gst_ebml_read_float        (GstEbmlRead *ebml,
                                          guint32     *id,
                                          gdouble     *num);

GstFlowReturn gst_ebml_read_ascii        (GstEbmlRead *ebml,
                                          guint32     *id,
                                          gchar      **str);

GstFlowReturn gst_ebml_read_utf8         (GstEbmlRead *ebml,
                                          guint32     *id,
                                          gchar      **str);

GstFlowReturn gst_ebml_read_date         (GstEbmlRead *ebml,
                                          guint32     *id,
                                          gint64      *date);

GstFlowReturn gst_ebml_read_master       (GstEbmlRead *ebml,
                                          guint32     *id);

GstFlowReturn gst_ebml_read_pop_master   (GstEbmlRead *ebml);

GstFlowReturn gst_ebml_read_binary       (GstEbmlRead *ebml,
                                          guint32     *id,
                                          guint8     **binary,
                                          guint64     *length);

GstFlowReturn gst_ebml_read_header       (GstEbmlRead *read,
                                          gchar      **doctype,
                                          guint       *version);

/* Returns current (absolute) position of Ebml parser,
 * i.e. taking into account offset provided at init */
static inline guint64
gst_ebml_read_get_pos (GstEbmlRead * ebml)
{
  GstEbmlMaster *m;

  g_return_val_if_fail (ebml->readers, 0);
  g_return_val_if_fail (ebml->readers->len, 0);

  m = &(g_array_index (ebml->readers, GstEbmlMaster, ebml->readers->len - 1));
  return m->offset + gst_byte_reader_get_pos (&m->br);
}

/* Returns starting offset of Ebml parser */
static inline guint64
gst_ebml_read_get_offset (GstEbmlRead * ebml)
{
  return ebml->offset;
}

static inline GstByteReader *
gst_ebml_read_br (GstEbmlRead * ebml)
{
  g_return_val_if_fail (ebml->readers, NULL);
  g_return_val_if_fail (ebml->readers->len, NULL);

  return &(g_array_index (ebml->readers,
          GstEbmlMaster, ebml->readers->len - 1).br);
}

static inline gboolean
gst_ebml_read_has_remaining (GstEbmlRead * ebml, guint64 bytes_needed,
    gboolean auto_pop)
{
  gboolean res;

  res = (gst_byte_reader_get_remaining (gst_ebml_read_br (ebml)) >= bytes_needed);
  if (G_LIKELY (!res && auto_pop)) {
    gst_ebml_read_pop_master (ebml);
  }

  return G_LIKELY (res);
}

G_END_DECLS

#endif /* __GST_EBML_READ_H__ */